From 4e92443832476aa85e3d54df9f81021d847be8af Mon Sep 17 00:00:00 2001 From: nzbr Date: Tue, 7 Nov 2023 01:00:15 +0100 Subject: [PATCH] ci: split out reusable actions --- .../actions/build-nix-expression/action.yml | 27 ++++ .github/actions/build-wsl-tarball/action.yml | 20 +++ .github/actions/install-nix/action.yml | 19 +++ .github/workflows/main.yml | 136 ------------------ .github/workflows/on_push.yml | 21 +++ .../workflows/{release.yml => on_release.yml} | 13 +- .github/workflows/run_build.yml | 48 +++++++ .github/workflows/run_checks.yml | 46 ++++++ .github/workflows/run_tests.yml | 52 +++++++ 9 files changed, 240 insertions(+), 142 deletions(-) create mode 100644 .github/actions/build-nix-expression/action.yml create mode 100644 .github/actions/build-wsl-tarball/action.yml create mode 100644 .github/actions/install-nix/action.yml delete mode 100644 .github/workflows/main.yml create mode 100644 .github/workflows/on_push.yml rename .github/workflows/{release.yml => on_release.yml} (76%) create mode 100644 .github/workflows/run_build.yml create mode 100644 .github/workflows/run_checks.yml create mode 100644 .github/workflows/run_tests.yml diff --git a/.github/actions/build-nix-expression/action.yml b/.github/actions/build-nix-expression/action.yml new file mode 100644 index 00000000..3d3b2b10 --- /dev/null +++ b/.github/actions/build-nix-expression/action.yml @@ -0,0 +1,27 @@ +inputs: + expression: + description: 'Nix expression to build' + required: true + +outputs: + derivation: + description: 'Path to the built derivation' + value: ${{ steps.build.outputs.derivation }} + +runs: + using: 'composite' + steps: + - name: Install Nix โ„๏ธ + uses: ./.github/actions/install-nix + + - name: Build ${{ inputs.expression }} ๐Ÿ› ๏ธ + id: build + shell: bash + run: | + JSON=$(mktemp) + (nix build -L ${{ inputs.expression }} --no-link --json >$JSON) |& sed -uE 's/^(trace: +)?warning:(\s+|$)/::warning::/;s/^(trace: +)?error:(\s+|$)/::error::/;s/^trace:(\s+|$)/::notice::trace: /' + DRV=$(jq -r .[0].outputs.out <$JSON) + echo "derivation=$DRV" >> $GITHUB_OUTPUT + echo "- Built \`$DRV\`" >> $GITHUB_STEP_SUMMARY + echo " - $(nix show-derivation -r $DRV | jq 'keys[]' | wc -l) derivations in closure" >> $GITHUB_STEP_SUMMARY + echo " - $(nix path-info -S --json $DRV | jq -r '.[0].closureSize' | xargs numfmt --to=iec-i --suffix=B --format='%.3f') total size" >> $GITHUB_STEP_SUMMARY diff --git a/.github/actions/build-wsl-tarball/action.yml b/.github/actions/build-wsl-tarball/action.yml new file mode 100644 index 00000000..04e57a8a --- /dev/null +++ b/.github/actions/build-wsl-tarball/action.yml @@ -0,0 +1,20 @@ +inputs: + config: + description: 'System configuration to build' + required: true + filename: + description: 'Filename to save the image as' + required: true + +runs: + using: 'composite' + steps: + - name: Build tarball builder ๐Ÿ› ๏ธ + id: buildBuilder + uses: ./.github/actions/build-nix-expression + with: + expression: '.#nixosConfigurations.${{ inputs.config }}.config.system.build.tarballBuilder' + + - name: Build tarball ๐Ÿ“ฆ + shell: bash + run: sudo ${{ steps.buildBuilder.outputs.derivation }}/bin/nixos-wsl-tarball-builder ${{ inputs.filename }} diff --git a/.github/actions/install-nix/action.yml b/.github/actions/install-nix/action.yml new file mode 100644 index 00000000..1eb45905 --- /dev/null +++ b/.github/actions/install-nix/action.yml @@ -0,0 +1,19 @@ +runs: + using: 'composite' + steps: + - name: Check for nix โœ… + id: check-nix + shell: bash + run: | + if command -v nix &> /dev/null + then + echo "nix-found=true" | tee -a $GITHUB_OUTPUT + else + echo "nix-found=false" | tee -a $GITHUB_OUTPUT + fi + + - name: Install Nix โ„๏ธ + if: ${{ steps.check-nix.outputs.nix-found != 'true' }} + uses: cachix/install-nix-action@v22 + with: + github_access_token: ${{ github.token }} diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 4ae011d3..00000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,136 +0,0 @@ -name: 'CI' - -on: - push: - branches: [main] - pull_request: {} - workflow_call: {} - -jobs: - prepare: - name: Prepare ๐Ÿš€ - runs-on: ubuntu-latest - outputs: - tests: ${{ steps.tests.outputs.tests }} - checks: ${{ steps.checks.outputs.checks }} - version: ${{ steps.version.outputs.version }} - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Install nix โ„๏ธ - uses: cachix/install-nix-action@6a9a9e84a173d90b3ffb42c5ddaf9ea033fad011 # v23 - with: - extra_nix_config: 'access-tokens = github.com=${{ github.token }}' - - - name: Find tests ๐Ÿ” - id: tests - run: | - find tests -name '*.Tests.ps1' -print0 | perl -pe 's|(.*?)\x0|"\1",|g;s|,$||;s|(.*)|tests=[\1]|' >> $GITHUB_OUTPUT - - - name: Find checks ๐Ÿ” - id: checks - run: | - nix-instantiate --json --eval --strict -E 'with builtins; attrNames (getFlake (toString ./.)).checks.${currentSystem}' | perl -pe 's|(.*)|checks=\1|' >>$GITHUB_OUTPUT - - - name: Generate Version ๐Ÿท๏ธ - id: version - run: | - TAG_COUNT=$(git rev-list --tags --no-walk --count) # Count all tags - COMMIT_COUNT=$(git rev-list --use-bitmap-index --count $(git rev-list --tags --no-walk --max-count=1)..HEAD) # Count all commits since the last tag - NIXOS_VERSION=$(nix-instantiate --eval -E '(import ./.).inputs.nixpkgs.lib.version' | sed -E 's/"(.+\...).*"/\1/') # Get NixOS version from nixpkgs - NIXOS_VERSION_MS=$(echo $NIXOS_VERSION | sed -E 's/\.0*(.+)/\.\1/') # Remove the leading 0 from the minor version (if it exists) - NIXOS_WSL_VERSION=${NIXOS_VERSION_MS}.${TAG_COUNT}.${COMMIT_COUNT} # Compose the NixOS-WSL version number - echo "version=$NIXOS_WSL_VERSION" >> $GITHUB_OUTPUT - - build: - name: Build ๐Ÿ› ๏ธ - needs: - - prepare - runs-on: ubuntu-latest - strategy: - matrix: - config: - - modern - - legacy - - test - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Install nix โ„๏ธ - uses: cachix/install-nix-action@6a9a9e84a173d90b3ffb42c5ddaf9ea033fad011 # v23 - with: - extra_nix_config: 'access-tokens = github.com=${{ github.token }}' - - - name: Set version ๐Ÿท๏ธ - run: | - echo ${{ needs.prepare.outputs.version }} > ./VERSION - echo $(git rev-parse HEAD) >> ./VERSION - - - name: Build tarballs ๐Ÿ› ๏ธ - # We can't just nix run here because nix is not on root's PATH in the container - run: | - nix build .#nixosConfigurations.${{ matrix.config }}.config.system.build.tarballBuilder - sudo ./result/bin/nixos-wsl-tarball-builder nixos-wsl.tar.gz - - - name: Upload tarball ๐Ÿ“ค - uses: actions/upload-artifact@v3 - with: - name: tarball-${{ matrix.config }} - path: nixos-wsl.tar.gz - - checks: - name: Flake Check ๐Ÿ“‹ - needs: - - prepare - strategy: - fail-fast: false - matrix: - check: ${{ fromJSON(needs.prepare.outputs.checks) }} - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Install nix โ„๏ธ - uses: cachix/install-nix-action@6a9a9e84a173d90b3ffb42c5ddaf9ea033fad011 # v23 - with: - extra_nix_config: 'access-tokens = github.com=${{ github.token }}' - - - name: Run check ๐Ÿ“‹ - run: | - nix build -L --impure --expr "with builtins; (getFlake (toString ./.)).checks.\${currentSystem}.${{ matrix.check }}" - - tests: - name: Test ๐Ÿงช - needs: - - prepare - - build - strategy: - fail-fast: false - matrix: - test: ${{ fromJSON(needs.prepare.outputs.tests) }} - os: - - ubuntu-20.04 - # - windows-latest # doesn't work due to lack of nested virtualization on the runners, hopefully this will work one day - runs-on: ${{ matrix.os }} - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Download tarball ๐Ÿ“ฅ - uses: actions/download-artifact@v3 - with: - name: tarball-test - - - name: Execute test ๐Ÿงช - shell: pwsh - run: | - Invoke-Pester -Output Detailed ${{ matrix.test }} diff --git a/.github/workflows/on_push.yml b/.github/workflows/on_push.yml new file mode 100644 index 00000000..8fc3997f --- /dev/null +++ b/.github/workflows/on_push.yml @@ -0,0 +1,21 @@ +name: "Push" + +on: + push: + branches: [main] + pull_request: {} + +jobs: + build: + name: Build Tarballs ๐Ÿ› ๏ธ + uses: ./.github/workflows/run_build.yml + + checks: + name: Flake Checks ๐Ÿ“‹ + uses: ./.github/workflows/run_checks.yml + + tests: + name: Tests ๐Ÿงช + uses: ./.github/workflows/run_tests.yml + needs: + - build diff --git a/.github/workflows/release.yml b/.github/workflows/on_release.yml similarity index 76% rename from .github/workflows/release.yml rename to .github/workflows/on_release.yml index ab862ea5..12009612 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/on_release.yml @@ -1,4 +1,4 @@ -name: 'Release' +name: "Release" on: push: @@ -6,18 +6,19 @@ on: jobs: build: - name: Build ๐Ÿ› ๏ธ - uses: nix-community/nixos-wsl/.github/workflows/main.yml@main + name: Build Tarballs ๐Ÿ› ๏ธ + uses: ./.github/workflows/run_build.yml + release: needs: - build name: Create Release ๐Ÿ“ข runs-on: ubuntu-latest steps: - - name: Download tarball ๐Ÿ“ฅ + - name: Download Tarball ๐Ÿ“ฅ uses: actions/download-artifact@v3 - - name: Generate checksums ๐Ÿ”‘ + - name: Generate Checksums ๐Ÿ”‘ run: | mv tarball-modern/nixos-wsl.tar.gz nixos-wsl.tar.gz mv tarball-legacy/nixos-wsl.tar.gz nixos-wsl-legacy.tar.gz @@ -25,7 +26,7 @@ jobs: sha256sum $x > ${x}.sha256 done - - name: Attach to release ๐Ÿ“ฆ + - name: Attach to Release ๐Ÿ“Ž uses: softprops/action-gh-release@v1 with: files: | diff --git a/.github/workflows/run_build.yml b/.github/workflows/run_build.yml new file mode 100644 index 00000000..f0b40cc7 --- /dev/null +++ b/.github/workflows/run_build.yml @@ -0,0 +1,48 @@ +name: Build Tarballs + +on: + workflow_call: {} + +jobs: + build: + name: Build ๐Ÿ› ๏ธ + runs-on: ubuntu-latest + strategy: + matrix: + config: + - modern + - legacy + - test + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install Nix โ„๏ธ + uses: ./.github/actions/install-nix + + - name: Generate Version ๐Ÿท๏ธ + id: version + run: | + TAG_COUNT=$(git rev-list --tags --no-walk --count) # Count all tags + COMMIT_COUNT=$(git rev-list --use-bitmap-index --count $(git rev-list --tags --no-walk --max-count=1)..HEAD) # Count all commits since the last tag + NIXOS_VERSION=$(nix-instantiate --eval -E '(import ./.).inputs.nixpkgs.lib.version' | sed -E 's/"(.+\...).*"/\1/') # Get NixOS version from nixpkgs + NIXOS_VERSION_MS=$(echo $NIXOS_VERSION | sed -E 's/\.0*(.+)/\.\1/') # Remove the leading 0 from the minor version (if it exists) + NIXOS_WSL_VERSION=${NIXOS_VERSION_MS}.${TAG_COUNT}.${COMMIT_COUNT} # Compose the NixOS-WSL version number + echo "version=$NIXOS_WSL_VERSION" >> $GITHUB_OUTPUT + + echo $NIXOS_WSL_VERSION > ./VERSION + echo $(git rev-parse HEAD) >> ./VERSION + + - name: Build Tarball ๐Ÿ› ๏ธ + uses: ./.github/actions/build-wsl-tarball + with: + config: ${{ matrix.config }} + filename: nixos-wsl.tar.gz + + - name: Upload Tarball ๐Ÿ“ค + uses: actions/upload-artifact@v3 + with: + name: tarball-${{ matrix.config }} + path: nixos-wsl.tar.gz diff --git a/.github/workflows/run_checks.yml b/.github/workflows/run_checks.yml new file mode 100644 index 00000000..dfca9abb --- /dev/null +++ b/.github/workflows/run_checks.yml @@ -0,0 +1,46 @@ +name: Flake Checks + +on: + workflow_call: {} + +jobs: + prepare: + name: Find Checks ๐Ÿ” + runs-on: ubuntu-latest + outputs: + checks: ${{ steps.checks.outputs.checks }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install Nix โ„๏ธ + uses: ./.github/actions/install-nix + + - name: Find Checks ๐Ÿ” + id: checks + run: | + nix-instantiate --json --eval --strict -E 'with builtins; attrNames (getFlake (toString ./.)).checks.${currentSystem}' | perl -pe 's|(.*)|checks=\1|' >>$GITHUB_OUTPUT + + checks: + name: Check ๐Ÿ“‹ + needs: + - prepare + strategy: + fail-fast: false + matrix: + check: ${{ fromJSON(needs.prepare.outputs.checks) }} + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install Nix โ„๏ธ + uses: ./.github/actions/install-nix + + - name: Run Check ๐Ÿ“‹ + run: | + nix build -L --impure --expr "with builtins; (getFlake (toString ./.)).checks.\${currentSystem}.${{ matrix.check }}" diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml new file mode 100644 index 00000000..3cf476be --- /dev/null +++ b/.github/workflows/run_tests.yml @@ -0,0 +1,52 @@ +name: Tests + +on: + workflow_call: {} + +jobs: + prepare: + name: Find Tests ๐Ÿ” + runs-on: ubuntu-latest + outputs: + tests: ${{ steps.tests.outputs.tests }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install Nix โ„๏ธ + uses: ./.github/actions/install-nix + + - name: Find Tests ๐Ÿ” + id: tests + run: | + find tests -name '*.Tests.ps1' -print0 | perl -pe 's|(.*?)\x0|"\1",|g;s|,$||;s|(.*)|tests=[\1]|' >> $GITHUB_OUTPUT + + tests: + name: Test ๐Ÿงช + needs: + - prepare + strategy: + fail-fast: false + matrix: + test: ${{ fromJSON(needs.prepare.outputs.tests) }} + os: + - ubuntu-20.04 + # - windows-latest # doesn't work due to lack of nested virtualization on the runners, hopefully this will work one day + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Download Tarball ๐Ÿ“ฅ + uses: actions/download-artifact@v3 + with: + name: tarball-test + + - name: Execute Test ๐Ÿงช + shell: pwsh + run: | + Invoke-Pester -Output Detailed ${{ matrix.test }}