|
| 1 | +name: Release (Tag) |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v*" |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + release-guard: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + |
| 19 | + - name: Release guard checks |
| 20 | + env: |
| 21 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + REPO_NAME: ${{ github.event.repository.name }} |
| 23 | + REPO_FULL: ${{ github.repository }} |
| 24 | + TAG_NAME: ${{ github.ref_name }} |
| 25 | + run: | |
| 26 | + set -euo pipefail |
| 27 | +
|
| 28 | + fail() { |
| 29 | + echo "::error::$1" |
| 30 | + exit 1 |
| 31 | + } |
| 32 | +
|
| 33 | + warn() { |
| 34 | + echo "::warning::$1" |
| 35 | + } |
| 36 | +
|
| 37 | + api_get() { |
| 38 | + local endpoint="$1" |
| 39 | + curl -fsSL \ |
| 40 | + -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 41 | + -H "Accept: application/vnd.github+json" \ |
| 42 | + "https://api.github.com/repos/${REPO_FULL}${endpoint}" |
| 43 | + } |
| 44 | +
|
| 45 | + api_get_best_effort() { |
| 46 | + local endpoint="$1" |
| 47 | + local out_file="$2" |
| 48 | + local code |
| 49 | + code=$(curl -sS -o "$out_file" -w "%{http_code}" \ |
| 50 | + -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 51 | + -H "Accept: application/vnd.github+json" \ |
| 52 | + "https://api.github.com/repos/${REPO_FULL}${endpoint}") |
| 53 | + echo "$code" |
| 54 | + } |
| 55 | +
|
| 56 | + expected_checks_for_repo() { |
| 57 | + case "$REPO_NAME" in |
| 58 | + repo-template-base) |
| 59 | + printf '%s\n' "core-validation" "pr-title" |
| 60 | + ;; |
| 61 | + repo-template-python) |
| 62 | + printf '%s\n' "core-gated (3.10)" "core-gated (3.11)" "core-gated (3.12)" "pr-title" |
| 63 | + ;; |
| 64 | + repo-template-flutter) |
| 65 | + printf '%s\n' "core-gated" "pr-title" |
| 66 | + ;; |
| 67 | + repo-template-cpp-cmake) |
| 68 | + printf '%s\n' "build" "pr-title" |
| 69 | + ;; |
| 70 | + repo-template-cpp-family) |
| 71 | + printf '%s\n' "smoke" "pr-title" |
| 72 | + ;; |
| 73 | + *) |
| 74 | + printf '%s\n' "pr-title" |
| 75 | + ;; |
| 76 | + esac |
| 77 | + } |
| 78 | +
|
| 79 | + check_branch_protection() { |
| 80 | + local branch="$1" |
| 81 | + local expected |
| 82 | + expected="$(expected_checks_for_repo)" |
| 83 | +
|
| 84 | + local prot_file sig_file |
| 85 | + prot_file="$(mktemp)" |
| 86 | + sig_file="$(mktemp)" |
| 87 | + trap 'rm -f "$prot_file" "$sig_file"' EXIT |
| 88 | +
|
| 89 | + local prot_code |
| 90 | + prot_code="$(api_get_best_effort "/branches/${branch}/protection" "$prot_file")" |
| 91 | + if [ "$prot_code" != "200" ]; then |
| 92 | + warn "Best-effort protection check skipped for ${branch}: HTTP ${prot_code}" |
| 93 | + return 0 |
| 94 | + fi |
| 95 | +
|
| 96 | + jq -e '.required_status_checks != null' "$prot_file" >/dev/null || fail "Missing required status checks protection on ${branch}" |
| 97 | + jq -e '.required_pull_request_reviews != null' "$prot_file" >/dev/null || fail "Missing required PR reviews protection on ${branch}" |
| 98 | + jq -e '.allow_force_pushes.enabled == false' "$prot_file" >/dev/null || fail "Force-push must be blocked on ${branch}" |
| 99 | + jq -e '.allow_deletions.enabled == false' "$prot_file" >/dev/null || fail "Branch deletion must be blocked on ${branch}" |
| 100 | +
|
| 101 | + local contexts |
| 102 | + contexts="$(jq -r '.required_status_checks.contexts[]?' "$prot_file")" |
| 103 | + while IFS= read -r check; do |
| 104 | + [ -n "$check" ] || continue |
| 105 | + echo "$contexts" | grep -Fx "$check" >/dev/null || fail "Required check '${check}' missing on ${branch}" |
| 106 | + done <<< "$expected" |
| 107 | +
|
| 108 | + local sig_code |
| 109 | + sig_code="$(api_get_best_effort "/branches/${branch}/protection/required_signatures" "$sig_file")" |
| 110 | + if [ "$sig_code" != "200" ]; then |
| 111 | + warn "Best-effort signature check skipped for ${branch}: HTTP ${sig_code}" |
| 112 | + return 0 |
| 113 | + fi |
| 114 | + jq -e '.enabled == true' "$sig_file" >/dev/null || fail "Signed commits must be required on ${branch}" |
| 115 | +
|
| 116 | + echo "Protection check passed for ${branch}" |
| 117 | + } |
| 118 | +
|
| 119 | + echo "Checking working tree cleanliness" |
| 120 | + [ -z "$(git status --porcelain)" ] || fail "Repository is not clean at release time" |
| 121 | +
|
| 122 | + echo "Checking tag signature verification for ${TAG_NAME}" |
| 123 | + ref_json="$(api_get "/git/ref/tags/${TAG_NAME}")" |
| 124 | + ref_type="$(echo "$ref_json" | jq -r '.object.type')" |
| 125 | + ref_sha="$(echo "$ref_json" | jq -r '.object.sha')" |
| 126 | +
|
| 127 | + [ "$ref_type" = "tag" ] || fail "Tag ${TAG_NAME} is lightweight; signed annotated tags are required" |
| 128 | +
|
| 129 | + tag_json="$(api_get "/git/tags/${ref_sha}")" |
| 130 | + verified="$(echo "$tag_json" | jq -r '.verification.verified')" |
| 131 | + reason="$(echo "$tag_json" | jq -r '.verification.reason')" |
| 132 | + [ "$verified" = "true" ] || fail "Tag ${TAG_NAME} is not verified (reason: ${reason})" |
| 133 | +
|
| 134 | + echo "Checking required branch protections (best-effort via API)" |
| 135 | + check_branch_protection master |
| 136 | +
|
| 137 | + draft-release: |
| 138 | + runs-on: ubuntu-latest |
| 139 | + needs: release-guard |
| 140 | + steps: |
| 141 | + - name: Create or update draft release |
| 142 | + uses: ncipollo/release-action@v1 |
| 143 | + with: |
| 144 | + tag: ${{ github.ref_name }} |
| 145 | + draft: true |
| 146 | + generateReleaseNotes: true |
| 147 | + allowUpdates: true |
0 commit comments