diff --git a/.github/workflows/selftest.yml b/.github/workflows/selftest.yml index 3635ed1..6ac5705 100644 --- a/.github/workflows/selftest.yml +++ b/.github/workflows/selftest.yml @@ -2,11 +2,13 @@ # the fixtures in tests/fixtures: # positive controls go-pass passes the gate and the scan with every fail-on # category set; node-pass-npm / node-pass-bun pass the node -# gate; python-pass passes the python gate; this -# repository's own workflows lint clean +# gate; python-pass passes the python gate; shell-pass +# passes the shell gate and its build output reaches a +# later job; this repository's own workflows lint clean # negative controls planted findings (fake key, reachable vulnerable call, # secret logged to the console, root container, unpinned -# action, failing Go, Node and Python tests, a second or missing +# action, failing Go, Node, Python and bats tests, an +# unquoted shell expansion, a second or missing # lockfile, a privileged container in a Helm chart) are each # reported; a Helm chart trivy skips fails the scan # Negative controls run report-only and are asserted from the outputs, so the @@ -228,6 +230,56 @@ jobs: fi echo "just test failed as expected" + shell-gate-pass: + uses: ./.github/workflows/shell-gate.yml + with: + working-directory: tests/fixtures/shell-pass + build-args: out + build-artifact-path: tests/fixtures/shell-pass/out/greeting.txt + build-artifact-name: shell-gate-selftest + build-artifact-retention-days: 1 + harness-ref: ${{ github.sha }} + + # The build output of the gate must reach a later job of the caller (an + # image test, a release): the artifact is downloaded and checked here. + shell-gate-artifact: + needs: [shell-gate-pass] + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: shell-gate-selftest + path: out + - name: the build output reached this job + run: grep -qx 'hello world' out/greeting.txt + + shell-gate-fail: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - run: scripts/install-tool.sh just shellcheck shfmt + - run: | + sudo apt-get update -qq + sudo apt-get install -y -qq --no-install-recommends bats bats-support bats-assert + - name: the planted findings must fail `just lint` and `just test` + working-directory: tests/fixtures/shell-fail + run: | + fail=0 + if just lint; then + echo "::error::just lint passed although greet.sh has an unquoted expansion" + fail=1 + fi + if just test; then + echo "::error::just test passed although a bats test fails" + fail=1 + fi + [ "$fail" = 0 ] && echo "just lint and just test failed as expected" + exit "$fail" + security-clean: uses: ./.github/workflows/security.yml permissions: @@ -466,7 +518,7 @@ jobs: # above succeeded (skipped or cancelled counts as failed). selftest-ok: if: ${{ always() }} - needs: [lint-self, lint-templates, lint-planted, scripts, gate-pass, gate-fail, node-gate-npm, node-gate-npm-tools, node-gate-bun, node-gate-fail, python-gate-pass, python-gate-fail, security-clean, security-clean-node, security-planted, config-scan-skipped-chart, security-helm, release-dry-run, release-dry-run-package-json, build-context-artifact, release-dry-run-build-context, release-dry-run-image-tag, assert-image-tag, assert-planted] + needs: [lint-self, lint-templates, lint-planted, scripts, gate-pass, gate-fail, node-gate-npm, node-gate-npm-tools, node-gate-bun, node-gate-fail, python-gate-pass, python-gate-fail, shell-gate-pass, shell-gate-artifact, shell-gate-fail, security-clean, security-clean-node, security-planted, config-scan-skipped-chart, security-helm, release-dry-run, release-dry-run-package-json, build-context-artifact, release-dry-run-build-context, release-dry-run-image-tag, assert-image-tag, assert-planted] runs-on: ubuntu-latest timeout-minutes: 5 steps: diff --git a/.github/workflows/shell-gate.yml b/.github/workflows/shell-gate.yml new file mode 100644 index 0000000..a64b867 --- /dev/null +++ b/.github/workflows/shell-gate.yml @@ -0,0 +1,223 @@ +# Reusable gate for shell and just repositories (image builders, installers, +# script collections): lint, unit tests, build. +# +# Every logic step is a `just` recipe of the calling repository, so the gate +# runs the same locally and in CI. `just` runs from `working-directory` and +# finds the repository's Justfile upwards from there. +# +# Recipe contract (see README): +# lint non-mutating static checks: just --fmt --check, shellcheck, +# shfmt -d, yamllint where configured +# test hermetic unit tests (bats) +# build the repository's artefact build, without publishing +# +# The gate provides the tools, the recipes decide how to use them: just, +# shellcheck and shfmt from tools.lock in every job, yamllint (lint) and bats +# with bats-support and bats-assert (unit) from the Ubuntu archive. A build +# that needs more (mmdebstrap, genimage, qemu) names them in +# `build-apt-packages`. `build-artifact-path` hands the build output to a +# later job of the caller (an image test, a release), as an artifact. +# +# Job names stay `lint` / `unit` / `build`, as in the other gates. +name: shell-gate + +on: + workflow_call: + inputs: + working-directory: + description: Directory `just` runs from. + type: string + default: "." + skip: + description: Comma list of jobs to skip (lint, unit, build). + type: string + default: "" + tools: + description: Extra tools from tools.lock to install in every job (space-separated). + type: string + default: "" + apt-packages: + description: Extra Ubuntu packages for every job (space-separated). + type: string + default: "" + build-apt-packages: + description: Extra Ubuntu packages for the build job only (space-separated). + type: string + default: "" + lint-recipe: + type: string + default: lint + test-recipe: + type: string + default: test + build-recipe: + type: string + default: build + build-args: + description: Arguments of the build recipe (space-separated words), e.g. "amd64-efi base". + type: string + default: "" + build-runs-on: + description: Runner of the build job (ubuntu-24.04-arm for a native arm64 build). + type: string + default: ubuntu-latest + build-timeout-minutes: + type: number + default: 30 + build-artifact-path: + description: Files the build leaves for later jobs, relative to the repository root (upload-artifact path syntax); empty uploads nothing. + type: string + default: "" + build-artifact-name: + type: string + default: build + build-artifact-retention-days: + type: number + default: 3 + harness-ref: + description: Ref of IronFlock/workflows the harness scripts are taken from. + type: string + default: v1 + +permissions: + contents: read + +defaults: + run: + shell: bash + +jobs: + lint: + if: ${{ !contains(inputs.skip, 'lint') }} + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: IronFlock/workflows + ref: ${{ inputs.harness-ref }} + path: .harness + persist-credentials: false + sparse-checkout: | + scripts + tools.lock + sparse-checkout-cone-mode: false + - name: move harness out of the workspace + run: | + mv .harness "$RUNNER_TEMP/harness" + echo "HARNESS=$RUNNER_TEMP/harness" >> "$GITHUB_ENV" + - name: install just, shellcheck and shfmt (and extra tools) + env: + TOOLS: ${{ inputs.tools }} + run: | + # shellcheck disable=SC2086 # a space-separated list of tool names + "$HARNESS/scripts/install-tool.sh" just shellcheck shfmt $TOOLS + - name: install yamllint (and extra packages) + env: + PKGS: ${{ inputs.apt-packages }} + run: | + sudo apt-get update -qq + # shellcheck disable=SC2086 # a space-separated list of package names + sudo apt-get install -y -qq --no-install-recommends yamllint $PKGS + - name: just ${{ inputs.lint-recipe }} + working-directory: ${{ inputs.working-directory }} + env: + RECIPE: ${{ inputs.lint-recipe }} + run: just "$RECIPE" + + unit: + if: ${{ !contains(inputs.skip, 'unit') }} + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: IronFlock/workflows + ref: ${{ inputs.harness-ref }} + path: .harness + persist-credentials: false + sparse-checkout: | + scripts + tools.lock + sparse-checkout-cone-mode: false + - name: move harness out of the workspace + run: | + mv .harness "$RUNNER_TEMP/harness" + echo "HARNESS=$RUNNER_TEMP/harness" >> "$GITHUB_ENV" + - name: install just, shellcheck and shfmt (and extra tools) + env: + TOOLS: ${{ inputs.tools }} + run: | + # shellcheck disable=SC2086 # a space-separated list of tool names + "$HARNESS/scripts/install-tool.sh" just shellcheck shfmt $TOOLS + - name: install bats (and extra packages) + env: + PKGS: ${{ inputs.apt-packages }} + run: | + sudo apt-get update -qq + # shellcheck disable=SC2086 # a space-separated list of package names + sudo apt-get install -y -qq --no-install-recommends bats bats-support bats-assert $PKGS + - name: just ${{ inputs.test-recipe }} + working-directory: ${{ inputs.working-directory }} + env: + RECIPE: ${{ inputs.test-recipe }} + run: just "$RECIPE" + + build: + if: ${{ !contains(inputs.skip, 'build') }} + runs-on: ${{ inputs.build-runs-on }} + timeout-minutes: ${{ inputs.build-timeout-minutes }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: IronFlock/workflows + ref: ${{ inputs.harness-ref }} + path: .harness + persist-credentials: false + sparse-checkout: | + scripts + tools.lock + sparse-checkout-cone-mode: false + - name: move harness out of the workspace + run: | + mv .harness "$RUNNER_TEMP/harness" + echo "HARNESS=$RUNNER_TEMP/harness" >> "$GITHUB_ENV" + - name: install just, shellcheck and shfmt (and extra tools) + env: + TOOLS: ${{ inputs.tools }} + run: | + # shellcheck disable=SC2086 # a space-separated list of tool names + "$HARNESS/scripts/install-tool.sh" just shellcheck shfmt $TOOLS + - name: install build packages + if: ${{ inputs.apt-packages != '' || inputs.build-apt-packages != '' }} + env: + PKGS: ${{ inputs.apt-packages }} ${{ inputs.build-apt-packages }} + run: | + sudo apt-get update -qq + # shellcheck disable=SC2086 # a space-separated list of package names + sudo apt-get install -y -qq --no-install-recommends $PKGS + - name: just ${{ inputs.build-recipe }} ${{ inputs.build-args }} + working-directory: ${{ inputs.working-directory }} + env: + RECIPE: ${{ inputs.build-recipe }} + ARGS: ${{ inputs.build-args }} + run: | + # shellcheck disable=SC2086 # recipe arguments are space-separated words + just "$RECIPE" $ARGS + - name: upload the build output + if: ${{ inputs.build-artifact-path != '' }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: ${{ inputs.build-artifact-name }} + path: ${{ inputs.build-artifact-path }} + if-no-files-found: error + retention-days: ${{ inputs.build-artifact-retention-days }} diff --git a/README.md b/README.md index cf5d2f3..d1cace7 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ The inputs of each workflow are documented in its `on.workflow_call` block. | [go-gate.yml](.github/workflows/go-gate.yml) | `lint`, `unit`, `build` | Go gate; cross-build matrix from `targets` / `targets-file` | | [node-gate.yml](.github/workflows/node-gate.yml) | `lint`, `unit`, `build` | Node/Bun gate: `package-manager` bun or npm, installs from the lockfile only (`bun install --frozen-lockfile` / `npm ci`), fails on a lockfile of the other package manager; `skip` drops jobs a repository has no recipe for | | [python-gate.yml](.github/workflows/python-gate.yml) | `lint`, `unit`, `build` | Python gate: uv from `tools.lock`; with a `uv.lock`, `uv sync --frozen` (all groups) before the recipes, otherwise the recipes bring their own tools (pinned `uvx`) | +| [shell-gate.yml](.github/workflows/shell-gate.yml) | `lint`, `unit`, `build` | Shell/just gate (image builders, installers, script collections): just, shellcheck, shfmt from `tools.lock`, yamllint and bats from the Ubuntu archive; `build-apt-packages` for build tools, `build-args` for the build recipe, `build-runs-on` for a native arm64 build, `build-artifact-path` hands the build output to a later job | | [security.yml](.github/workflows/security.yml) | `gitleaks`, `deps`, `semgrep`, `sbom`, `config`, `licenses`, `image`, `report` | security screening; `report` applies `fail-on` | | [workflows-lint.yml](.github/workflows/workflows-lint.yml) | `lint` | actionlint, zizmor, pinact on the caller's workflows; always blocking | | [release-intent.yml](.github/workflows/release-intent.yml) | `intent` | the version file decides: a rise makes a release PR (title `release ` required), an unchanged version a no-release PR; output `kind` gates the release dry run; listed in the `needs` of `ci-ok` | @@ -50,15 +51,15 @@ The inputs of each workflow are documented in its `on.workflow_call` block. | [release-tag.yml](.github/workflows/release-tag.yml) | `tag` | the annotated git tag of a release, after every image job of the caller; a tag always means "in the registry"; written with a token of the release GitHub App from the caller's `release` environment | | [selftest.yml](.github/workflows/selftest.yml) | — | proves the harness against [tests/fixtures](tests/fixtures) | -Gates for Node, Python and chart/shell repositories follow the same pattern -and are added when the first repository of that kind adopts the harness. +A gate for chart repositories follows the same pattern and is added when the +first repository of that kind adopts the harness. ## Recipe contract | Recipe | Meaning | |---|---| -| `lint` | non-mutating static checks. Go: `gofmt -l`, `go mod tidy -diff`, `go vet`, golangci-lint (incl. gosec, config in [templates/repo/.golangci.yml](templates/repo/.golangci.yml)) | -| `test` | hermetic unit tests. Go: `go test -race -shuffle=on -count=1 ./...` | +| `lint` | non-mutating static checks. Go: `gofmt -l`, `go mod tidy -diff`, `go vet`, golangci-lint (incl. gosec, config in [templates/repo/.golangci.yml](templates/repo/.golangci.yml)). Shell: `just --fmt --check`, `shellcheck`, `shfmt -d`, `yamllint` | +| `test` | hermetic unit tests. Go: `go test -race -shuffle=on -count=1 ./...`. Shell: `bats` | | `build` | the artefact build, without publishing | | `sarif`, `sbom` | optional (Go): write govulncheck SARIF / CycloneDX files; otherwise the harness runs govulncheck and cyclonedx-gomod itself | diff --git a/templates/workflows/ci-shell.yml b/templates/workflows/ci-shell.yml new file mode 100644 index 0000000..4e90a5e --- /dev/null +++ b/templates/workflows/ci-shell.yml @@ -0,0 +1,66 @@ +# Caller template for a shell or just repository: gate + workflow lint + release intent. +# Copy to .github/workflows/ci.yml and adjust the `with:` blocks. +# Runs on pull requests into main only: the ruleset lets nothing reach main +# without this check, so a run after the merge would test the same tree again. +name: ci + +on: + pull_request: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + gate: + uses: IronFlock/workflows/.github/workflows/shell-gate.yml@v1 + with: + working-directory: . + # build-args: amd64-efi base + # build-apt-packages: mmdebstrap genimage + + workflows: + uses: IronFlock/workflows/.github/workflows/workflows-lint.yml@v1 + + # The version file decides: a bump makes a release PR (`just release-patch`, + # title `release v`), an unchanged version a no-release PR + # (`just merge`) whose merge releases nothing. + intent: + uses: IronFlock/workflows/.github/workflows/release-intent.yml@v1 + with: + version-file: VERSION + + # Release pull requests only: the release workflow as a dry run (every + # platform build, no push, no tag), so a broken image build never reaches + # main. + release-build: + needs: [intent] + if: needs.intent.outputs.kind == 'release' + uses: ./.github/workflows/release.yml + permissions: + contents: read + id-token: write + + # The one required check of the main-branch ruleset: green only when every + # job above succeeded (skipped or cancelled counts as failed; release-build + # may be skipped, on a no-release pull request). + ci-ok: + if: ${{ always() }} + needs: [gate, workflows, intent, release-build] + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: all ci jobs succeeded + env: + REQUIRED: ${{ needs.gate.result }} ${{ needs.workflows.result }} ${{ needs.intent.result }} + RELEASE_BUILD: ${{ needs.release-build.result }} + KIND: ${{ needs.intent.outputs.kind }} + run: | + echo "required: $REQUIRED, release-build: $RELEASE_BUILD ($KIND)" + for r in $REQUIRED; do [ "$r" = success ] || exit 1; done + if [ "$KIND" = release ]; then [ "$RELEASE_BUILD" = success ]; else [ "$RELEASE_BUILD" = skipped ]; fi diff --git a/tests/fixtures/shell-fail/Justfile b/tests/fixtures/shell-fail/Justfile new file mode 100644 index 0000000..19c9808 --- /dev/null +++ b/tests/fixtures/shell-fail/Justfile @@ -0,0 +1,14 @@ +# Recipe contract of a shell repository, in its smallest form. + +lint: + just --fmt --check --unstable + shellcheck greet.sh + shfmt -d -i 2 greet.sh + yamllint -d relaxed config.yml + +test: + bats test + +build OUT="out": + mkdir -p "{{ OUT }}" + ./greet.sh world > "{{ OUT }}/greeting.txt" diff --git a/tests/fixtures/shell-fail/config.yml b/tests/fixtures/shell-fail/config.yml new file mode 100644 index 0000000..3af3c0d --- /dev/null +++ b/tests/fixtures/shell-fail/config.yml @@ -0,0 +1 @@ +greeting: hello diff --git a/tests/fixtures/shell-fail/greet.sh b/tests/fixtures/shell-fail/greet.sh new file mode 100755 index 0000000..59880e4 --- /dev/null +++ b/tests/fixtures/shell-fail/greet.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# Negative control of the shell gate: an unquoted expansion (SC2086) and a +# greeting the test does not expect. +name=$1 +echo hi $name diff --git a/tests/fixtures/shell-fail/test/greet.bats b/tests/fixtures/shell-fail/test/greet.bats new file mode 100644 index 0000000..2302e1e --- /dev/null +++ b/tests/fixtures/shell-fail/test/greet.bats @@ -0,0 +1,15 @@ +#!/usr/bin/env bats + +load /usr/lib/bats/bats-support/load +load /usr/lib/bats/bats-assert/load + +@test "greets by name" { + run "$BATS_TEST_DIRNAME/../greet.sh" world + assert_success + assert_output "hello world" +} + +@test "refuses to run without a name" { + run "$BATS_TEST_DIRNAME/../greet.sh" + assert_failure +} diff --git a/tests/fixtures/shell-pass/Justfile b/tests/fixtures/shell-pass/Justfile new file mode 100644 index 0000000..19c9808 --- /dev/null +++ b/tests/fixtures/shell-pass/Justfile @@ -0,0 +1,14 @@ +# Recipe contract of a shell repository, in its smallest form. + +lint: + just --fmt --check --unstable + shellcheck greet.sh + shfmt -d -i 2 greet.sh + yamllint -d relaxed config.yml + +test: + bats test + +build OUT="out": + mkdir -p "{{ OUT }}" + ./greet.sh world > "{{ OUT }}/greeting.txt" diff --git a/tests/fixtures/shell-pass/config.yml b/tests/fixtures/shell-pass/config.yml new file mode 100644 index 0000000..3af3c0d --- /dev/null +++ b/tests/fixtures/shell-pass/config.yml @@ -0,0 +1 @@ +greeting: hello diff --git a/tests/fixtures/shell-pass/greet.sh b/tests/fixtures/shell-pass/greet.sh new file mode 100755 index 0000000..e528ede --- /dev/null +++ b/tests/fixtures/shell-pass/greet.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +# Prints a greeting; the positive control of the shell gate. +set -euo pipefail + +name=${1:?usage: greet.sh NAME} +printf 'hello %s\n' "$name" diff --git a/tests/fixtures/shell-pass/test/greet.bats b/tests/fixtures/shell-pass/test/greet.bats new file mode 100644 index 0000000..2302e1e --- /dev/null +++ b/tests/fixtures/shell-pass/test/greet.bats @@ -0,0 +1,15 @@ +#!/usr/bin/env bats + +load /usr/lib/bats/bats-support/load +load /usr/lib/bats/bats-assert/load + +@test "greets by name" { + run "$BATS_TEST_DIRNAME/../greet.sh" world + assert_success + assert_output "hello world" +} + +@test "refuses to run without a name" { + run "$BATS_TEST_DIRNAME/../greet.sh" + assert_failure +} diff --git a/tools.lock b/tools.lock index f953e7d..4bd33d8 100644 --- a/tools.lock +++ b/tools.lock @@ -15,6 +15,7 @@ actionlint 1.7.12 8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349 zizmor 1.31.1 d185f726bdaf92df78273217dfdd24ebcd6b0ef48cf3ce776ff5762b6e138c99 https://github.com/zizmorcore/zizmor/releases/download/v1.31.1/zizmor-x86_64-unknown-linux-gnu.tar.gz zizmor pinact 5.0.0 d005bbb85da80dacdc07816f24a5da723a9f6d1e9f3d3e7e73df33f9caa1358f https://github.com/suzuki-shunsuke/pinact/releases/download/v5.0.0/pinact_linux_amd64.tar.gz pinact shellcheck 0.11.0 8c3be12b05d5c177a04c29e3c78ce89ac86f1595681cab149b65b97c4e227198 https://github.com/koalaman/shellcheck/releases/download/v0.11.0/shellcheck-v0.11.0.linux.x86_64.tar.xz shellcheck-v0.11.0/shellcheck +shfmt 3.14.1 76e77641faa025814b77f153b29796b8e6fa2fca03e0c76a691608b86c7ea7bf https://github.com/mvdan/sh/releases/download/v3.14.1/shfmt_v3.14.1_linux_amd64 - just 1.59.0 409044617b6f06517c1035e55ca38c8c856ff8b650e41bc2aa1ef82d5903a23e https://github.com/casey/just/releases/download/1.59.0/just-1.59.0-x86_64-unknown-linux-musl.tar.gz just kubeconform 0.8.0 9bc2bffbf71f261128533edaf912153948b7ff238f9a531ae6d34466ec287883 https://github.com/yannh/kubeconform/releases/download/v0.8.0/kubeconform-linux-amd64.tar.gz kubeconform helm 3.22.0 1e4ab49e429626cf6c6958d914248b78c9730803c2751b87627e171dc800e7bb https://get.helm.sh/helm-v3.22.0-linux-amd64.tar.gz linux-amd64/helm