Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebuild WordPress every 20 minutes, short-circuit if no new version is found #1061

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading