Repository navigation
Expand file tree
/
Copy pathrelease-gate.sh
More file actions
59 lines (54 loc) · 2.17 KB
/
Copy pathrelease-gate.sh
File metadata and controls
59 lines (54 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
# Release gate: exit non-zero unless CI evidence for $SHA is green.
#
# 1. Validate (push event) finished and succeeded on exactly this SHA.
# 2. If the SHA was merged from a PR, every check on that PR passed or was
# skipped. Blender Smoke runs on PRs only, so this is its evidence.
#
# Env: GH_TOKEN, SHA, GITHUB_REPOSITORY. Optional: GATE_EVENT (default push),
# GATE_TIMEOUT seconds (default 1500), GATE_POLL seconds (default 20).
set -euo pipefail
repo="${GITHUB_REPOSITORY:?}"
sha="${SHA:?}"
event="${GATE_EVENT:-push}"
deadline=$((SECONDS + ${GATE_TIMEOUT:-1500}))
while :; do
read -r status conclusion < <(
gh run list --repo "$repo" --workflow validate.yml --commit "$sha" \
--event "$event" --limit 1 --json status,conclusion \
--jq '.[0] // {} | "\(.status // "none") \(.conclusion // "none")"'
)
[ "$status" = "completed" ] && break
if [ "$SECONDS" -ge "$deadline" ]; then
echo "::error::Validate did not finish for $sha (last status: $status)"
exit 1
fi
echo "Validate status for $sha: $status; waiting"
sleep "${GATE_POLL:-20}"
done
if [ "$conclusion" != "success" ]; then
echo "::error::Validate concluded '$conclusion' for $sha; not releasing"
exit 1
fi
echo "Validate: success"
pr=$(gh api "repos/$repo/commits/$sha/pulls" --jq '.[0].number // empty')
if [ -z "$pr" ]; then
echo "No PR is associated with $sha (direct push); Validate alone gates this release"
exit 0
fi
# gh exits non-zero while checks are pending or failing even with --json, so
# judge the JSON itself (gh's built-in --jq, no jq binary needed) and treat
# unreadable output as a failed gate.
count=$(gh pr checks "$pr" --repo "$repo" --json name --jq 'length' 2>/dev/null) || true
case "$count" in
''|*[!0-9]*|0)
echo "::error::could not read checks for PR #$pr; not releasing"
exit 1
;;
esac
bad=$(gh pr checks "$pr" --repo "$repo" --json name,bucket --jq '[.[] | select(.bucket != "pass" and .bucket != "skipping") | "\(.name)=\(.bucket)"] | join(", ")' 2>/dev/null) || true
if [ -n "$bad" ]; then
echo "::error::PR #$pr has checks that did not pass: $bad; not releasing"
exit 1
fi
echo "PR #$pr: $count checks passed or were skipped"