From 5e1158a9ee9d6d695ee0f9f7d4b993b9141eeab9 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Wed, 11 May 2022 15:03:54 +0200 Subject: [PATCH] fixup! ci: Auto-determine checks to run --- .github/workflows/build.yaml | 31 ++-- .github/workflows/test.yaml | 130 +++++++++++++++ .../workflows/workflow-from-flake-checks.nix | 151 ++++++++++++++++++ run-flake-checks.nix | 67 -------- 4 files changed, 290 insertions(+), 89 deletions(-) create mode 100644 .github/workflows/test.yaml create mode 100644 .github/workflows/workflow-from-flake-checks.nix delete mode 100644 run-flake-checks.nix diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 998e7c8a..24eb51a9 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -8,32 +8,19 @@ on: push: jobs: - build: - name: 'PHP ${{ matrix.php.branch }}' + prepare-ci: + name: 'Prepare actions' runs-on: ubuntu-20.04 - strategy: - matrix: - php: - - branch: '8.1' - - branch: '8.0' - - branch: '7.4' - - branch: '7.3' - - branch: '7.2' - - branch: '7.1' - - branch: '7.0' - - branch: '5.6' - # We want to fix failures individually. - fail-fast: false steps: - uses: actions/checkout@v2 - name: Install Nix uses: cachix/install-nix-action@v14 - - name: Set up Nix cache - uses: cachix/cachix-action@v10 - with: - name: fossar - authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' - - - run: nix-shell run-flake-checks.nix --argstr branch "${{ matrix.php.branch }}" + - name: Generate actions + run: nix-shell .github/workflows/workflow-from-flake-checks.nix + test: + needs: + - prepare-ci + uses: ./.github/workflows/test.yaml + secrets: inherit diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 00000000..ac2be719 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,130 @@ +{ + "jobs": { + "test": { + "name": "PHP ${{ matrix.php.branch }}", + "runs-on": "${{ matrix.os.image }}", + "steps": [ + { + "uses": "actions/checkout@v2" + }, + { + "name": "Install Nix", + "uses": "cachix/install-nix-action@v14" + }, + { + "name": "Set up Nix cache", + "uses": "cachix/cachix-action@v10", + "with": { + "authToken": "${{ secrets.CACHIX_AUTH_TOKEN }}", + "name": "fossar" + } + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\",\"php70\",\"php71\",\"php72\",\"php73\",\"php74\",\"php80\",\"php81\"]'), matrix.php.attr) }}", + "name": "Check that composer PHAR works", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-composer-phar" + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\",\"php70\",\"php71\",\"php72\",\"php73\",\"php74\",\"php80\",\"php81\"]'), matrix.php.attr) }}", + "name": "Build Imagick extension", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-imagick" + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\"]'), matrix.php.attr) }}", + "name": "Build MySQL extension", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-mysql" + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\",\"php70\",\"php71\",\"php72\",\"php73\",\"php74\",\"php80\",\"php81\"]'), matrix.php.attr) }}", + "name": "Validate php.extensions.mysqli default unix socket path", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-mysqli-socket-path" + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\",\"php70\",\"php71\",\"php72\",\"php73\",\"php74\",\"php80\",\"php81\"]'), matrix.php.attr) }}", + "name": "Validate php.extensions.pdo_mysql default unix socket path", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-pdo_mysql-socket-path" + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\",\"php70\",\"php71\",\"php72\",\"php73\",\"php74\",\"php80\",\"php81\"]'), matrix.php.attr) }}", + "name": "Build PHP", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-php" + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\",\"php70\",\"php71\",\"php72\",\"php73\",\"php74\",\"php80\",\"php81\"]'), matrix.php.attr) }}", + "name": "Build Redis extension", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-redis" + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\",\"php70\",\"php71\",\"php72\",\"php73\",\"php74\"]'), matrix.php.attr) }}", + "name": "Build Redis 3 extension", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-redis3" + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\",\"php70\",\"php71\",\"php72\",\"php73\",\"php74\",\"php80\",\"php81\"]'), matrix.php.attr) }}", + "name": "Build Tidy extension", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-tidy" + }, + { + "if": "${{ contains(fromJSON('[\"aarch64-darwin\",\"aarch64-linux\",\"i686-linux\",\"x86_64-darwin\",\"x86_64-linux\"]'), matrix.os.platform) && contains(fromJSON('[\"php56\",\"php70\",\"php71\",\"php72\",\"php73\",\"php74\",\"php80\",\"php81\"]'), matrix.php.attr) }}", + "name": "Build Xdebug extension", + "run": "nix-build -A outputs.checks.${{ matrix.os.platform }}.${{ matrix.php.attr }}-xdebug" + } + ], + "strategy": { + "fail-fast": false, + "matrix": { + "os": [ + { + "image": "ubuntu-20.04", + "platform": "x86_64-linux" + } + ], + "php": [ + { + "attr": "php56", + "branch": "5.6" + }, + { + "attr": "php70", + "branch": "7.0" + }, + { + "attr": "php71", + "branch": "7.1" + }, + { + "attr": "php72", + "branch": "7.2" + }, + { + "attr": "php73", + "branch": "7.3" + }, + { + "attr": "php74", + "branch": "7.4" + }, + { + "attr": "php80", + "branch": "8.0" + }, + { + "attr": "php81", + "branch": "8.1" + } + ] + } + } + } + }, + "name": "Test", + "on": { + "workflow_call": { + "secrets": { + "CACHIX_AUTH_TOKEN": { + "required": true + } + } + } + } +} diff --git a/.github/workflows/workflow-from-flake-checks.nix b/.github/workflows/workflow-from-flake-checks.nix new file mode 100644 index 00000000..8d89b761 --- /dev/null +++ b/.github/workflows/workflow-from-flake-checks.nix @@ -0,0 +1,151 @@ +# Runs Nix flake checks individually with group markers for GitHub Actions. +# Invoke with `nix-shell workflow-from-flake-checks.nix` + +let + self = import ../..; + + pkgs = self.inputs.nixpkgs.legacyPackages.${builtins.currentSystem}; + inherit (self.inputs.nixpkgs) lib; + + checkAttrs = + lib.foldl' + ( + acc: + { + name, + value, + }: + + acc // { + "${name}" = { + inherit (value) description; + platforms = acc.${name}.platforms or {} // { "${value.platform}" = null; }; + phps = acc.${name}.phps or {} // { "${value.php}" = null; }; + }; + } + ) + {} + ( + builtins.concatLists ( + lib.mapAttrsToList + ( + platform: + checks: + + lib.mapAttrsToList + ( + attr: + check: + + let + match = builtins.match "(php[0-9]+)-(.+)" attr; + in + { + name = builtins.head (builtins.tail match); + value = { + inherit (check) description; + inherit platform; + php = builtins.head match; + }; + } + ) + checks + ) + self.outputs.checks + ) + ); + + packages = self.outputs.packages.${builtins.currentSystem}; + phpPackages = builtins.filter (name: builtins.match "php[0-9]+" name != null) (builtins.attrNames packages); + + workflow = { + name = "Test"; + + on = { + workflow_call = { + secrets = { + CACHIX_AUTH_TOKEN = { + required = true; + }; + }; + }; + }; + + jobs = { + test = { + name = "PHP \${{ matrix.php.branch }}"; + runs-on = "\${{ matrix.os.image }}"; + steps = [ + { + uses = "actions/checkout@v2"; + } + { + name = "Install Nix"; + uses = "cachix/install-nix-action@v14"; + } + { + name = "Set up Nix cache"; + uses = "cachix/cachix-action@v10"; + "with" = { + authToken = "\${{ secrets.CACHIX_AUTH_TOKEN }}"; + name = "fossar"; + }; + } + ] + ++ lib.mapAttrsToList + ( + attr: + check: + + { + name = check.description; + "if" = "\${{ contains(fromJSON('${lib.escape [ "\\" "'" ] (builtins.toJSON (builtins.attrNames check.platforms))}'), matrix.os.platform) && contains(fromJSON('${lib.escape [ "\\" "'" ] (builtins.toJSON (builtins.attrNames check.phps))}'), matrix.php.attr) }}"; + run = "nix-build -A outputs.checks.\${{ matrix.os.platform }}.\${{ matrix.php.attr }}-${attr}"; + } + ) + checkAttrs + ; + strategy = { + # We want to fix failures individually. + fail-fast = false; + matrix = { + os = [ + { + image = "ubuntu-20.04"; + platform = "x86_64-linux"; + } + ]; + php = + builtins.map + ( + phpAttr: + + { + branch = lib.versions.majorMinor packages.${phpAttr}.version; + attr = phpAttr; + } + ) + phpPackages; + }; + }; + }; + }; + }; +in + +pkgs.stdenv.mkDerivation { + name = "workflow-from-flake-checks"; + + buildCommand = '' + echo 'Please run `nix-shell workflow-from-flake-checks.nix`, `nix-build` cannot be used.' > /dev/stderr + exit 1 + ''; + + shellHook = + '' + set -o errexit + echo ${lib.escapeShellArgs [ (builtins.toJSON workflow) ]} | ${pkgs.jq}/bin/jq . > .github/workflows/test.yaml + exit 0 + '' + ; +} diff --git a/run-flake-checks.nix b/run-flake-checks.nix deleted file mode 100644 index 199f3cbe..00000000 --- a/run-flake-checks.nix +++ /dev/null @@ -1,67 +0,0 @@ -# Runs Nix flake checks individually with group markers for GitHub Actions. -# Invoke with `nix-shell run-flake-checks.nix --argstr branch "8.1"` -{ - # PHP attribute to run checks for. `null` for all checks. - branch ? null, -}: - -let - self = import ./.; - - pkgs = self.inputs.nixpkgs.legacyPackages.${builtins.currentSystem}; - inherit (self.inputs.nixpkgs) lib; - - checks = self.outputs.checks.${builtins.currentSystem}; - - phpName = - assert lib.assertMsg (builtins.match "[0-9]+.[0-9]+" branch != null) "Branch name “${builtins.toString branch}” does not match a version number."; - - "php${lib.versions.major branch}${lib.versions.minor branch}"; - - relevantChecks = - if branch == null - then checks - else lib.filterAttrs (key: value: lib.hasPrefix "${phpName}-" key) checks; -in - -assert lib.assertMsg (relevantChecks != { }) "No checks found for branch “${builtins.toString branch}”."; - -pkgs.stdenv.mkDerivation { - name = "run-flake-checks"; - - buildCommand = '' - echo 'Please run `nix-shell run-flake-checks.nix`, `nix-build` cannot be used.' > /dev/stderr - exit 1 - ''; - - shellHook = - '' - set -o errexit - '' - + builtins.concatStringsSep - "\n" - ( - lib.mapAttrsToList - ( - name: - value: - - let - description = - lib.optionalString (branch == null) "PHP ${value.phpBranch} – " - + value.description; - in - '' - echo "::group::${description}" - echo Run nix-build -A outputs.checks.${builtins.currentSystem}.${name} - nix-build --no-out-link -A outputs.checks.${builtins.currentSystem}.${name} - echo "::endgroup::" - '' - ) - relevantChecks - ) - + '' - exit 0 - '' - ; -}