diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 417093c..4c3128c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,7 @@ on: branches: [main] permissions: + actions: read contents: read concurrency: @@ -14,7 +15,92 @@ concurrency: cancel-in-progress: true jobs: + preflight: + name: Preflight + runs-on: ubuntu-latest + timeout-minutes: 5 + outputs: + expensive: ${{ steps.changes.outputs.expensive }} + release: ${{ steps.changes.outputs.release }} + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + fetch-depth: 0 + persist-credentials: false + - uses: gradle/actions/wrapper-validation@9c971963bec38e04b3d30dcc455b5382be2fdbfb # v6.3.0 + - name: Classify changes + id: changes + shell: bash + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + PREVIOUS_SHA: ${{ github.event.before }} + run: | + set -euo pipefail + python3 - <<'PY' + import os + import subprocess + + event = os.environ["GITHUB_EVENT_NAME"] + revision = None + if event == "pull_request": + revision = f"{os.environ['BASE_SHA']}...{os.environ['HEAD_SHA']}" + elif event == "push" and os.environ["PREVIOUS_SHA"] and os.environ["PREVIOUS_SHA"] != "0" * 40: + revision = f"{os.environ['PREVIOUS_SHA']}..{os.environ['GITHUB_SHA']}" + + paths = None + if revision: + changed = subprocess.check_output([ + "git", "diff", "--no-renames", "--name-only", "-z", revision + ]) + paths = [p for p in changed.decode("utf-8", "surrogateescape").split("\0") if p] + subprocess.run(["git", "diff", "--check", revision], check=True) + + def trivial(path): + return ( + path == "README.md" + or path.startswith("docs/") and path.endswith((".md", ".png", ".jpg", ".jpeg", ".svg", ".webp")) + ) + + def release_related(path): + return ( + path in ("build.gradle.kts", "bounded-origin-cli/build.gradle.kts") + or path.startswith("release-tests/") + or path.startswith("scripts/verify-release-") + or path.startswith(".github/workflows/") + or path in ("scripts/central-portal.sh", "scripts/verify-release-workflow-policy.sh") + ) + + expensive = not (paths is not None and bool(paths) and all(map(trivial, paths))) + release = expensive and (paths is None or any(map(release_related, paths))) + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: + output.write(f"expensive={str(expensive).lower()}\n") + output.write(f"release={str(release).lower()}\n") + PY + - name: Verify required CI approver + shell: bash + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + gh api "repos/${GITHUB_REPOSITORY}/environments/ci-approved" --jq \ + 'if .can_admins_bypass == false then [.protection_rules[] | select(.type == "required_reviewers" and .prevent_self_review == false and (.reviewers | length) == 1 and .reviewers[0].type == "User" and .reviewers[0].reviewer.id == 22101794)] | length else 0 end' \ + | grep -qx '1' + + authorize: + name: Approve expensive CI + needs: preflight + if: needs.preflight.outputs.expensive == 'true' + runs-on: ubuntu-latest + environment: + name: ci-approved + deployment: false + steps: + - run: echo "CI approved" + check: + needs: authorize + if: needs.authorize.result == 'success' strategy: fail-fast: false matrix: @@ -84,6 +170,8 @@ jobs: if: runner.os == 'Windows' reproducibility: + needs: authorize + if: needs.authorize.result == 'success' runs-on: ubuntu-latest steps: - name: Checkout @@ -108,6 +196,7 @@ jobs: docker-smoke: runs-on: ubuntu-latest needs: check + if: needs.check.result == 'success' steps: - name: Checkout uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 @@ -127,3 +216,54 @@ jobs: - name: Docker smoke run: bash ./scripts/docker-smoke.sh + + release-verification: + name: Release verification + needs: [preflight, authorize] + if: needs.authorize.result == 'success' && needs.preflight.outputs.release == 'true' + uses: ./.github/workflows/release-verification.yml + with: + approved: true + permissions: + actions: read + contents: read + + required: + name: Required checks + if: always() + needs: [preflight, authorize, check, reproducibility, docker-smoke, release-verification] + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Verify required jobs + shell: bash + env: + NEEDS_JSON: ${{ toJSON(needs) }} + run: | + python3 - <<'PY' + import json + import os + import sys + + needs = json.loads(os.environ["NEEDS_JSON"]) + results = {name: job["result"] for name, job in needs.items()} + if results["preflight"] != "success": + sys.exit("Preflight failed") + + expensive = needs["preflight"]["outputs"].get("expensive") + release = needs["preflight"]["outputs"].get("release") + if expensive not in ("true", "false") or release not in ("true", "false"): + sys.exit("Invalid change classification") + if expensive == "false" and release != "false": + sys.exit("Invalid release classification") + + expected = "success" if expensive == "true" else "skipped" + for name in ("authorize", "check", "reproducibility", "docker-smoke"): + if results[name] != expected: + sys.exit(f"{name}: expected {expected}, got {results[name]}") + + release_expected = "success" if release == "true" else "skipped" + if results["release-verification"] != release_expected: + sys.exit(f"release-verification: expected {release_expected}, got {results['release-verification']}") + print("All applicable checks passed") + PY diff --git a/.github/workflows/release-verification.yml b/.github/workflows/release-verification.yml index 4e62ac1..95a57e8 100644 --- a/.github/workflows/release-verification.yml +++ b/.github/workflows/release-verification.yml @@ -1,22 +1,16 @@ name: Release Verification on: + workflow_call: + inputs: + approved: + required: true + type: boolean push: branches: [release/0.1.0] - pull_request: - branches: [main] - paths: - - build.gradle.kts - - bounded-origin-cli/build.gradle.kts - - release-tests/** - - scripts/verify-release-*.sh - - .github/workflows/release-verification.yml - - .github/workflows/release.yml - - .github/workflows/central-preflight.yml - - scripts/central-portal.sh - - scripts/verify-release-workflow-policy.sh permissions: + actions: read contents: read concurrency: @@ -24,7 +18,36 @@ concurrency: cancel-in-progress: true jobs: + preflight: + name: Release verification preflight + if: github.event_name == 'push' && !inputs.approved + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Verify required CI approver + shell: bash + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + gh api "repos/${GITHUB_REPOSITORY}/environments/ci-approved" --jq \ + 'if .can_admins_bypass == false then [.protection_rules[] | select(.type == "required_reviewers" and .prevent_self_review == false and (.reviewers | length) == 1 and .reviewers[0].type == "User" and .reviewers[0].reviewer.id == 22101794)] | length else 0 end' \ + | grep -qx '1' + + authorize: + name: Approve release verification + if: github.event_name == 'push' && !inputs.approved + needs: preflight + runs-on: ubuntu-latest + environment: + name: ci-approved + deployment: false + steps: + - run: echo "Release verification approved" + central-bundle: + needs: authorize + if: always() && (inputs.approved == true || needs.authorize.result == 'success') runs-on: ubuntu-latest steps: - name: Checkout @@ -93,6 +116,8 @@ jobs: run: bash scripts/verify-release-negative.sh cli-release: + needs: authorize + if: always() && (inputs.approved == true || needs.authorize.result == 'success') runs-on: ubuntu-latest steps: - name: Checkout @@ -115,6 +140,8 @@ jobs: run: ./gradlew :bounded-origin-cli:cliReleaseArtifacts --stacktrace external-consumer: + needs: authorize + if: always() && (inputs.approved == true || needs.authorize.result == 'success') runs-on: ubuntu-latest steps: - name: Checkout @@ -137,6 +164,8 @@ jobs: run: ./gradlew verifyExternalMavenConsumer --stacktrace release-reproducibility: + needs: authorize + if: always() && (inputs.approved == true || needs.authorize.result == 'success') runs-on: ubuntu-latest steps: - name: Checkout @@ -160,6 +189,8 @@ jobs: run: bash scripts/verify-release-reproducible.sh release-policy: + needs: authorize + if: always() && (inputs.approved == true || needs.authorize.result == 'success') runs-on: ubuntu-latest steps: - name: Checkout diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8314002..3085d56 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,18 +3,9 @@ name: Release on: push: tags: ['v*'] - pull_request: - branches: [main] - paths: - - .github/workflows/release.yml - - .github/workflows/central-preflight.yml - - build.gradle.kts - - bounded-origin-cli/build.gradle.kts - - release-tests/** - - scripts/central-portal.sh - - scripts/verify-release-workflow-policy.sh permissions: + actions: read contents: read concurrency: @@ -22,7 +13,36 @@ concurrency: cancel-in-progress: false jobs: + preflight: + name: Release preflight + if: github.event_name == 'push' + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Verify required CI approver + shell: bash + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + gh api "repos/${GITHUB_REPOSITORY}/environments/ci-approved" --jq \ + 'if .can_admins_bypass == false then [.protection_rules[] | select(.type == "required_reviewers" and .prevent_self_review == false and (.reviewers | length) == 1 and .reviewers[0].type == "User" and .reviewers[0].reviewer.id == 22101794)] | length else 0 end' \ + | grep -qx '1' + + authorize: + name: Approve release verification + if: github.event_name == 'push' + needs: preflight + runs-on: ubuntu-latest + environment: + name: ci-approved + deployment: false + steps: + - run: echo "Release CI approved" + validate: + needs: authorize + if: github.event_name == 'push' && needs.authorize.result == 'success' runs-on: ubuntu-latest steps: - name: Checkout