From 1f70144e71981ba8b267ff8f40ea78f8b2cf6010 Mon Sep 17 00:00:00 2001 From: Adam Daley Date: Tue, 7 Jul 2026 06:34:37 +0100 Subject: [PATCH] Speed up CI: share Docker test image, dedupe PHPStan, fix cache contention (#3933) * Speed up CI: share Docker test image, dedupe PHPStan, fix cache contention live-tests and cypress-tests each independently rebuilt the same PHP 8.5 test image already built by the php-build-test matrix, paying for a registry-cache pull each time. Build it once, export it as a workflow artifact, and have live-tests/cypress-tests just docker load it. PHPStan analyzes source, not runtime behavior, so running it identically across all 3 PHP versions in the matrix was pure duplication. Now it runs once, on PHP 8.3. php-build-test-push and preview-build-push wrote to the shared GHCR registry cache on every push to every branch, not just main. Concurrent branch pushes (including frequent bot autofix branches) could stomp on each other's cache entries, degrading hit rates for everyone. Cache writes are now restricted to main; other branches read-only, same as the existing PR path. * Trim comment verbosity to match existing workflow style The repo's existing workflow comments are single-line "why" notes; tighten the multi-line ones added in the previous commit to match, and drop an unused step id. --- .github/workflows/ci.yml | 6 +++-- .github/workflows/cypress-tests.yml | 29 +++++++--------------- .github/workflows/live-tests.yml | 36 ++++++++++------------------ .github/workflows/php-build-test.yml | 15 ++++++++++++ 4 files changed, 40 insertions(+), 46 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4553d7fce..8e12628b1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -99,7 +99,8 @@ jobs: if: ${{ github.event_name == 'push' }} uses: ./.github/workflows/php-build-test.yml with: - write-cache: true + # Only main writes the shared registry cache, so concurrent branch pushes don't stomp on it. + write-cache: ${{ github.ref == 'refs/heads/main' }} live-tests-pr: name: 'Live Tests' @@ -160,7 +161,8 @@ jobs: if: ${{ github.event_name == 'push' && github.ref != 'refs/heads/main' }} uses: ./.github/workflows/preview-build.yml with: - write-cache: true + # This job only runs for non-main branches; main's preview-docker job keeps the cache warm. + write-cache: false preview-docker: name: 'Build and Push Preview Docker Image' diff --git a/.github/workflows/cypress-tests.yml b/.github/workflows/cypress-tests.yml index b0407350c..02bbc945c 100644 --- a/.github/workflows/cypress-tests.yml +++ b/.github/workflows/cypress-tests.yml @@ -24,35 +24,24 @@ jobs: with: persist-credentials: false - - name: 'Lowercase github.repository' - run: | - echo "IMAGE_NAME=${GITHUB_REPOSITORY@L}" >> "${GITHUB_ENV}" - - name: 'Set up Docker Compose' uses: docker/setup-compose-action@4eb059ff7f16592f9c84d5ca339c53cb7c5064e2 # v2.3.0 - - name: 'Set up Docker Buildx' - uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4 - - # This job runs checked-out Cypress code, so it only reads the registry cache. - - name: 'Build Docker Test Image' - uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7 + # Reuses the image already built once in the PHP Build and Test (8.5) job. + - name: 'Download Test Image Artifact' + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 with: - build-args: | - PHP_VERSION=8.5 - INSTALL_TRANSLATIONS=false - cache-from: type=registry,ref=ghcr.io/${{ env.IMAGE_NAME }}-build-cache:cache-ci-8.5 - context: . - load: true - tags: | - fossbilling:cypress-test - target: test + name: docker-test-image-8.5 + path: /tmp + + - name: 'Load Test Image' + run: docker load --input /tmp/test-image-8.5.tar - name: 'Run Cypress Tests' env: CYPRESS_PROJECT_ID: ${{ inputs.cypress-project-id }} CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} - run: bash ./.github/scripts/run-docker-cypress-tests.sh fossbilling:cypress-test + run: bash ./.github/scripts/run-docker-cypress-tests.sh fossbilling:test-8.5 - name: 'Upload Cypress Artifacts' if: ${{ failure() && env.CYPRESS_RECORDING_DISABLED == 'true' }} diff --git a/.github/workflows/live-tests.yml b/.github/workflows/live-tests.yml index 3b78bc269..b820e2f30 100644 --- a/.github/workflows/live-tests.yml +++ b/.github/workflows/live-tests.yml @@ -14,33 +14,23 @@ jobs: with: persist-credentials: false - - name: 'Set Registry Cache Image' - run: echo "CACHE_IMAGE=ghcr.io/${GITHUB_REPOSITORY@L}-build-cache" >> "${GITHUB_ENV}" - - name: 'Set up Docker Compose' uses: docker/setup-compose-action@4eb059ff7f16592f9c84d5ca339c53cb7c5064e2 # v2.3.0 - - name: 'Set up Docker Buildx' - uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4 - - # This job runs checked-out live-test code, so it only reads the registry cache. - - name: 'Build Docker Test Image' - id: build - uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7 + # Reuses the image already built once in the PHP Build and Test (8.5) job. + - name: 'Download Test Image Artifact' + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 with: - build-args: | - PHP_VERSION=8.5 - INSTALL_TRANSLATIONS=false - cache-from: type=registry,ref=${{ env.CACHE_IMAGE }}:cache-ci-8.5 - context: . - load: true - tags: | - fossbilling:live-test - target: test + name: docker-test-image-8.5 + path: /tmp + + - name: 'Load Test Image' + id: build + run: docker load --input /tmp/test-image-8.5.tar - name: 'Run Live Tests' id: live_tests - run: bash ./.github/scripts/run-docker-live-tests.sh fossbilling:live-test + run: bash ./.github/scripts/run-docker-live-tests.sh fossbilling:test-8.5 - name: 'Write Job Summary' if: always() @@ -50,11 +40,9 @@ jobs: | Item | Value | | --- | --- | - | Test image | `fossbilling:live-test` | + | Test image | `fossbilling:test-8.5` (shared artifact) | | PHP version | `8.5` | - | Registry cache | `${{ env.CACHE_IMAGE }}:cache-ci-8.5` | - | Cache mode | `read-only` | | Compose file | `.github/docker/live-tests.compose.yml` | - | Docker build | `${{ steps.build.outcome }}` | + | Image load | `${{ steps.build.outcome }}` | | Live tests | `${{ steps.live_tests.outcome }}` | EOF diff --git a/.github/workflows/php-build-test.yml b/.github/workflows/php-build-test.yml index 4d1ed0ebd..e657baf62 100644 --- a/.github/workflows/php-build-test.yml +++ b/.github/workflows/php-build-test.yml @@ -69,6 +69,8 @@ jobs: - name: 'PHPStan' id: phpstan + # PHPStan analyses source, not runtime behavior, so one PHP version is sufficient coverage. + if: matrix.php == '8.3' run: | docker run --rm \ --env APP_ENV=test \ @@ -83,6 +85,19 @@ jobs: fossbilling:test-${{ matrix.php }} \ ./src/vendor/bin/pest --test-directory ../tests --configuration phpunit.xml.dist --ci + - name: 'Export Test Image Artifact' + # Shared with live-tests/cypress-tests so they don't each rebuild the same image. + if: matrix.php == '8.5' + run: docker save --output /tmp/test-image-8.5.tar fossbilling:test-8.5 + + - name: 'Upload Test Image Artifact' + if: matrix.php == '8.5' + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: docker-test-image-8.5 + path: /tmp/test-image-8.5.tar + retention-days: 1 + - name: 'Write Job Summary' if: always() run: |