Skip to content

Commit

Permalink
Rebuild WordPress every 20 minutes, short-circuit if no new version i…
Browse files Browse the repository at this point in the history
…s found (#1061)

Splits the WordPress building flow into:

* Nightly version – rebuilt every 24 hours
* Major releases and betas – rebuilt every 20 minutes provided a new
major version is available

This should ensure that new WordPress beta versions are deployed shortly
after the release parties.

 ## Testing instructions

There isn't a good way of testing CI actions :( Eyeball the code, run
`nx bundle-wordpress:major-and-beta playground-wordpress`, and hope for
the best. I'll be monitoring GitHub actions after merging this PR.
  • Loading branch information
adamziel authored Feb 27, 2024
1 parent 7413f8d commit a0f73e6
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 65 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/refresh-wordpress-major-and-beta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: 'Refresh WordPress Major&Beta'

on:
workflow_dispatch:
# Every 20 minutes
schedule:
- cron: '*/20 * * * *'

jobs:
build_and_deploy:
# Only run this workflow from the trunk branch and when it's triggered by dmsnell OR adamziel
if: >
github.ref == 'refs/heads/trunk' && (
github.actor == 'adamziel' ||
github.actor == 'dmsnell' ||
github.actor == 'bgrgicak'
)
runs-on: ubuntu-latest
environment:
name: wordpress-assets
concurrency:
group: check-version-and-run-build
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
clean: true
fetch-depth: 0
persist-credentials: false
- uses: ./.github/actions/prepare-playground
- name: 'Recompile WordPress'
shell: bash
run: npx nx bundle-wordpress:major-and-beta playground-wordpress
- name: Config git user
run: |
git config --global user.name "deployment_bot"
git config --global user.email "deployment_bot@users.noreply.github.com"
git remote set-url origin https://${{ secrets.GH_ACTOR }}:${{ secrets.GH_TOKEN }}@github.com/${{ github.repository }}
git add -A
git commit -a -m "Recompile WordPress major and beta versions"
git pull --rebase
# Push if the pull did not result in a conflict
if [ $? -eq 0 ]; then
git push origin HEAD:trunk
fi;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: 'Refresh WordPress assets'
name: 'Refresh WordPress Nightly'

on:
workflow_dispatch:
# Every day at 8am UTC
# Deploy the nightly version of WordPress every day at 8am UTC
schedule:
- cron: '0 8 * * *'

Expand All @@ -29,14 +29,14 @@ jobs:
- uses: ./.github/actions/prepare-playground
- name: 'Recompile WordPress'
shell: bash
run: npx nx bundle-wordpress:all playground-wordpress
run: npx nx bundle-wordpress:nightly playground-wordpress
- name: Config git user
run: |
git config --global user.name "deployment_bot"
git config --global user.email "deployment_bot@users.noreply.github.com"
git remote set-url origin https://${{ secrets.GH_ACTOR }}:${{ secrets.GH_TOKEN }}@github.com/${{ github.repository }}
git add -A
git commit -a -m "Recompile WordPress"
git commit -a -m "Refresh WordPress Nightly"
git pull --rebase
# Push if the pull did not result in a conflict
if [ $? -eq 0 ]; then
Expand Down
118 changes: 59 additions & 59 deletions packages/playground/wordpress/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,66 +26,55 @@ const parser = yargs(process.argv.slice(2))

const args = parser.argv;

const releasesPageResponse = await fetch(
'https://wordpress.org/download/releases/'
);
const releasesPage = await releasesPageResponse.text();
let latestVersions = 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')

const versionInfo = {};
let beta = null;
if (latestVersions[0].current.includes('beta') || latestVersions[0].current.includes('rc')) {
beta = latestVersions[0];
latestVersions = latestVersions.slice(1);
}

function toVersionInfo(apiVersion, slug=null) {
if (!apiVersion) {
return {};
}
return {
url: apiVersion.download,
version: apiVersion.version,
majorVersion: apiVersion.version.substring(0, 3),
slug: slug || apiVersion.version.substring(0, 3),
};
}

let versionInfo = {};
if (args.wpVersion === 'nightly') {
versionInfo.url =
'https://wordpress.org/nightly-builds/wordpress-latest.zip';
versionInfo.version = 'nightly';
versionInfo.majorVersion = 'nightly';
versionInfo.slug = 'nightly';
} else if (args.wpVersion === 'beta') {
const matches = releasesPage.match(
/https:\/\/wordpress\.org\/wordpress-(\d\.\d-(?:RC|beta|alpha)\d)\.zip/
);
if (!matches) {
throw new Error('Could not find a beta version');
}
versionInfo.url = matches[0];
versionInfo.version = matches[1];
versionInfo.majorVersion = matches[1].substring(0, 3);
versionInfo.slug = 'beta';
versionInfo = toVersionInfo(beta, 'beta');
} else if (args.wpVersion.startsWith('latest')) {
const latestBranches = releasesPage
.match(/(\d\.\d) Branch/g)
.slice(0, 4)
.map((branch) => branch.replace(' Branch', ''));
const wpBranch = {
latest: latestBranches[0],
'latest-minus-1': latestBranches[1],
'latest-minus-2': latestBranches[2],
'latest-minus-3': latestBranches[3],
const relevantApiVersion = {
latest: latestVersions[0],
'latest-minus-1': latestVersions[1],
'latest-minus-2': latestVersions[2],
'latest-minus-3': latestVersions[3],
}[args.wpVersion];

const matches = releasesPage.match(
new RegExp(`https://wordpress.org/wordpress-(${wpBranch}\\.\\d)\\.zip`)
);
if (!matches) {
throw new Error('Could not find version ' + wpBranch);
}
versionInfo.url = matches[0];
versionInfo.version = matches[1];
versionInfo.majorVersion = matches[1].substring(0, 3);
versionInfo.slug = versionInfo.majorVersion;
versionInfo = toVersionInfo(relevantApiVersion);
} else if (args.wpVersion.match(/\d\.\d/)) {
const matches = releasesPage.match(
new RegExp(
`https://wordpress.org/wordpress-(${args.wpVersion}(?:\\.\\d)?)\\.zip`
)
);
if (!matches) {
throw new Error('Could not find version ' + args.wpVersion);
}
versionInfo.url = matches[0];
versionInfo.version = matches[1];
versionInfo.majorVersion = args.wpVersion;
versionInfo.slug = versionInfo.majorVersion;
} else {
process.stdout.write(`WP version ${parser.wpVersion} is not supported\n`);
const relevantApiVersion = latestVersions.find((v) => v.version.startsWith(args.wpVersion));
versionInfo = toVersionInfo(relevantApiVersion);
}

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 @@ -94,6 +83,27 @@ const sourceDir = path.dirname(new URL(import.meta.url).pathname);
const outputAssetsDir = path.resolve(process.cwd(), args.outputAssets);
const outputJsDir = path.resolve(process.cwd(), args.outputJs);

// Short-circuit if the version is already downloaded
const versionsPath = `${outputJsDir}/wp-versions.json`;
let versions = {};
try {
const data = await fs.readFile(versionsPath, 'utf8');
versions = JSON.parse(data);
} catch (e) {
// If the existing JSON file doesn't exist or cannot be read,
// just ignore that and assume an empty one.
versions = {};
}

// We don't get granular version info for nightly, so we always download it.
// Otherwise, we only download if the version is not already the latest one
// in the repository.
if (versionInfo.slug !== 'nightly' && versions[versionInfo.slug] === versionInfo.version) {
process.stdout.write(`The requested version was ${args.wpVersion}, but it's latest release (${versionInfo.version}) is already downloaded\n`);
process.exit(0);
}


// Build WordPress
await asyncSpawn(
'docker',
Expand Down Expand Up @@ -151,16 +161,6 @@ await asyncSpawn(
);

// Update the WordPress versions JSON
const versionsPath = `${outputJsDir}/wp-versions.json`;
let versions = {};
try {
const data = await fs.readFile(versionsPath, 'utf8');
versions = JSON.parse(data);
} catch (e) {
// If the existing JSON file doesn't exist or cannot be read,
// just ignore that and create a new one.
}

// Set WordPress version
versions[versionInfo.slug] = versionInfo.version;

Expand Down
19 changes: 17 additions & 2 deletions packages/playground/wordpress/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,30 @@
}
},
"bundle-wordpress:all": {
"executor": "nx:noop",
"dependsOn": [
"bundle-wordpress:nightly",
"bundle-wordpress:major-and-beta"
]
},
"bundle-wordpress:nightly": {
"executor": "nx:run-commands",
"options": {
"commands": [
"node packages/playground/wordpress/build/build.js --wp-version=nightly --output-js=packages/playground/wordpress/src/wordpress --output-assets=packages/playground/wordpress/public"
],
"parallel": false
}
},
"bundle-wordpress:major-and-beta": {
"executor": "nx:run-commands",
"options": {
"commands": [
"node packages/playground/wordpress/build/build.js --wp-version=latest-minus-3 --output-js=packages/playground/wordpress/src/wordpress --output-assets=packages/playground/wordpress/public",
"node packages/playground/wordpress/build/build.js --wp-version=latest-minus-2 --output-js=packages/playground/wordpress/src/wordpress --output-assets=packages/playground/wordpress/public",
"node packages/playground/wordpress/build/build.js --wp-version=latest-minus-1 --output-js=packages/playground/wordpress/src/wordpress --output-assets=packages/playground/wordpress/public",
"node packages/playground/wordpress/build/build.js --wp-version=latest --output-js=packages/playground/wordpress/src/wordpress --output-assets=packages/playground/wordpress/public",
"node packages/playground/wordpress/build/build.js --wp-version=beta --output-js=packages/playground/wordpress/src/wordpress --output-assets=packages/playground/wordpress/public",
"node packages/playground/wordpress/build/build.js --wp-version=nightly --output-js=packages/playground/wordpress/src/wordpress --output-assets=packages/playground/wordpress/public"
"node packages/playground/wordpress/build/build.js --wp-version=beta --output-js=packages/playground/wordpress/src/wordpress --output-assets=packages/playground/wordpress/public"
],
"parallel": false
}
Expand Down

0 comments on commit a0f73e6

Please sign in to comment.