From d379fb8ae095e1da5c741a88330c4b11f2298bce Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 8 Sep 2024 14:22:11 +0200 Subject: [PATCH 01/12] Merge all testing tools into one Composer command --- composer.json | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 4f37687..917d910 100644 --- a/composer.json +++ b/composer.json @@ -39,18 +39,20 @@ "symfony/finder": "^5.4" }, "scripts": { - "lint": [ + "test": [ + "@test:lint", + "@test:phpunit", + "@test:phpstan" + ], + "test:lint": [ "composer validate", "phpcs" ], - "analysis": [ - "@analysis:phpstan" + "test:phpunit": [ + "./vendor/bin/phpunit" ], - "analysis:phpstan": [ + "test:phpstan": [ "./vendor/bin/phpstan --memory-limit=2G" - ], - "test": [ - "phpunit" ] } } From cce478494228e643c5df5b4a35cd301ea3773048 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 8 Sep 2024 14:41:28 +0200 Subject: [PATCH 02/12] Change main GitHub workflow to use internal Docker image as host --- .github/workflows/main.yml | 60 ++++++++------------------------------ 1 file changed, 12 insertions(+), 48 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 12da66a..ec650a5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,60 +9,24 @@ on: - master jobs: - tests: + linting: runs-on: ubuntu-latest - strategy: - matrix: - php: ['8.0', '8.1', '8.2', '8.3'] - name: PHP ${{ matrix.php }} tests + name: Linting steps: - uses: actions/checkout@v3 - - name: Install PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - - name: Debugging - run: | - php --version - php -m - composer --version - - name: Install dependencies - run: composer install --prefer-dist --no-suggest --no-progress - - name: Run tests - run: vendor/bin/phpunit --printer mheap\\GithubActionsReporter\\Printer - analysis: + - name: Run linting + run: docker compose run --rm builder composer test:lint + testing: runs-on: ubuntu-latest - name: Analysis of code + name: Testing steps: - uses: actions/checkout@v3 - - name: Install PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.1' - - name: Debugging - run: | - php --version - php -m - composer --version - - name: Install dependencies - run: composer install --prefer-dist --no-suggest --no-progress - - name: Run validate - run: composer run analysis - lint: + - name: Run testing + run: docker compose run --rm builder composer test:phpunit + analysis: runs-on: ubuntu-latest - name: Lint all project files + name: Analysis steps: - uses: actions/checkout@v3 - - name: Install PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.0' - - name: Debugging - run: | - php --version - php -m - composer --version - - name: Install dependencies - run: composer install --prefer-dist --no-suggest --no-progress - - name: Run validate - run: composer run lint + - name: Run analysis + run: docker compose run --rm builder composer test:analysis From 4cec041bd5c7d0afd8ab7fad1c075287ca59ba24 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 8 Sep 2024 14:45:12 +0200 Subject: [PATCH 03/12] Fix phpcs path in lint testing command --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 917d910..8a5740e 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ ], "test:lint": [ "composer validate", - "phpcs" + "./vendor/bin/phpcs" ], "test:phpunit": [ "./vendor/bin/phpunit" From cf1d3e03c80a6b46c73870c9e3fda6b614832854 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 8 Sep 2024 14:45:52 +0200 Subject: [PATCH 04/12] Fix phpstan command calling in GitHub main workflow --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ec650a5..318baa6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -29,4 +29,4 @@ jobs: steps: - uses: actions/checkout@v3 - name: Run analysis - run: docker compose run --rm builder composer test:analysis + run: docker compose run --rm builder composer test:phpstan From 8c279b4b30f7bec827639ea2443bf2383fcf4471 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 8 Sep 2024 14:48:46 +0200 Subject: [PATCH 05/12] Install Composer dependencies before each job runs --- .github/workflows/main.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 318baa6..c364b8c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,6 +14,8 @@ jobs: name: Linting steps: - uses: actions/checkout@v3 + - name: Setup dependencies + run: docker compose run --rm builder composer install - name: Run linting run: docker compose run --rm builder composer test:lint testing: @@ -21,6 +23,8 @@ jobs: name: Testing steps: - uses: actions/checkout@v3 + - name: Setup dependencies + run: docker compose run --rm builder composer install - name: Run testing run: docker compose run --rm builder composer test:phpunit analysis: @@ -28,5 +32,7 @@ jobs: name: Analysis steps: - uses: actions/checkout@v3 + - name: Setup dependencies + run: docker compose run --rm builder composer install - name: Run analysis run: docker compose run --rm builder composer test:phpstan From 1c1d81886e5abe66390fe8b0fc5643a311d546b9 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 8 Sep 2024 15:23:41 +0200 Subject: [PATCH 06/12] Setup shared cache for Composer dependencies --- .github/workflows/main.yml | 53 +++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c364b8c..0bb02a9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,4 @@ -name: Lint, analyse and test +name: Lint, test and analyse on: push: @@ -9,30 +9,63 @@ on: - master jobs: + install_dependencies: + runs-on: ubuntu-latest + name: Install dependencies + steps: + - uses: actions/checkout@v4 + - name: Install dependencies + run: docker compose run --rm builder composer install + - name: Cache installed dependencies + id: cache-composer-dependencies + uses: actions/cache/save@v4 + with: + path: | + ./vendor/ + ./composer.lock + key: ${{ runner.os }}-build-composer-dependencies-${{ github.sha }} linting: + needs: install_dependencies runs-on: ubuntu-latest name: Linting steps: - - uses: actions/checkout@v3 - - name: Setup dependencies - run: docker compose run --rm builder composer install + - uses: actions/checkout@v4 + - uses: actions/cache/restore@v4 + name: Restore cached dependencies + with: + path: | + ./vendor/ + ./composer.lock + key: ${{ runner.os }}-build-composer-dependencies-${{ github.sha }} - name: Run linting run: docker compose run --rm builder composer test:lint testing: + needs: install_dependencies runs-on: ubuntu-latest name: Testing steps: - - uses: actions/checkout@v3 - - name: Setup dependencies - run: docker compose run --rm builder composer install + - uses: actions/checkout@v4 + - uses: actions/cache/restore@v4 + name: Restore cached dependencies + with: + path: | + ./vendor/ + ./composer.lock + key: ${{ runner.os }}-build-composer-dependencies-${{ github.sha }} - name: Run testing run: docker compose run --rm builder composer test:phpunit analysis: + needs: install_dependencies runs-on: ubuntu-latest name: Analysis steps: - - uses: actions/checkout@v3 - - name: Setup dependencies - run: docker compose run --rm builder composer install + - uses: actions/checkout@v4 + - uses: actions/cache/restore@v4 + name: Restore cached dependencies + with: + path: | + ./vendor/ + ./composer.lock + key: ${{ runner.os }}-build-composer-dependencies-${{ github.sha }} - name: Run analysis run: docker compose run --rm builder composer test:phpstan From 22f62852591e60d7122b4898c56beaa13a9fde7d Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 8 Sep 2024 16:08:39 +0200 Subject: [PATCH 07/12] Run GitHub Actions in separate service without the volume --- .github/workflows/main.yml | 45 +++----------------------------------- docker-compose.yml | 11 ++++++++++ 2 files changed, 14 insertions(+), 42 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0bb02a9..6ad948c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,63 +9,24 @@ on: - master jobs: - install_dependencies: - runs-on: ubuntu-latest - name: Install dependencies - steps: - - uses: actions/checkout@v4 - - name: Install dependencies - run: docker compose run --rm builder composer install - - name: Cache installed dependencies - id: cache-composer-dependencies - uses: actions/cache/save@v4 - with: - path: | - ./vendor/ - ./composer.lock - key: ${{ runner.os }}-build-composer-dependencies-${{ github.sha }} linting: - needs: install_dependencies runs-on: ubuntu-latest name: Linting steps: - uses: actions/checkout@v4 - - uses: actions/cache/restore@v4 - name: Restore cached dependencies - with: - path: | - ./vendor/ - ./composer.lock - key: ${{ runner.os }}-build-composer-dependencies-${{ github.sha }} - name: Run linting - run: docker compose run --rm builder composer test:lint + run: docker compose run --rm actions-tester composer test:lint testing: - needs: install_dependencies runs-on: ubuntu-latest name: Testing steps: - uses: actions/checkout@v4 - - uses: actions/cache/restore@v4 - name: Restore cached dependencies - with: - path: | - ./vendor/ - ./composer.lock - key: ${{ runner.os }}-build-composer-dependencies-${{ github.sha }} - name: Run testing - run: docker compose run --rm builder composer test:phpunit + run: docker compose run --rm actions-tester composer test:phpunit analysis: - needs: install_dependencies runs-on: ubuntu-latest name: Analysis steps: - uses: actions/checkout@v4 - - uses: actions/cache/restore@v4 - name: Restore cached dependencies - with: - path: | - ./vendor/ - ./composer.lock - key: ${{ runner.os }}-build-composer-dependencies-${{ github.sha }} - name: Run analysis - run: docker compose run --rm builder composer test:phpstan + run: docker compose run --rm actions-tester composer test:phpstan diff --git a/docker-compose.yml b/docker-compose.yml index 1def952..8835bc8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,3 +8,14 @@ services: volumes: - .:/mozart/ command: /bin/sh -c "while sleep 1000; do :; done" + actions-tester: + # This service is technically the same as the builder service, only this one + # has no volume mounted to the working directory. This causes the files to + # be used (for example installed dependencies) that are created _inside_ the + # container to be used, rather than having to set them up in the working + # directory, during GitHub Actions for example. + build: + context: . + dockerfile: Dockerfile + target: builder + command: /bin/sh -c "while sleep 1000; do :; done" From 97659813e7f4b8fde1c6d97a4d3c469ebc398c46 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 8 Sep 2024 16:15:08 +0200 Subject: [PATCH 08/12] Explicitely run the test and lint commands with their configuration file parameters --- Dockerfile | 2 +- composer.json | 6 +++--- phpcs.xml => phpcs.xml.dist | 0 phpunit.xml => phpunit.xml.dist | 0 4 files changed, 4 insertions(+), 4 deletions(-) rename phpcs.xml => phpcs.xml.dist (100%) rename phpunit.xml => phpunit.xml.dist (100%) diff --git a/Dockerfile b/Dockerfile index c9073b9..1d8651f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ FROM php:8.3.9-cli-alpine AS base FROM base as builder RUN apk update && apk add git COPY --from=composer /usr/bin/composer /usr/bin/composer -COPY ./composer.json /mozart/ +COPY ./ /mozart/ WORKDIR /mozart/ RUN composer install diff --git a/composer.json b/composer.json index 8a5740e..f5b7421 100644 --- a/composer.json +++ b/composer.json @@ -46,13 +46,13 @@ ], "test:lint": [ "composer validate", - "./vendor/bin/phpcs" + "./vendor/bin/phpcs --standard=phpcs.xml.dist" ], "test:phpunit": [ - "./vendor/bin/phpunit" + "./vendor/bin/phpunit -c phpunit.xml.dist" ], "test:phpstan": [ - "./vendor/bin/phpstan --memory-limit=2G" + "./vendor/bin/phpstan analyse -c phpstan.neon.dist --memory-limit=2G" ] } } diff --git a/phpcs.xml b/phpcs.xml.dist similarity index 100% rename from phpcs.xml rename to phpcs.xml.dist diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 100% rename from phpunit.xml rename to phpunit.xml.dist From 359fff3ed46f4f5eeafc5ae6e8a774beb0919de1 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 8 Sep 2024 16:39:26 +0200 Subject: [PATCH 09/12] Explicit Docker build step before other jobs including caching --- .github/workflows/main.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6ad948c..e547ed1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,7 +9,21 @@ on: - master jobs: + docker_build: + runs-on: ubuntu-latest + steps: + - name: Setup Docker buildx + uses: docker/setup-buildx-action@v2 + - name: Build Docker image + id: build-and-push + uses: docker/build-push-action@v4 + with: + context: "{{defaultContext}}" + push: false + cache-from: type=gha + cache-to: type=gha,mode=max linting: + needs: docker_build runs-on: ubuntu-latest name: Linting steps: @@ -17,6 +31,7 @@ jobs: - name: Run linting run: docker compose run --rm actions-tester composer test:lint testing: + needs: docker_build runs-on: ubuntu-latest name: Testing steps: @@ -24,6 +39,7 @@ jobs: - name: Run testing run: docker compose run --rm actions-tester composer test:phpunit analysis: + needs: docker_build runs-on: ubuntu-latest name: Analysis steps: From 7388798270205cc000b450847181aae50c1df411 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Mon, 9 Sep 2024 08:56:53 +0200 Subject: [PATCH 10/12] Have each job have its own build step using cache layer --- .github/workflows/main.yml | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e547ed1..f5583e8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,11 +9,13 @@ on: - master jobs: - docker_build: + linting: runs-on: ubuntu-latest + name: Linting steps: - name: Setup Docker buildx uses: docker/setup-buildx-action@v2 + - uses: actions/checkout@v4 - name: Build Docker image id: build-and-push uses: docker/build-push-action@v4 @@ -22,27 +24,39 @@ jobs: push: false cache-from: type=gha cache-to: type=gha,mode=max - linting: - needs: docker_build - runs-on: ubuntu-latest - name: Linting - steps: - - uses: actions/checkout@v4 - name: Run linting run: docker compose run --rm actions-tester composer test:lint testing: - needs: docker_build runs-on: ubuntu-latest name: Testing steps: + - name: Setup Docker buildx + uses: docker/setup-buildx-action@v2 - uses: actions/checkout@v4 + - name: Build Docker image + id: build-and-push + uses: docker/build-push-action@v4 + with: + context: "{{defaultContext}}" + push: false + cache-from: type=gha + cache-to: type=gha,mode=max - name: Run testing run: docker compose run --rm actions-tester composer test:phpunit analysis: - needs: docker_build runs-on: ubuntu-latest name: Analysis steps: + - name: Setup Docker buildx + uses: docker/setup-buildx-action@v2 - uses: actions/checkout@v4 + - name: Build Docker image + id: build-and-push + uses: docker/build-push-action@v4 + with: + context: "{{defaultContext}}" + push: false + cache-from: type=gha + cache-to: type=gha,mode=max - name: Run analysis run: docker compose run --rm actions-tester composer test:phpstan From 640ed30b27b796e0553ee62aaf423bc1a0ce34aa Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Mon, 9 Sep 2024 09:15:23 +0200 Subject: [PATCH 11/12] Use same cache backend in Docker tagging workflows --- .github/workflows/docker-latest.yml | 12 ++---------- .github/workflows/release.yml | 12 ++---------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/.github/workflows/docker-latest.yml b/.github/workflows/docker-latest.yml index f0e5b77..3d6f39f 100644 --- a/.github/workflows/docker-latest.yml +++ b/.github/workflows/docker-latest.yml @@ -23,14 +23,6 @@ jobs: id: buildx uses: docker/setup-buildx-action@v1 - - name: Cache Docker layers - uses: actions/cache@v2 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-buildx- - - name: Login to DockerHub uses: docker/login-action@v1 with: @@ -48,5 +40,5 @@ jobs: platforms: linux/amd64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.prep.outputs.tags }} - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,dest=/tmp/.buildx-cache + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dedc4f4..aaa5cdc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -62,14 +62,6 @@ jobs: id: buildx uses: docker/setup-buildx-action@v1 - - name: Cache Docker layers - uses: actions/cache@v2 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-buildx- - - name: Login to DockerHub uses: docker/login-action@v1 with: @@ -87,8 +79,8 @@ jobs: platforms: linux/amd64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.prep.outputs.tags }} - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,dest=/tmp/.buildx-cache + cache-from: type=gha + cache-to: type=gha,mode=max - name: Image digest run: echo ${{ steps.docker_build.outputs.digest }} From 31f3c5efe13ebce276304c1cb3151cda08b6f2b3 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Mon, 9 Sep 2024 09:35:43 +0200 Subject: [PATCH 12/12] Enforce PHP 8.0 (and up) compatibility through PHPCS --- composer.json | 7 +++++-- phpcs.xml.dist | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index f5b7421..eda7461 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ }, "config": { "allow-plugins": { - "phpstan/extension-installer": true + "phpstan/extension-installer": true, + "dealerdirect/phpcodesniffer-composer-installer": true }, "platform": { "php": "8.0" @@ -36,7 +37,9 @@ "phpstan/extension-installer": "^1.4", "league/flysystem": "^2.5", "symfony/console": "^5.4", - "symfony/finder": "^5.4" + "symfony/finder": "^5.4", + "dealerdirect/phpcodesniffer-composer-installer": "^1.0", + "phpcompatibility/php-compatibility": "dev-develop" }, "scripts": { "test": [ diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 2bf4e6c..27d5af4 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -3,5 +3,9 @@ Coding standard ruleset based on the PSR-2 coding standard. + + + + ./src - \ No newline at end of file +