Skip to content

Commit bcde35e

Browse files
authored
Speed up setup of WordPress CI jobs (#620)
## Summary Each WordPress CI job spends 2 to 3 minutes preparing WordPress before its first test. This PR cuts that to **about 30 seconds in PHPUnit jobs** and **about 80 seconds in E2E jobs**. The same tests run, with the same results. The PR makes three changes: - **Cache the prepared WordPress folder.** Installing about 2,600 npm packages and building WordPress takes about 100 seconds, and the result only changes when the WordPress 7.1 branch gets a new commit or when our setup changes. All four WordPress jobs now restore it from the GitHub Actions cache instead. - **Run PHPUnit with only the PHP container.** The PHPUnit tests use SQLite, so they don't need MySQL, nginx, WP-CLI, an installed site, or the Gutenberg and Query Monitor plugins. The only thing they used from the installation step was `wp-tests-config.php`, and `wp-setup.sh` now creates the same file. - **Remove the "Stop Docker containers" steps.** GitHub discards the runner after each job, so this cleanup only added about 12 seconds. Locally, `composer run wp-test-php` and the plugin PHPUnit commands now also start only the PHP container. The E2E commands start the full environment as before. ## How the cache works The cache key combines the Node.js version, the latest commit of the WordPress branch, and a hash of `wp-setup.sh` and the `db.php` source file. When any of them changes, the job sets up WordPress from scratch like today. The cache is saved right after setup, before any tests run, so tests can't leave files in it. - **Size:** The cache is **262 MB**, and restoring it takes 6 to 7 seconds. - **Cache misses** happen once for each new commit on the WordPress branch, which comes every few days to weeks, and when GitHub updates Node.js on its runners. In PHPUnit jobs, a miss takes about as long as today's setup. In E2E jobs, it can take about 30 seconds longer, because it also pulls the PHP image, installs Composer packages, and saves the cache. - **Composer packages are cached too.** With the cache, PHPUnit jobs don't run `composer update` at all, and E2E jobs don't need to download any package files. This makes failures like a GitHub 504 during `composer update` less likely. ## Measured times GitHub-hosted runners vary a lot in speed, so each job is compared with its own earlier runs. The baseline is 9 to 10 successful runs of each job from 6 November, on trunk and other PRs. "Setup" is the time from the start of the job to its first test. The table shows medians, with the range in parentheses. | Job | Setup before | Setup with cache | Whole job before | Whole job with cache | | --- | ---: | ---: | ---: | ---: | | WordPress PHPUnit | 149s (121–171s) | **32s** (32–32s) | see below | see below | | SQLite plugin PHPUnit | 154s (114–233s) | **29s** (27–34s) | 188s | **50s** | | End-to-end | 148s (137–176s) | **78s** (69–86s) | 168s | **86s** | | WordPress end-to-end | 153s (131–221s) | **82s** (77–85s) | 252s | **166s** | The WordPress PHPUnit tests themselves took between 2.5 and 5 minutes in these runs, depending on the runner, so its whole-job time is too noisy to compare. Everything else in that job went from **163s to 33s** (median, 134–184s before, 33–34s with cache). With the cache, there were 3 to 4 runs of each job, from before and after rebasing on #619. That PR changes how long the WordPress PHPUnit tests take, but not the setup. With a cache miss, setup took 114–156s in the PHPUnit jobs and 145–187s in the E2E jobs. In the E2E jobs, most of the remaining setup is pulling Docker images (about 45 seconds) and waiting for MySQL to start (about 10 seconds). <details> <summary>Runs of this PR</summary> After rebasing on #619: | Job | Cache miss | With cache | | --- | --- | --- | | WordPress PHPUnit | [attempt 1](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897287/attempts/1) | [attempt 2](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897287/attempts/2), [attempt 3](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897287/attempts/3) | | SQLite plugin PHPUnit | [attempt 1](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897274/attempts/1) | [attempt 2](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897274/attempts/2), [attempt 3](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897274/attempts/3) | | End-to-end | [attempt 1](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897204/attempts/1) | [attempt 2](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897204/attempts/2), [attempt 3](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897204/attempts/3) | | WordPress end-to-end | [attempt 1](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897291/attempts/1) | [attempt 2](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897291/attempts/2), [attempt 3](https://github.com/WordPress/sqlite-database-integration/actions/runs/42498897291/attempts/3) | Before rebasing on #619: | Job | Cache miss | With cache | | --- | --- | --- | | WordPress PHPUnit | [attempt 1](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348220/attempts/1) | [attempt 2](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348220/attempts/2) | | SQLite plugin PHPUnit | [attempt 1](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348170/attempts/1) | [attempt 2](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348170/attempts/2), [attempt 3](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348170/attempts/3) | | End-to-end | [attempt 1](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348338/attempts/1) | [attempt 2](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348338/attempts/2), [attempt 3](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348338/attempts/3) | | WordPress end-to-end | [attempt 1](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348294/attempts/1) | [attempt 2](https://github.com/WordPress/sqlite-database-integration/actions/runs/42251348294/attempts/2) | </details> ## Same test results Every run of this PR gives the same results as the baseline: - **WordPress PHPUnit** reports the same counts as other PR runs in all 5 runs: `Tests: 30900, Assertions: 4559579, Errors: 1, Failures: 34, Warnings: 86, Skipped: 42`. The lists of skipped tests, warnings, errors, and failures are identical, and the expected failures check passes. Trunk pushes skip 17 fewer tests, because WordPress runs those only on trunk. - **SQLite plugin PHPUnit** runs the same tests with the same number of assertions, in both single-site and multisite. - **Both E2E jobs** pass the same tests as before. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Chores** * WordPress setup is cached and shared across end-to-end and PHPUnit test workflows, reducing repeated preparation. * PHP test environments now verify required configuration and tools before starting services. * WordPress test configuration now uses consistent database credentials and filesystem settings, helping provide a more predictable test environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent c3dde4f commit bcde35e

7 files changed

Lines changed: 61 additions & 21 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Prepare WordPress
2+
description: Set up the WordPress repository.
3+
runs:
4+
using: composite
5+
steps:
6+
- name: Resolve WordPress cache key
7+
id: wordpress
8+
shell: bash
9+
env:
10+
# The setup script builds the "wordpress" folder and copies "db.copy" into it.
11+
SETUP_HASH: ${{ hashFiles( 'wp-setup.sh', 'packages/plugin-sqlite-database-integration/db.copy' ) }}
12+
run: |
13+
version="$( sed -n 's/^WP_VERSION="\(.*\)"$/\1/p' wp-setup.sh )"
14+
commit="$( git ls-remote --exit-code https://github.com/WordPress/wordpress-develop.git "refs/heads/$version" | cut -f1 )"
15+
echo "cache-key=wordpress-$( node --version )-$commit-$SETUP_HASH" >> "$GITHUB_OUTPUT"
16+
17+
- name: Restore WordPress from the cache
18+
id: cache
19+
uses: actions/cache/restore@v6
20+
with:
21+
path: wordpress
22+
key: ${{ steps.wordpress.outputs.cache-key }}
23+
24+
- name: Set up WordPress
25+
if: steps.cache.outputs.cache-hit != 'true'
26+
shell: bash
27+
run: composer run wp-test-ensure-php-env
28+
29+
# Save now rather than using "actions/cache", which saves at the end of the job.
30+
# Saving after the tests would also cache files they create, like the SQLite database.
31+
- name: Save WordPress to the cache
32+
if: steps.cache.outputs.cache-hit != 'true'
33+
uses: actions/cache/save@v6
34+
with:
35+
path: wordpress
36+
key: ${{ steps.wordpress.outputs.cache-key }}

‎.github/workflows/end-to-end-tests.yml‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ jobs:
2828
echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV
2929
3030
- name: Prepare WordPress
31-
run: composer run wp-setup
31+
uses: ./.github/actions/wordpress
3232

3333
- name: Run end-to-end tests
3434
uses: ./.github/actions/playwright
3535
with:
3636
test-script: test-e2e
37-
38-
- name: Stop Docker containers
39-
if: always()
40-
run: composer run wp-test-clean

‎.github/workflows/sqlite-plugin-phpunit-tests.yml‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ jobs:
3131
echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV
3232
echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV
3333
34+
- name: Prepare WordPress
35+
uses: ./.github/actions/wordpress
36+
3437
- name: Run SQLite plugin single-site PHPUnit tests
3538
run: composer run wp-test-sqlite-plugin-php
3639

3740
- name: Run SQLite plugin multisite PHPUnit tests
3841
run: composer run wp-test-sqlite-plugin-php-multisite
39-
40-
- name: Stop Docker containers
41-
if: always()
42-
run: composer run wp-test-clean

‎.github/workflows/wp-tests-end-to-end.yml‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ jobs:
2828
echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV
2929
3030
- name: Prepare WordPress
31-
run: composer run wp-setup
31+
uses: ./.github/actions/wordpress
3232

3333
- name: Run WordPress end-to-end tests
3434
uses: ./.github/actions/playwright
3535
with:
3636
test-script: wp-test-e2e
37-
38-
- name: Stop Docker containers
39-
if: always()
40-
run: composer run wp-test-clean

‎.github/workflows/wp-tests-phpunit.yml‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ jobs:
2727
echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV
2828
echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV
2929
30+
- name: Prepare WordPress
31+
uses: ./.github/actions/wordpress
32+
3033
- name: Run WordPress PHPUnit tests
3134
run: node .github/workflows/wp-tests-phpunit-run.js
32-
33-
- name: Stop Docker containers
34-
if: always()
35-
run: composer run wp-test-clean

‎composer.json‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@
5959
"npm --prefix wordpress run env:cli -- plugin install gutenberg --version=22.3.0",
6060
"npm --prefix wordpress run env:cli -- plugin install query-monitor"
6161
],
62-
"wp-test-ensure-env": [
63-
"if [ ! -f wordpress/src/wp-load.php ]; then composer run wp-setup; fi",
62+
"wp-test-ensure-php-env": [
63+
"if [ ! -f wordpress/wp-tests-config.php ]; then composer run wp-setup; fi",
6464
"@putenv COMPOSE_IGNORE_ORPHANS=true",
65+
"if [ ! -f wordpress/vendor/autoload.php ]; then npm --prefix wordpress run env:composer -- update -W; fi"
66+
],
67+
"wp-test-ensure-env": [
68+
"@wp-test-ensure-php-env",
6569
"cd wordpress && if [ -z \"$(DOTENV_CONFIG_QUIET=true node tools/local-env/scripts/docker.js ps -q)\" ]; then cd ..; composer run wp-test-start; fi"
6670
],
6771
"wp-test-php": [
68-
"@wp-test-ensure-env @no_additional_args",
72+
"@wp-test-ensure-php-env @no_additional_args",
6973
"@wp-test-remove-database @no_additional_args",
7074
"cd wordpress && node tools/local-env/scripts/docker.js run --rm php php -d zend.enable_gc=0 -d opcache.enable_cli=1 -d opcache.memory_consumption=256 -d opcache.jit=tracing -d opcache.jit_buffer_size=64M ./vendor/bin/phpunit @additional_args"
7175
],

‎wp-setup.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,13 @@ cp "$WP_DIR/.env.example" "$WP_DIR/.env"
8888
echo "Installing dependencies..."
8989
npm --prefix "$WP_DIR" install
9090
npm --prefix "$WP_DIR" run build:dev
91+
92+
# 10. Create "wp-tests-config.php" like "tools/local-env/scripts/install.js" does, so PHPUnit doesn't need "env:install".
93+
echo "Creating the 'wp-tests-config.php' file..."
94+
sed \
95+
-e "s/youremptytestdbnamehere/wordpress_develop_tests/" \
96+
-e "s/yourusernamehere/root/" \
97+
-e "s/yourpasswordhere/password/" \
98+
-e "s/localhost/mysql/" \
99+
"$WP_DIR/wp-tests-config-sample.php" > "$WP_DIR/wp-tests-config.php"
100+
printf "\ndefine( 'FS_METHOD', 'direct' );\n" >> "$WP_DIR/wp-tests-config.php"

0 commit comments

Comments
 (0)