Skip to content

Commit

Permalink
[WordPress build] Only build the latest patch version of WordPress (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
bgrgicak authored Nov 1, 2024
1 parent a91cbce commit 79c65d6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/refresh-wordpress-major-and-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' ||
Expand All @@ -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 }}
Expand All @@ -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 "[email protected]"
Expand All @@ -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
Expand Down
65 changes: 50 additions & 15 deletions packages/playground/wordpress-builds/build/build.js
Original file line number Diff line number Diff line change
@@ -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]')
Expand All @@ -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 {};
}
Expand Down Expand Up @@ -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);
Expand All @@ -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`
);
Expand All @@ -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' }
);
Expand Down

0 comments on commit 79c65d6

Please sign in to comment.