Skip to content

Commit

Permalink
WordPress Build: Use Playground CLI to produce minified WordPress bui…
Browse files Browse the repository at this point in the history
…ld (without the SQLite integration plugin)

Uses Playground CLI to produce a minified WordPress build, replacing the custom bash-based WordPress installation flow that relied on wget, unzip, sed, wp-cli etc.

This is to dogfood Playground CLI and put the boot protocol to a practical use. With this PR, CLI Playground produces a minified WordPress version that is later used to boot the web Playground.

Depends on #1423 #1423 #1425

 ## Testing instructions

Rebuild the latest WordPress with

```shell
rm -rf packages/playground/wordpress-builds/public/wp-6.5
npx nx bundle-wordpress:major-and-beta playground-wordpress-builds
```

Then run npm run dev and confirm the local Playground loads without any issues.

Related: #1398
  • Loading branch information
adamziel committed May 18, 2024
1 parent 51d1d88 commit 05c57e0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 78 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/refresh-wordpress-major-and-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ jobs:
ref: ${{ github.event.pull_request.head.ref }}
clean: true
persist-credentials: false
- name: 'Install bun (for the changelog)'
run: |
curl -fsSL https://bun.sh/install | bash
- uses: ./.github/actions/prepare-playground
- name: 'Recompile WordPress'
shell: bash
env:
FORCE_REBUILD: ${{ github.event.inputs.force_rebuild }}
run: npx nx bundle-wordpress:major-and-beta playground-wordpress-builds
run: PATH="${PATH}:${HOME}/.bun/bin" npx nx bundle-wordpress:major-and-beta playground-wordpress-builds
- name: Check for uncommitted changes
id: changes
run: |
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/refresh-wordpress-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ jobs:
clean: true
persist-credentials: false
- uses: ./.github/actions/prepare-playground
- name: 'Install bun (for the changelog)'
run: |
curl -fsSL https://bun.sh/install | bash
- name: 'Recompile WordPress'
shell: bash
run: npx nx bundle-wordpress:nightly playground-wordpress-builds
run: PATH="${PATH}:${HOME}/.bun/bin" npx nx bundle-wordpress:nightly playground-wordpress-builds
- name: Config git user
run: |
git config --global user.name "deployment_bot"
Expand Down
74 changes: 13 additions & 61 deletions packages/playground/wordpress-builds/build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,15 @@ RUN set -euxo pipefail;\
apt-get update; \
emsdk install latest;

# Download specific version of WordPress
RUN wget -O wp.zip $WP_ZIP_URL && \
unzip -q wp.zip && \
rm wp.zip

# === Create the mu-plugins directory ===
RUN mkdir wordpress/wp-content/mu-plugins

# Enable the SQLite support as a mu-plugin to:
# * Ensure it won't accidentally be disabled by the user
# * Prevent it from blocking a multisite setup where disabling all the plugins is required
# https://github.com/WordPress/sqlite-database-integration
RUN git clone https://github.com/WordPress/sqlite-database-integration.git \
wordpress/wp-content/mu-plugins/sqlite-database-integration \
--branch main \
--single-branch \
--depth 1 && \
rm -rf wordpress/wp-content/mu-plugins/sqlite-database-integration/.git && \
# Required by the SQLite integration plugin:
cat wordpress/wp-content/mu-plugins/sqlite-database-integration/db.copy \
| sed "s#'{SQLITE_IMPLEMENTATION_FOLDER_PATH}'#__DIR__.'/mu-plugins/sqlite-database-integration/'#g" \
| sed "s#'{SQLITE_PLUGIN}'#__DIR__.'/mu-plugins/sqlite-database-integration/load.php'#g" \
> wordpress/wp-content/db.php && \
echo '<?php require_once __DIR__ . "/sqlite-database-integration/load.php"; ' > \
wordpress/wp-content/mu-plugins/0-sqlite.php && \
cp wordpress/wp-config-sample.php wordpress/wp-config.php
COPY ./wordpress /root/wordpress

# === Minify WordPress ===

# Remove non-default themes
RUN cat wordpress/wp-includes/default-constants.php | awk -F"'" "/define\( 'WP_DEFAULT_THEME'/"'{print $4}' > wordpress/.default_theme
RUN export KEEP_THEME=$(cat wordpress/.default_theme); \
cd wordpress/wp-content/themes && \
rm -r $(ls | grep -v $KEEP_THEME)

# Remove the akismet plugin as it's not likely Playground sites will
# get many spammy comments :-)
Expand All @@ -54,24 +37,11 @@ RUN cp -r wordpress wordpress-static && \
# Remove all empty directories
find . -type d -empty -delete

# Remove the sqlite database integration from the static files
RUN rm -rf wordpress-static/wp-content/mu-plugins

# Move the static files to the final output directory
RUN mkdir /root/output/$OUT_FILENAME
RUN mv wordpress-static/* /root/output/$OUT_FILENAME/

# === Minify WordPress ===

# Remove non-default themes

RUN cat wordpress/wp-includes/default-constants.php | awk -F"'" "/define\( 'WP_DEFAULT_THEME'/"'{print $4}' > wordpress/.default_theme

RUN export KEEP_THEME=$(cat wordpress/.default_theme); \
cd wordpress/wp-content/themes && \
rm -r $(ls | grep -v $KEEP_THEME)

# Remove unused static files
# Remove static files not used by PHP
RUN cd wordpress && \
find ./ -type f \( \
-name '*.eot' -o -name '*.gif' -o -name '*.htaccess' \
Expand Down Expand Up @@ -118,22 +88,6 @@ RUN cd wordpress && \
FROM php:8.0-cli AS php
WORKDIR /root/
COPY --from=emscripten /root/wordpress ./wordpress
RUN apt update && apt install unzip

# === Run WordPress Installer ===
RUN ls -la && \
cd wordpress && \
nohup bash -c "php -S 127.0.0.1:8000&" && \
sleep 6; \
http_response=$(curl -o ./debug.txt -s -w "%{http_code}\n" -XPOST http://127.0.0.1:8000/wp-admin/install.php\?step\=2 --data "language=en&prefix=wp_&weblog_title=My WordPress Website&user_name=admin&admin_password=password&admin_password2=password&Submit=Install WordPress&pw_weak=1&admin_email=admin@localhost.com") && \
if [ "$http_response" != "200" ]; then \
cat debug.txt && \
exit 'WordPress installation failed'; \
fi

# === Install WP-CLI ===
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
chmod +x wp-cli.phar

# Strip whitespaces from PHP files.
# PHP >= 8.0 is needed to preserve PHP 8.0 attributes,
Expand Down Expand Up @@ -161,14 +115,12 @@ FROM emscripten as emscripten2
RUN rm -rf ./wordpress
COPY --from=php /root/wordpress ./wordpress

# === Postprocess WordPress ===

# Disable load-scripts.php
# Disable load-scripts.php in the minified build
RUN cd wordpress && \
sed "s/<?php/<?php define( 'CONCATENATE_SCRIPTS', false );/" wp-config.php > wp-config.php.new && \
mv wp-config.php.new wp-config.php
sed "s/<?php/<?php define( 'CONCATENATE_SCRIPTS', false );/" wp-config-sample.php > wp-config.php

# Build the final wp.zip file
RUN mv wordpress /wordpress && \
cd /wordpress && \
zip /root/output/$OUT_FILENAME.zip -r .

52 changes: 37 additions & 15 deletions packages/playground/wordpress-builds/build/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import path from 'path';
import path, { join } from 'path';
import { spawn } from 'child_process';
import yargs from 'yargs';
import { promises as fs, statSync } from 'fs';
Expand Down Expand Up @@ -106,20 +106,42 @@ if (!args.force && versionInfo.slug !== 'nightly' && versions[versionInfo.slug]
}

// Build WordPress
await asyncSpawn(
'docker',
[
'build',
'.',
'--progress=plain',
'--tag=wordpress-playground',
'--build-arg',
`WP_ZIP_URL=${versionInfo.url}`,
'--build-arg',
`OUT_FILENAME=wp-${versionInfo.slug}`,
],
{ cwd: sourceDir, stdio: 'inherit' }
);
const wordpressDir = join(sourceDir, 'wordpress');
try {
try {
await fs.rm(wordpressDir, { recursive: true });
} catch (e) {
// Ignore
}
await fs.mkdir(wordpressDir);
// Install WordPress in a local directory
await asyncSpawn(
'bun',
[
'../../cli/src/cli.ts',
'run-blueprint',
`--wp=${versionInfo.url}`,
`--mount-before-install=${wordpressDir}:/wordpress`
],
{ cwd: sourceDir, stdio: 'inherit' }
);

// Minify that WordPress
await asyncSpawn(
'docker',
[
'build',
'.',
'--progress=plain',
'--tag=wordpress-playground',
'--build-arg',
`OUT_FILENAME=wp-${versionInfo.slug}`,
],
{ cwd: sourceDir, stdio: 'inherit' }
);
} finally {
await fs.rm(wordpressDir, { recursive: true });
}

// Extract the WordPress static root with wp-includes/ etc
await asyncSpawn(
Expand Down

0 comments on commit 05c57e0

Please sign in to comment.