|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Guard for .github/workflows/release.yml: a release (GitHub Release, tag, cask |
| 3 | +# pushed to the Homebrew tap, signing certificate imported) happens only from |
| 4 | +# main or from a vX.Y.Z tag on a commit that is on main. |
| 5 | +# |
| 6 | +# "Run workflow" can be started on any branch, and a tag can be pushed on any |
| 7 | +# commit. Before this guard a manual run on a feature branch computed |
| 8 | +# release=true and published unmerged code to every `brew upgrade`. |
| 9 | +# |
| 10 | +# This guards against mistakes, not against someone with push access: a run |
| 11 | +# uses the workflow file of its own ref, which that ref can change. The hard |
| 12 | +# stop is repository settings (an environment limited to main and v* tags |
| 13 | +# holding the secrets, a ruleset on v* tags). |
| 14 | +# |
| 15 | +# Two kinds of checks: |
| 16 | +# - the `decide` step's script is extracted from the workflow and RUN with a |
| 17 | +# stub `git`, for each kind of ref (behaviour, not text); |
| 18 | +# - the release job's IS_RELEASE also requires main or a tag (second guard, |
| 19 | +# independent of decide), and every step that publishes or reads a secret |
| 20 | +# is gated on it. |
| 21 | +# |
| 22 | +# No YAML parser: plain bash + awk, so it runs on any runner. |
| 23 | +# Usage: .github/test-release-workflow.sh [path-to-release.yml] |
| 24 | +set -euo pipefail |
| 25 | + |
| 26 | +F="${1:-.github/workflows/release.yml}" |
| 27 | +[ -f "$F" ] || { echo "missing $F"; exit 1; } |
| 28 | +CODE="$(grep -v '^[[:space:]]*#' "$F")" |
| 29 | +fail=0 |
| 30 | +ok() { echo "OK $1"; } |
| 31 | +bad() { echo "BAD $1"; fail=1; } |
| 32 | + |
| 33 | +work="$(mktemp -d "${TMPDIR:-/tmp}/release-wf.XXXXXX")" |
| 34 | +trap 'rm -rf "$work"' EXIT |
| 35 | + |
| 36 | +# --- decide: run its script ------------------------------------------------- |
| 37 | +# The `run: |` block right after `- id: decide`, de-indented. |
| 38 | +printf '%s\n' "$CODE" | awk ' |
| 39 | + /^[[:space:]]*- id: decide[[:space:]]*$/ { found = 1; next } |
| 40 | + found && !inrun && /^[[:space:]]*run: \|[[:space:]]*$/ { |
| 41 | + inrun = 1; ind = -1; next |
| 42 | + } |
| 43 | + inrun { |
| 44 | + if ($0 ~ /^[[:space:]]*$/) { print ""; next } |
| 45 | + match($0, /^ */); n = RLENGTH |
| 46 | + if (ind < 0) ind = n |
| 47 | + if (n < ind) exit |
| 48 | + print substr($0, ind + 1) |
| 49 | + }' > "$work/decide.sh" |
| 50 | +if [ ! -s "$work/decide.sh" ]; then |
| 51 | + bad "decide step script not found" |
| 52 | + echo "FAILED"; exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +# Stub git: tags that exist come from STUB_TAGS (space separated, without v). |
| 56 | +# STUB_ON_MAIN=yes|no answers `merge-base --is-ancestor`, which must ask about |
| 57 | +# the run's own commit against the freshly fetched main - anything else (HEAD, |
| 58 | +# a local `main`, another sha) is an error, so a check that compares the wrong |
| 59 | +# things cannot pass. |
| 60 | +mkdir -p "$work/bin" |
| 61 | +cat > "$work/bin/git" <<'STUB' |
| 62 | +#!/usr/bin/env bash |
| 63 | +case "$1" in |
| 64 | + fetch) |
| 65 | + [ "$*" = "fetch --no-tags origin +refs/heads/main:refs/remotes/origin/main" ] \ |
| 66 | + || { echo "stub git: unexpected '$*'" >&2; exit 97; } |
| 67 | + ;; |
| 68 | + merge-base) |
| 69 | + [ "$*" = "merge-base --is-ancestor $GITHUB_SHA refs/remotes/origin/main" ] \ |
| 70 | + || { echo "stub git: unexpected '$*'" >&2; exit 97; } |
| 71 | + [ "${STUB_ON_MAIN:-no}" = yes ] |
| 72 | + ;; |
| 73 | + ls-remote) |
| 74 | + shift |
| 75 | + exact=0 |
| 76 | + [ "$1" = "--exit-code" ] && { exact=1; shift; } |
| 77 | + while [ "${1#--}" != "$1" ]; do shift; done |
| 78 | + shift # origin |
| 79 | + pattern="${1#refs/tags/v}" |
| 80 | + hit=0 |
| 81 | + for t in ${STUB_TAGS:-}; do |
| 82 | + # shellcheck disable=SC2053 |
| 83 | + if [[ "$t" == $pattern ]]; then |
| 84 | + echo "0000000000000000000000000000000000000000 refs/tags/v$t"; hit=1 |
| 85 | + fi |
| 86 | + done |
| 87 | + [ $exact = 0 ] || [ $hit = 1 ] |
| 88 | + ;; |
| 89 | + *) echo "stub git: unexpected '$*'" >&2; exit 97 ;; |
| 90 | +esac |
| 91 | +STUB |
| 92 | +chmod +x "$work/bin/git" |
| 93 | +mkdir -p "$work/repo/mac-app" |
| 94 | +echo "1.3.0" > "$work/repo/mac-app/VERSION" |
| 95 | + |
| 96 | +# run_decide EVENT REF ON_MAIN -> prints "rc release skip version" |
| 97 | +run_decide() { |
| 98 | + local out="$work/out" rc=0 |
| 99 | + : > "$out" |
| 100 | + ( |
| 101 | + cd "$work/repo" |
| 102 | + PATH="$work/bin:$PATH" GITHUB_OUTPUT="$out" GITHUB_EVENT_NAME="$1" \ |
| 103 | + GITHUB_REF="$2" GITHUB_REF_NAME="${2##*/}" GITHUB_SHA=abc123 \ |
| 104 | + STUB_TAGS="1.3.0 1.3.1" STUB_ON_MAIN="$3" \ |
| 105 | + bash -e "$work/decide.sh" |
| 106 | + ) > "$work/log" 2>&1 || rc=$? |
| 107 | + local rel skip ver |
| 108 | + rel="$(sed -n 's/^release=//p' "$out")" |
| 109 | + skip="$(sed -n 's/^skip=//p' "$out")" |
| 110 | + ver="$(sed -n 's/^version=//p' "$out")" |
| 111 | + echo "$rc ${rel:-none} ${skip:-none} ${ver:-none}" |
| 112 | +} |
| 113 | + |
| 114 | +expect() { |
| 115 | + local what="$1" want="$2" got |
| 116 | + got="$(run_decide "$3" "$4" "$5")" |
| 117 | + if [ "$got" = "$want" ]; then |
| 118 | + ok "$what ($got)" |
| 119 | + else |
| 120 | + bad "$what: want '$want', got '$got'" |
| 121 | + sed 's/^/ /' "$work/log" |
| 122 | + fi |
| 123 | +} |
| 124 | + |
| 125 | +# VERSION is 1.3.0 and v1.3.0, v1.3.1 exist, so main releases the next patch. |
| 126 | +# rc release skip version |
| 127 | +expect "push to main releases the next patch" "0 true false 1.3.2" push refs/heads/main yes |
| 128 | +expect "manual run on main releases" "0 true false 1.3.2" workflow_dispatch refs/heads/main yes |
| 129 | +expect "pull request is a dry run" "0 false false 1.3.0" pull_request refs/pull/7/merge no |
| 130 | +expect "manual run on a feature branch does NOT release" "0 false false 1.3.0" workflow_dispatch refs/heads/feature/x no |
| 131 | +expect "push to another branch does NOT release" "0 false false 1.3.0" push refs/heads/feature/x no |
| 132 | +expect "tag on a commit on main releases that version" "0 true false 1.3.5" push refs/tags/v1.3.5 yes |
| 133 | +expect "tag on a commit NOT on main fails" "1 none none none" push refs/tags/v1.3.5 no |
| 134 | +expect "manual run on a tag NOT on main fails" "1 none none none" workflow_dispatch refs/tags/v1.3.5 no |
| 135 | + |
| 136 | +# --- second guard in the release job ---------------------------------------- |
| 137 | +release_job="$(printf '%s\n' "$CODE" | awk ' |
| 138 | + /^ release:/ { j = 1; next } |
| 139 | + j && /^ [^ ]/ { j = 0 } |
| 140 | + j { print }')" |
| 141 | +# Compared whole: any extra `|| ...` would let another ref through. |
| 142 | +is_release="$(printf '%s\n' "$release_job" | grep -E '^ IS_RELEASE:' || true)" |
| 143 | +want_is_release=" IS_RELEASE: \${{ needs.decide.outputs.release == 'true' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) }}" |
| 144 | +if [ "$is_release" = "$want_is_release" ]; then |
| 145 | + ok "IS_RELEASE requires decide + (main or v* tag)" |
| 146 | +else |
| 147 | + bad "IS_RELEASE: want '${want_is_release# }', got '${is_release:-none}'" |
| 148 | +fi |
| 149 | + |
| 150 | +# --- every step that publishes or reads a secret ---------------------------- |
| 151 | +# One line per step of the release job: name, if, secret (\037-separated); secret is |
| 152 | +# 1 when the step reads a secret or the tap token. The `if:` is found wherever |
| 153 | +# it sits in the step (after `id:`, `uses:`, ...). |
| 154 | +steps="$(printf '%s\n' "$release_job" | awk ' |
| 155 | + function flush() { if (started) printf "%s\037%s\037%d\n", name, cond, sec } |
| 156 | + /^ steps:/ { insteps = 1; next } |
| 157 | + !insteps { next } |
| 158 | + /^ - / { flush(); started = 1; name = ""; cond = ""; sec = 0 } |
| 159 | + !started { next } |
| 160 | + { line = $0; sub(/^ - /, " ", line) } |
| 161 | + line ~ /^ name: / { name = substr(line, 15) } |
| 162 | + line ~ /^ uses: / && name == "" { name = substr(line, 15) } |
| 163 | + line ~ /^ if: / { cond = substr(line, 13) } |
| 164 | + /secrets\.|steps\.tap-token\./ { sec = 1 } |
| 165 | + END { flush() }')" |
| 166 | + |
| 167 | +for step in "Import signing certificate" "Tap token" "Check tap access" \ |
| 168 | + "Check tap app" "Publish GitHub Release" "Push cask to tap"; do |
| 169 | + printf '%s\n' "$steps" | cut -d $'\037' -f1 | grep -qxF "$step" || bad "step '$step' not found in the release job" |
| 170 | +done |
| 171 | + |
| 172 | +# The tap token is also taken on pull requests, on purpose: an app that lost |
| 173 | +# its installation fails the PR, not the release. Never on a dry run from |
| 174 | +# another branch - that branch's code has already run in earlier steps. |
| 175 | +TAP_PROBE="env.IS_RELEASE == 'true' || github.event_name == 'pull_request'" |
| 176 | +# Publishing steps that read no secret themselves: the gate that stops a build |
| 177 | +# which changed tracked files, the release action (job token), git push with |
| 178 | +# the tap checkout's credentials. |
| 179 | +PUBLISHING="Build did not change tracked files|Publish GitHub Release|Check out tap|Push cask to tap" |
| 180 | +GATE="env.IS_RELEASE == 'true'" |
| 181 | +# \037 (unit separator), not a tab: `read` merges adjacent tabs, so an empty |
| 182 | +# `if` would shift the fields. |
| 183 | +while IFS=$'\037' read -r name cond sec; do |
| 184 | + [ -n "$name" ] || continue |
| 185 | + case "$name" in |
| 186 | + "Tap token" | "Check tap access") |
| 187 | + if [ "$cond" = "$TAP_PROBE" ]; then |
| 188 | + ok "$name: release or pull request only" |
| 189 | + else |
| 190 | + bad "$name: want 'if: $TAP_PROBE', got '${cond:-no if}'" |
| 191 | + fi |
| 192 | + continue ;; |
| 193 | + esac |
| 194 | + if [ "$sec" = 1 ] || printf '%s\n' "$name" | grep -qxE "$PUBLISHING"; then |
| 195 | + if [ "$cond" = "$GATE" ]; then |
| 196 | + ok "$name: only when IS_RELEASE" |
| 197 | + else |
| 198 | + bad "$name: publishes or reads a secret, want 'if: $GATE', got '${cond:-no if}'" |
| 199 | + fi |
| 200 | + fi |
| 201 | +done <<< "$steps" |
| 202 | + |
| 203 | +[ $fail = 0 ] && echo "ALL OK" || echo "FAILED" |
| 204 | +exit $fail |
0 commit comments