Skip to content

Commit 44bc752

Browse files
authored
Merge pull request #50 from RenaCode/ci/publikacja-tylko-z-main
ci: wydanie tylko z main albo z tagu na main
2 parents 2d6b1b9 + e27f853 commit 44bc752

3 files changed

Lines changed: 243 additions & 4 deletions

File tree

‎.github/test-release-workflow.sh‎

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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

‎.github/workflows/ci.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ on:
66
pull_request:
77

88
jobs:
9+
# release.yml publishes only from main or a tag on main
10+
# (see the script's header).
11+
release-workflow:
12+
name: Release workflow guard
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
16+
- name: test-release-workflow
17+
run: bash .github/test-release-workflow.sh
18+
919
build-app:
1020
name: Build Swift package
1121
runs-on: macos-15

‎.github/workflows/release.yml‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ name: Release
1717
# Also: "Run workflow" on the Actions tab (same as a merge), and a vX.Y.Z tag
1818
# pushed by hand, which releases exactly that version.
1919
#
20+
# Only main publishes (2026-11-04). "Run workflow" can be started on any
21+
# branch: there it is a dry run, like a pull request. A tag releases only when
22+
# its commit is on main; a tag on any other commit fails. The release job
23+
# re-checks the ref in IS_RELEASE, so a mistake in `decide` alone cannot
24+
# publish. .github/test-release-workflow.sh (CI) runs `decide` for each kind
25+
# of ref and checks that every publishing step is gated on IS_RELEASE.
26+
#
2027
# A pull request that changes the build or the cask goes through the same
2128
# pipeline as a dry run: ad-hoc signature, no release and no tap, artifacts
2229
# downloadable from the run.
@@ -80,6 +87,9 @@ jobs:
8087
version: ${{ steps.decide.outputs.version }}
8188
steps:
8289
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
90+
with:
91+
# Full history: a tag is checked for being on main (merge-base).
92+
fetch-depth: 0
8393
- id: decide
8494
run: |
8595
base="$(tr -d '[:space:]' < mac-app/VERSION)"
@@ -88,8 +98,18 @@ jobs:
8898
if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then
8999
version="$base"
90100
elif [[ "$GITHUB_REF" == refs/tags/* ]]; then
101+
# A tag can be pushed on any commit; only one on main is released.
102+
git fetch --no-tags origin +refs/heads/main:refs/remotes/origin/main
103+
if ! git merge-base --is-ancestor "$GITHUB_SHA" refs/remotes/origin/main; then
104+
echo "::error::${GITHUB_REF_NAME} is not on main - only commits on main are released."
105+
exit 1
106+
fi
91107
version="${GITHUB_REF_NAME#v}"
92108
release=true
109+
elif [ "$GITHUB_REF" != "refs/heads/main" ]; then
110+
# "Run workflow" on another branch: dry run, never a release.
111+
echo "::notice::${GITHUB_REF} is not main - dry run, nothing is published."
112+
version="$base"
93113
elif git ls-remote --exit-code --tags origin "refs/tags/v${base}" >/dev/null; then
94114
# Base already released: next patch after the highest tag of
95115
# this major.minor. Sorted as versions, not strings (1.3.10 > 1.3.9).
@@ -124,7 +144,8 @@ jobs:
124144
permissions:
125145
contents: write
126146
env:
127-
IS_RELEASE: ${{ needs.decide.outputs.release }}
147+
# Second guard, independent of decide's script: main or a v* tag only.
148+
IS_RELEASE: ${{ needs.decide.outputs.release == 'true' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) }}
128149
VERSION: ${{ needs.decide.outputs.version }}
129150
# Read by build-app and make-dmg; the computed version must not be
130151
# written into mac-app/VERSION, which would mark every release dirty.
@@ -294,11 +315,14 @@ jobs:
294315
exit 1
295316
fi
296317
297-
# Token and the access probe run on EVERY build, not only on release:
298-
# an app that lost its installation (or a missing key) fails the PR,
299-
# not the release after the DMG is already published.
318+
# Token and the access probe run on pull requests too, not only on
319+
# release: an app that lost its installation (or a missing key) fails
320+
# the PR, not the release after the DMG is already published. Not on a
321+
# dry run from another branch: its code has already run in the steps
322+
# above and could read the token (2026-11-04).
300323
- name: Tap token
301324
id: tap-token
325+
if: env.IS_RELEASE == 'true' || github.event_name == 'pull_request'
302326
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
303327
with:
304328
client-id: ${{ vars.TAP_APP_CLIENT_ID }}
@@ -307,6 +331,7 @@ jobs:
307331
repositories: homebrew-tap
308332

309333
- name: Check tap access
334+
if: env.IS_RELEASE == 'true' || github.event_name == 'pull_request'
310335
env:
311336
TAP_TOKEN: ${{ steps.tap-token.outputs.token }}
312337
run: git ls-remote "https://x-access-token:${TAP_TOKEN}@github.com/RenaCode/homebrew-tap.git" HEAD

0 commit comments

Comments
 (0)