From 6ecd93a67eb87b019356fac43c4c39398c427ff5 Mon Sep 17 00:00:00 2001 From: Chloe Sartorelli Date: Mon, 16 Nov 2026 17:49:31 +1300 Subject: [PATCH] ENH Improve DB service handling and ensure composer cache gets used - Only set up DB that is used and run healthcheck in parallel - Skip DB setup for jobs that don't use it - Use rolling cache key for composer to avoid stale cache The cache doesn't get updated if the key already exists, so it needs to be updated if composer.json changes, plus periodically so new versions of packages don't need to ge re-downloaded. --- .github/workflows/ci.yml | 168 +++++++++++++++++++++++++++------------ 1 file changed, 116 insertions(+), 52 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b9a97de..a0f795a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -132,55 +132,10 @@ jobs: runs-on: ubuntu-latest - services: - # It takes around 9 seconds per extra database added, so this is something that could be optimised later - # to only add the database that's required for the job - mysql57: - image: mysql:5.7 - env: - MYSQL_HOST: 127.0.0.1 - MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: SS_mysite - ports: - - 3357:3306 - options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=10 - mysql80: - image: mysql:8.0 - env: - MYSQL_HOST: 127.0.0.1 - MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: SS_mysite - ports: - - 3380:3306 - options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=10 - mysql84: - image: mysql:8.4 - env: - MYSQL_HOST: 127.0.0.1 - MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: SS_mysite - ports: - - 3384:3306 - options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=10 - postgres: - image: postgres - env: - POSTGRES_PASSWORD: postgres - options: --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=10 - ports: - - 5432:5432 - mariadb: - image: mariadb:11.4 - env: - MARIADB_HOST: 127.0.0.1 - MARIADB_ROOT_PASSWORD: root - MARIADB_DATABASE: SS_mysite - ports: - - 3311:3306 - options: --health-cmd="healthcheck.sh --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=10 - env: artifacts_name: ${{ matrix.name }} + # Only jobs that run PHP tests need a database. Linting and JS jobs do not. + uses_database: ${{ matrix.needs_full_setup != 'false' && (matrix.phpunit == 'true' || matrix.endtoend == 'true' || matrix.phpcoverage == 'true') }} name: ${{ matrix.name }} @@ -195,6 +150,73 @@ jobs: - name: Checkout code uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1 + # Start only the database this job uses. The image downloads in this step, then the database + # initialises while other steps run. The "Wait for database" step waits until it is ready. + - name: Start database + if: ${{ env.uses_database == 'true' }} + env: + MATRIX_DB: ${{ matrix.db }} + run: | + MYSQL_ENV=(-e MYSQL_HOST=127.0.0.1 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=SS_mysite) + # Use TCP so the check fails while the image's init script runs its socket-only temporary server + MYSQL_HEALTH="mysqladmin ping --protocol=tcp --host=127.0.0.1" + case "$MATRIX_DB" in + mysql57|mysql57pdo) + IMAGE="mysql:5.7" + PORTS="3357:3306" + DB_ENV=("${MYSQL_ENV[@]}") + HEALTH="$MYSQL_HEALTH" + ;; + mysql80) + IMAGE="mysql:8.0" + PORTS="3380:3306" + DB_ENV=("${MYSQL_ENV[@]}") + HEALTH="$MYSQL_HEALTH" + ;; + mysql84) + IMAGE="mysql:8.4" + PORTS="3384:3306" + DB_ENV=("${MYSQL_ENV[@]}") + HEALTH="$MYSQL_HEALTH" + ;; + pgsql) + IMAGE="postgres" + PORTS="5432:5432" + DB_ENV=(-e POSTGRES_PASSWORD=postgres) + HEALTH="pg_isready --host=127.0.0.1" + ;; + mariadb) + IMAGE="mariadb:11.4" + PORTS="3311:3306" + DB_ENV=(-e MARIADB_HOST=127.0.0.1 -e MARIADB_ROOT_PASSWORD=root -e MARIADB_DATABASE=SS_mysite) + HEALTH="healthcheck.sh --connect --innodb_initialized" + ;; + sqlite3|"") + echo "No database container is needed" + exit 0 + ;; + *) + echo "Unknown database '$MATRIX_DB'" + exit 1 + ;; + esac + for i in {1..3}; do + if docker pull "$IMAGE"; then + break + fi + if [[ $i == 3 ]]; then + echo "Could not pull $IMAGE" + exit 1 + fi + echo "Could not pull $IMAGE, trying again" + sleep 10 + done + # Check health often while the database starts, then less often while the tests run + docker run -d --name database "${DB_ENV[@]}" -p "$PORTS" \ + --health-cmd="$HEALTH" --health-interval=10s --health-timeout=5s \ + --health-start-period=180s --health-start-interval=2s \ + "$IMAGE" + - name: Install PHP # SHA will need to be updated to support new php version when they are released uses: shivammathur/setup-php@c541c155eee45413f5b09a52248675b1a2575231 # v2.31.1 @@ -457,14 +479,28 @@ jobs: sudo systemctl restart apache2 echo "Apache has been configured" - # This is shared between runs, not just jobs. It means the first time the repo runs the job it'll - # need to download requirements for the first time, after that it will be plenty quick + # A cache entry is never overwritten, so the key changes each week to keep the cache current. + # Do not use the "shared-composer-cache" key. gha-generate-matrix saves a small cache with that key. + - name: Get composer cache key + id: composer-cache-key + env: + MATRIX_PHP: ${{ matrix.php }} + PREFER: ${{ contains(matrix.composer_args, '--prefer-lowest') && 'lowest' || 'highest' }} + COMPOSER_JSON_HASH: ${{ hashFiles('composer.json') }} + run: | + PREFIX="composer-php$MATRIX_PHP-$PREFER" + echo "prefix=$PREFIX" >> $GITHUB_OUTPUT + echo "key=$PREFIX-$COMPOSER_JSON_HASH-$(date -u +%G-%V)" >> $GITHUB_OUTPUT + # https://docs.github.com/en/actions/advanced-guides/caching-dependencies-to-speed-up-workflows - name: Enable shared composer cache uses: actions/cache@0c907a75c2c80ebcb7f088228285e798b750cf8f # @v4.2.1 with: path: ~/.cache/composer - key: shared-composer-cache + key: ${{ steps.composer-cache-key.outputs.key }} + restore-keys: | + ${{ steps.composer-cache-key.outputs.prefix }}- + composer-php # Update composer.json and install dependencies such as Databases binaries to run the tests # Note that SQLite3 doesn't need to be installed because it is bundle in the GitHub docker image. @@ -801,18 +837,43 @@ jobs: composer dumpautoload -o fi + - name: Wait for database + if: ${{ env.uses_database == 'true' }} + run: | + if [[ -z $(docker ps -aq --filter name=^database$) ]]; then + echo "No database container to wait for" + exit 0 + fi + # The status stays "starting" during the health start period. After that period, a few failed checks + # make it "unhealthy", but the database can still become healthy. Stop early only if it is not running. + for i in {1..90}; do + STATUS=$(docker inspect --format '{{.State.Health.Status}}' database) + if [[ $STATUS == "healthy" ]]; then + echo "Database is ready" + exit 0 + fi + if [[ $(docker inspect --format '{{.State.Running}}' database) != "true" ]]; then + STATUS="stopped" + break + fi + sleep 2 + done + echo "Database did not become ready, status is '$STATUS'" + docker logs database + exit 1 + - name: Final preparation env: GITHUB_WORKSPACE: ${{ github.workspace }} GITHUB_REPOSITORY: ${{ github.repository }} - NEEDS_FULL_SETUP: ${{ matrix.needs_full_setup }} + USES_DATABASE: ${{ env.uses_database }} run: | # Artifacts directory must be created after composer install as it would remove the artifacts directory # This seems a bit strange, if you need to add an artifact at an earlier stage then revalidate that # composer install does actually remove the artifact directory mkdir artifacts - if [[ $NEEDS_FULL_SETUP == 'false' ]]; then + if [[ $USES_DATABASE != 'true' ]]; then echo "skipping .env, database, and other full site setup steps" else # Add .env file and create artifacts directory @@ -983,6 +1044,9 @@ jobs: if [[ -f $GITHUB_WORKSPACE/silverstripe.log ]]; then cp $GITHUB_WORKSPACE/silverstripe.log artifacts/ fi + if [[ -n $(docker ps -aq --filter name=^database$) ]]; then + docker logs database > artifacts/database.log 2>&1 + fi # https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts - name: Upload artifacts