|
| 1 | +version: 2.1 |
| 2 | +description: | |
| 3 | + Newspack shared CI config. |
| 4 | +display: |
| 5 | + home_url: https://newspack.com |
| 6 | + source_url: https://www.github.com/Automattic/newspack-scripts |
| 7 | +commands: |
| 8 | + checkout_with_workspace: |
| 9 | + description: | |
| 10 | + Chechout code and attach workspace. |
| 11 | + steps: |
| 12 | + - checkout |
| 13 | + - attach_workspace: |
| 14 | + at: ~/ |
| 15 | + set_node_version: |
| 16 | + description: | |
| 17 | + Set node version to the one defined in the .nvmrc file. |
| 18 | + steps: |
| 19 | + - run: |
| 20 | + command: | |
| 21 | + # https://support.circleci.com/hc/en-us/articles/360051656632-Swap-node-version-on-CircleCI-convenience-image |
| 22 | + set +e |
| 23 | + wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash |
| 24 | + export NVM_DIR="$HOME/.nvm" |
| 25 | + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" |
| 26 | + [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" |
| 27 | + nvm install |
| 28 | + nvm use |
| 29 | + # https://stackoverflow.com/a/61823737/3772847 |
| 30 | + NODE_DIR=$(dirname $(which node)) |
| 31 | + echo "export PATH=$NODE_DIR:\$PATH" >> $BASH_ENV |
| 32 | + name: Install nvm and use the node version defined in .nvmrc |
| 33 | + - run: |
| 34 | + command: node --version |
| 35 | + name: Check node version (should match the version set in project's .nvmrc file) |
| 36 | +executors: |
| 37 | + default: |
| 38 | + description: PHP executor |
| 39 | + docker: |
| 40 | + - image: cimg/php:<<parameters.tag>> |
| 41 | + parameters: |
| 42 | + tag: |
| 43 | + default: 8.3-node |
| 44 | + description: PHP image version |
| 45 | + type: string |
| 46 | + php-81: |
| 47 | + description: PHP executor |
| 48 | + docker: |
| 49 | + - image: cimg/php:<<parameters.tag>> |
| 50 | + parameters: |
| 51 | + tag: |
| 52 | + default: 8.1-node |
| 53 | + description: PHP image version |
| 54 | + type: string |
| 55 | +jobs: |
| 56 | + build: |
| 57 | + description: | |
| 58 | + Install node dependencies. |
| 59 | + executor: default |
| 60 | + steps: |
| 61 | + - checkout_with_workspace |
| 62 | + - set_node_version |
| 63 | + - restore_cache: |
| 64 | + key: v1-npm-{{ checksum "package.json" }}-{{checksum "package-lock.json" }}-{{ checksum ".nvmrc" }} |
| 65 | + - run: |
| 66 | + command: | |
| 67 | + if test -d "node_modules"; then |
| 68 | + echo "package.json and package-lock.json unchanged. Using cache." |
| 69 | + else |
| 70 | + npm ci --legacy-peer-deps --loglevel warn --yes |
| 71 | + fi |
| 72 | + name: Install node dependencies |
| 73 | + - save_cache: |
| 74 | + key: v1-npm-{{ checksum "package.json" }}-{{checksum "package-lock.json" }}-{{ checksum ".nvmrc" }} |
| 75 | + paths: |
| 76 | + - node_modules |
| 77 | + - persist_to_workspace: |
| 78 | + paths: |
| 79 | + - project |
| 80 | + root: ~/ |
| 81 | + build-distributable: |
| 82 | + description: | |
| 83 | + Build the distributable files, so they are available as CI artifacts. |
| 84 | + executor: default |
| 85 | + parameters: |
| 86 | + archive-name: |
| 87 | + default: "" |
| 88 | + description: Name of the ZIP archive distributable. |
| 89 | + type: string |
| 90 | + steps: |
| 91 | + - checkout_with_workspace |
| 92 | + - set_node_version |
| 93 | + - run: |
| 94 | + command: sudo apt-get update && sudo apt-get install rsync |
| 95 | + name: Install rsync |
| 96 | + - run: |
| 97 | + command: composer install --no-dev --no-scripts |
| 98 | + name: Install PHP packages |
| 99 | + - run: |
| 100 | + command: npm run build && npm run release:archive |
| 101 | + name: Build plugin files |
| 102 | + - store_artifacts: |
| 103 | + path: release/<<parameters.archive-name>>.zip |
| 104 | + check-typescript: |
| 105 | + description: | |
| 106 | + Validate TypeScript. |
| 107 | + executor: default |
| 108 | + steps: |
| 109 | + - checkout_with_workspace |
| 110 | + - set_node_version |
| 111 | + - run: |
| 112 | + command: npm run typescript:check |
| 113 | + name: Validate TypeScript |
| 114 | + generate-docs: |
| 115 | + description: | |
| 116 | + Generate documentation. This will create a new branch called docs and push the HTML documentation to it, as /docs directory. To make this work with GH pages, visit <repo-url>/settings/pages, set branch to "docs", and folder to "/docs". |
| 117 | + executor: php-81 |
| 118 | + steps: |
| 119 | + - checkout |
| 120 | + - run: |
| 121 | + command: | |
| 122 | + sudo apt-get update && sudo apt-get install -y graphviz plantuml |
| 123 | + name: Install dependencies |
| 124 | + - run: |
| 125 | + command: | |
| 126 | + curl -L -o ./phpdocumentor https://phpdoc.org/phpDocumentor.phar |
| 127 | + chmod +x ./phpdocumentor |
| 128 | + name: Download PHPDocumentor |
| 129 | + - run: |
| 130 | + command: | |
| 131 | + ./phpdocumentor run -d . -t ./docs |
| 132 | + name: Generate documentation |
| 133 | + - run: |
| 134 | + command: | |
| 135 | + git config user.name "${GIT_COMMITTER_NAME}" |
| 136 | + git config user.email "${GITHUB_COMMITER_EMAIL}" |
| 137 | + git checkout -b docs |
| 138 | + git add -f docs |
| 139 | + git commit -m "Update the docs" |
| 140 | + git push "https://${GITHUB_TOKEN}@github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}.git" --force |
| 141 | + name: Switch to docs branch, commit, and push |
| 142 | + i18n: |
| 143 | + description: | |
| 144 | + Create translation files. |
| 145 | + executor: default |
| 146 | + steps: |
| 147 | + - checkout_with_workspace |
| 148 | + - run: |
| 149 | + command: | |
| 150 | + COMMIT_AUTHOR=$(git log -1 --pretty=format:'%ae') |
| 151 | + if [ "$COMMIT_AUTHOR" = "$GITHUB_COMMITER_EMAIL" ]; then |
| 152 | + echo "Commit was made by bot ($(git rev-parse --short HEAD)), skipping translation update" |
| 153 | + circleci step halt |
| 154 | + fi |
| 155 | + name: Check if commit author is bot |
| 156 | + - run: |
| 157 | + command: | |
| 158 | + curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar |
| 159 | + chmod +x wp-cli.phar |
| 160 | + sudo mv wp-cli.phar /usr/local/bin/wp |
| 161 | + name: Install WP CLI |
| 162 | + - run: |
| 163 | + command: | |
| 164 | + npm run build || true |
| 165 | + name: Build plugin files, so dist dir is available |
| 166 | + - run: |
| 167 | + command: | |
| 168 | + # Theme is excluded, more in https://github.com/Automattic/newspack-theme/pull/2458 |
| 169 | + if [ "$CIRCLE_PROJECT_REPONAME" = "newspack-theme" ]; then |
| 170 | + cd ./newspack-theme |
| 171 | + wp i18n make-pot . languages/${CIRCLE_PROJECT_REPONAME}.pot --exclude='src' --domain=${CIRCLE_PROJECT_REPONAME} |
| 172 | + cd - |
| 173 | + else |
| 174 | + wp i18n make-pot . languages/${CIRCLE_PROJECT_REPONAME}.pot --domain=${CIRCLE_PROJECT_REPONAME} |
| 175 | + fi |
| 176 | + name: Create POT translation files |
| 177 | + - run: |
| 178 | + command: | |
| 179 | + if [ "$CIRCLE_PROJECT_REPONAME" = "newspack-theme" ]; then |
| 180 | + cd ./newspack-theme |
| 181 | + fi |
| 182 | +
|
| 183 | + sudo apt-get update && sudo apt-get install -y gettext |
| 184 | +
|
| 185 | + cd languages |
| 186 | +
|
| 187 | + # Create PO files from POT if they don't exist |
| 188 | + if [ ! -f "${CIRCLE_PROJECT_REPONAME}-en_US.po" ]; then |
| 189 | + echo "Creating ${CIRCLE_PROJECT_REPONAME}-en_US.po from POT file" |
| 190 | + wp i18n update-po ${CIRCLE_PROJECT_REPONAME}.pot . |
| 191 | + else |
| 192 | + echo "${CIRCLE_PROJECT_REPONAME}-en_US.po file already exists, skipping creation" |
| 193 | + fi |
| 194 | +
|
| 195 | + for po in *.po; do |
| 196 | + if [ -f "$po" ]; then |
| 197 | + echo "Processing file $po …" |
| 198 | + # Update translations according to the new POT file |
| 199 | + msgmerge $po $CIRCLE_PROJECT_REPONAME.pot -o $po.out |
| 200 | + mv $po.out $po |
| 201 | + msgfmt $po -o $(basename $po .po).mo |
| 202 | + # no-purge since we need the JS translations for the next run |
| 203 | + wp i18n make-json --no-purge $po . |
| 204 | + fi |
| 205 | + done |
| 206 | + name: Create JSON translation files |
| 207 | + - run: |
| 208 | + command: | |
| 209 | + if [ "$CIRCLE_PROJECT_REPONAME" = "newspack-theme" ]; then |
| 210 | + cd ./newspack-theme |
| 211 | + fi |
| 212 | + if [ -d "languages" ]; then |
| 213 | + LINES_CHANGED=$(git diff --numstat languages/ | awk '{sum += $1 + $2} END {print sum}') |
| 214 | + # If no existing files were changed, check for new files |
| 215 | + if [ -z "$LINES_CHANGED" ] || [ "$LINES_CHANGED" -eq 0 ]; then |
| 216 | + LINES_CHANGED=$(git ls-files --others --exclude-standard languages/ | xargs wc -l 2>/dev/null | tail -1 | awk '{print $1}') |
| 217 | + fi |
| 218 | + else |
| 219 | + LINES_CHANGED=0 |
| 220 | + fi |
| 221 | + LINES_CHANGED=${LINES_CHANGED:-0} |
| 222 | + echo "Lines changed in languages/: $LINES_CHANGED" |
| 223 | + if [ "$LINES_CHANGED" -gt 3 ]; then |
| 224 | + git config user.email "$GITHUB_COMMITER_EMAIL" |
| 225 | + git config user.name "$GIT_COMMITTER_NAME" |
| 226 | + git remote set-url origin https://$GITHUB_TOKEN@github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME.git |
| 227 | + git add languages/ |
| 228 | + git commit -m "chore: update translation files [skip ci]" |
| 229 | + git push origin $CIRCLE_BRANCH |
| 230 | + fi |
| 231 | + name: Commit translation files |
| 232 | + lint-js-scss: |
| 233 | + description: | |
| 234 | + Lint the JS & SCSS (temporarily skipped) files. |
| 235 | + executor: default |
| 236 | + steps: |
| 237 | + - checkout_with_workspace |
| 238 | + - set_node_version |
| 239 | + - run: |
| 240 | + command: npm run lint:js |
| 241 | + name: Run Linter |
| 242 | + lint-php: |
| 243 | + description: | |
| 244 | + Lint PHP files. |
| 245 | + executor: default |
| 246 | + steps: |
| 247 | + - checkout |
| 248 | + - run: |
| 249 | + command: | |
| 250 | + composer install |
| 251 | + ./vendor/bin/phpcs |
| 252 | + name: Lint Files |
| 253 | + post-release: |
| 254 | + description: | |
| 255 | + Perform post-release tasks. |
| 256 | + executor: default |
| 257 | + steps: |
| 258 | + - checkout_with_workspace |
| 259 | + - run: |
| 260 | + command: ./node_modules/newspack-scripts/scripts/github/post-release.sh |
| 261 | + name: Perform post-release chores |
| 262 | + release: |
| 263 | + description: | |
| 264 | + Release new version. |
| 265 | + executor: default |
| 266 | + steps: |
| 267 | + - checkout_with_workspace |
| 268 | + - set_node_version |
| 269 | + - run: |
| 270 | + command: sudo apt-get update && sudo apt-get install rsync |
| 271 | + name: Install rsync |
| 272 | + - run: |
| 273 | + command: composer install --no-dev --no-scripts |
| 274 | + name: Install PHP packages |
| 275 | + - run: |
| 276 | + command: npm run release |
| 277 | + name: Release new version |
| 278 | + - persist_to_workspace: |
| 279 | + paths: |
| 280 | + - project |
| 281 | + root: ~/ |
| 282 | + test-js: |
| 283 | + description: | |
| 284 | + Run JS tests. |
| 285 | + executor: default |
| 286 | + steps: |
| 287 | + - checkout_with_workspace |
| 288 | + - set_node_version |
| 289 | + - run: |
| 290 | + command: npm run test |
| 291 | + name: Run JS Tests |
| 292 | + test-php: |
| 293 | + description: | |
| 294 | + Run PHP tests. |
| 295 | + docker: |
| 296 | + - image: cimg/php:8.3 |
| 297 | + - image: circleci/mysql:5.6.50 |
| 298 | + environment: |
| 299 | + - WP_TESTS_DIR: /tmp/wordpress-tests-lib |
| 300 | + - WP_CORE_DIR: /tmp/wordpress/ |
| 301 | + steps: |
| 302 | + - checkout |
| 303 | + - run: |
| 304 | + command: | |
| 305 | + echo "export PATH=$HOME/.composer/vendor/bin:$PATH" >> $BASH_ENV |
| 306 | + source /home/circleci/.bashrc |
| 307 | + name: Setup Environment Variables |
| 308 | + - run: |
| 309 | + command: | |
| 310 | + sudo apt-get update && sudo apt-get install -y subversion default-mysql-client |
| 311 | + name: Install Dependencies |
| 312 | + - run: |
| 313 | + command: | |
| 314 | + composer update |
| 315 | + rm -rf $WP_TESTS_DIR $WP_CORE_DIR |
| 316 | + bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest |
| 317 | + # https://circleci.com/docs/code-coverage/#php |
| 318 | + XDEBUG_MODE=coverage phpdbg -qrr vendor/bin/phpunit --coverage-clover coverage.xml |
| 319 | + name: Run Tests with Coverage |
| 320 | + - run: |
| 321 | + command: | |
| 322 | + if [[ -n $CODECOV_TOKEN ]]; then |
| 323 | + # download Codecov CLI |
| 324 | + curl -Os https://cli.codecov.io/latest/linux/codecov |
| 325 | +
|
| 326 | + # integrity check |
| 327 | + curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --no-default-keyring --keyring trustedkeys.gpg --import # One-time step |
| 328 | + curl -Os https://cli.codecov.io/latest/linux/codecov |
| 329 | + curl -Os https://cli.codecov.io/latest/linux/codecov.SHA256SUM |
| 330 | + curl -Os https://cli.codecov.io/latest/linux/codecov.SHA256SUM.sig |
| 331 | + gpgv codecov.SHA256SUM.sig codecov.SHA256SUM |
| 332 | +
|
| 333 | + shasum -a 256 -c codecov.SHA256SUM |
| 334 | + sudo chmod +x codecov |
| 335 | + ./codecov upload-process -t $CODECOV_TOKEN |
| 336 | + else |
| 337 | + echo "CODECOV_TOKEN is not set. Skipping upload to Codecov." |
| 338 | + fi |
| 339 | + name: Upload Test Results to Codecov |
| 340 | + |
0 commit comments