From 79c65d6070042a4dab2c79cc41264b6d1fa90ce0 Mon Sep 17 00:00:00 2001 From: Bero Date: Fri, 1 Nov 2024 15:45:41 +0100 Subject: [PATCH] [WordPress build] Only build the latest patch version of WordPress (#1955) This PR ensures we always build the latest patch version of each minor WordPress version when running the WordPress build. Playground supports only the latest patch version of each minor WordPress release (e.g. 6.6, 6.5). But before this PR, the WordPress build script could include multiple patch versions of WordPress, such as 6.6.2 and 6.6.1. When this happens the latest version would correctly be the latest minor version (6.6) and point to the latest patch (6.6.2), but the minus one minor version would be the older patch of the latest minor version (6.6.1) instead of the previous minor version (6.5). This means that a build script like `npx nx bundle-wordpress:major-and-beta playground-wordpress-builds` would correctly build the latest minor version (6.6) with the code from the latest patch (6.6.2). After that, instead of building the older minor version (6.5) it would rebuild the latest minor version (6.6) with the code from an older patch (6.6.1). As a consequence, our git history would be polluted by commits every time this _Refresh WordPress Major&Beta_ workflow runs. To address this we filter the list of latest versions to include only the latest patch for each minor version. Older patches are excluded from the list. This PR also prevents the _Refresh WordPress Major&Beta_ workflow from committing changes when the build fails. ## Testing Instructions (or ideally a Blueprint) - Run ` npx nx bundle-wordpress:major-and-beta playground-wordpress-builds` and confirm there were no changes --- .../refresh-wordpress-major-and-beta.yml | 13 ++-- .../wordpress-builds/build/build.js | 65 ++++++++++++++----- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/.github/workflows/refresh-wordpress-major-and-beta.yml b/.github/workflows/refresh-wordpress-major-and-beta.yml index f9a614e49f..86a70e5d2d 100644 --- a/.github/workflows/refresh-wordpress-major-and-beta.yml +++ b/.github/workflows/refresh-wordpress-major-and-beta.yml @@ -12,8 +12,8 @@ on: - cron: '*/20 * * * *' jobs: - build_and_deploy: - # Only run this workflow from the trunk branch and when it's triggered by dmsnell OR adamziel + build_wordpress_major_and_beta_push_to_github_and_deploy_website: + # Only run this workflow from the trunk branch and when it's triggered by a maintainer listed below if: > github.ref == 'refs/heads/trunk' && ( github.actor == 'adamziel' || @@ -39,6 +39,7 @@ jobs: curl -fsSL https://bun.sh/install | bash - uses: ./.github/actions/prepare-playground - name: 'Recompile WordPress' + id: build shell: bash env: FORCE_REBUILD: ${{ github.event.inputs.force_rebuild }} @@ -48,13 +49,13 @@ jobs: run: | if [ -z "$(git status --porcelain)" ]; then echo "No changes" - echo 'CHANGES=0' >> $GITHUB_OUTPUT + echo 'COMMIT_CHANGES=0' >> $GITHUB_OUTPUT else echo "Changes detected" - echo 'CHANGES=1' >> $GITHUB_OUTPUT + echo 'COMMIT_CHANGES=1' >> $GITHUB_OUTPUT fi - name: Push rebuilt WordPress to GitHub - if: steps.changes.outputs.CHANGES == '1' + if: steps.changes.outputs.COMMIT_CHANGES == '1' run: | git config --global user.name "deployment_bot" git config --global user.email "deployment_bot@users.noreply.github.com" @@ -67,7 +68,7 @@ jobs: git push origin HEAD:trunk fi; - name: Deploy website - if: steps.changes.outputs.CHANGES == '1' + if: steps.changes.outputs.COMMIT_CHANGES == '1' uses: benc-uk/workflow-dispatch@v1 with: workflow: build-website.yml diff --git a/packages/playground/wordpress-builds/build/build.js b/packages/playground/wordpress-builds/build/build.js index 4d62bc445d..c8fb12be4c 100644 --- a/packages/playground/wordpress-builds/build/build.js +++ b/packages/playground/wordpress-builds/build/build.js @@ -1,8 +1,8 @@ - import path, { join } from 'path'; import { spawn } from 'child_process'; import yargs from 'yargs'; import { promises as fs, statSync } from 'fs'; +import semver from 'semver'; const parser = yargs(process.argv.slice(2)) .usage('Usage: $0 [options]') @@ -25,27 +25,56 @@ const parser = yargs(process.argv.slice(2)) }, force: { type: 'boolean', - description: 'Force rebuild even if the version is already downloaded', + description: + 'Force rebuild even if the version is already downloaded', default: process.env.FORCE_REBUILD === 'true', }, }); const args = parser.argv; -let latestVersions = await fetch('https://api.wordpress.org/core/version-check/1.7/?channel=beta') - .then((res) => res.json()) +let wpVersions = await fetch( + 'https://api.wordpress.org/core/version-check/1.7/?channel=beta' +).then((res) => res.json()); -latestVersions = latestVersions - .offers - .filter((v) => v.response === 'autoupdate') +wpVersions = wpVersions.offers.filter((v) => v.response === 'autoupdate'); let beta = null; -if (latestVersions[0].current.includes('beta') || latestVersions[0].current.toLowerCase().includes('rc')) { - beta = latestVersions[0]; - latestVersions = latestVersions.slice(1); +if ( + wpVersions[0].current.includes('beta') || + wpVersions[0].current.toLowerCase().includes('rc') +) { + beta = wpVersions[0]; + wpVersions = wpVersions.slice(1); } -function toVersionInfo(apiVersion, slug=null) { +/** + * Create a list of the latest patch versions for each major.minor version. + * + * Sometimes the API may include multiple patch versions for the same major.minor version. + * Playground builds only the latest patch version. + */ +const latestVersions = wpVersions.reduce((versionAccumulator, wpVersion) => { + const [major, minor] = wpVersion.version.split('.'); + const majorMinor = `${major}.${minor}`; + + const currentVersionIndex = versionAccumulator.findIndex((v) => + v.version.startsWith(majorMinor) + ); + if (-1 === currentVersionIndex) { + versionAccumulator.push(wpVersion); + } else if ( + semver.gt( + wpVersion.version, + versionAccumulator[currentVersionIndex].version + ) + ) { + versionAccumulator[currentVersionIndex] = wpVersion; + } + return versionAccumulator; +}, []); + +function toVersionInfo(apiVersion, slug = null) { if (!apiVersion) { return {}; } @@ -75,11 +104,13 @@ if (args.wpVersion === 'nightly') { }[args.wpVersion]; versionInfo = toVersionInfo(relevantApiVersion); } else if (args.wpVersion.match(/\d\.\d/)) { - const relevantApiVersion = latestVersions.find((v) => v.version.startsWith(args.wpVersion)); + const relevantApiVersion = latestVersions.find((v) => + v.version.startsWith(args.wpVersion) + ); versionInfo = toVersionInfo(relevantApiVersion); } -if(!versionInfo.url) { +if (!versionInfo.url) { process.stdout.write(`WP version ${args.wpVersion} is not supported\n`); process.stdout.write(await parser.getHelp()); process.exit(1); @@ -101,7 +132,11 @@ try { versions = {}; } -if (!args.force && versionInfo.slug !== 'nightly' && versions[versionInfo.slug] === versionInfo.version) { +if ( + !args.force && + versionInfo.slug !== 'nightly' && + versions[versionInfo.slug] === versionInfo.version +) { process.stdout.write( `The requested version was ${args.wpVersion}, but its latest release (${versionInfo.version}) is already downloaded\n` ); @@ -124,7 +159,7 @@ try { '../../cli/src/cli.ts', 'run-blueprint', `--wp=${versionInfo.url}`, - `--mount-before-install=${wordpressDir}:/wordpress` + `--mount-before-install=${wordpressDir}:/wordpress`, ], { cwd: sourceDir, stdio: 'inherit' } );