8000 ci(release): gate publishing on CI and dispatch bindings after crates.io (#938, #939) by adityamukho · Pull Request #955 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions .github/release/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/usr/bin/env bash
set -euo pipefail

fail() {
echo "::error::$*" >&2
exit 1
}

validate_tag() {
local number='(0|[1-9][0-9]*)'
local identifier='(0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)'
[[ "$1" =~ ^v${number}\.${number}\.${number}(-${identifier}(\.${identifier})*)?$ ]] || fail "Invalid release tag: $1"
}

resolve_tag() {
validate_tag "$1"
gh api "repos/$GITHUB_REPOSITORY/commits/refs%2Ftags%2F$1" | jq -er '.sha'
}

release_runs() {
gh api "repos/$GITHUB_REPOSITORY/actions/workflows/release.yml/runs?head_sha=$1&event=push&per_page=100" --paginate --slurp
}

validate_run() {
local run=$1 tag=$2 sha=$3 workflow_id
workflow_id=$(gh api "repos/$GITHUB_REPOSITORY/actions/workflows/release.yml" | jq -er '.id')
jq -e --arg repo "$GITHUB_REPOSITORY" --arg tag "$tag" --arg sha "$sha" --argjson workflow "$workflow_id" '
.name == "Release" and .path == ".github/workflows/release.yml" and
.workflow_id == $workflow and .event == "push" and
.status == "completed" and .conclusion == "success" and
.head_branch == $tag and .head_sha == $sha and
.repository.full_name == $repo and .repository.fork == false and
.head_repository.full_name == $repo and .head_repository.fork == false
' <<< "$run" >/dev/null || fail 'Not a successful official tag-push Release run for this commit'
}

check_ci() {
local tag=$1 sha=$2 resolved files filename name runs latest checked=0
resolved=$(resolve_tag "$tag")
[[ "$resolved" == "$sha" ]] || fail "Tag $tag no longer resolves to $sha"
files=$(gh api "repos/$GITHUB_REPOSITORY/contents/.github/workflows?ref=$sha")
while IFS='|' read -r filename name; do
if ! jq -e --arg file "$filename" 'any(.[]; .name == $file)' <<< "$files" >/dev/null; then
echo "Skipping $name: workflow absent from $tag ($sha)"
continue
fi
checked=$((checked + 1))
runs=$(gh api "repos/$GITHUB_REPOSITORY/actions/workflows/$filename/runs?head_sha=$sha&event=push&per_page=100" --paginate --slurp)
latest=$(jq -c --arg repo "$GITHUB_REPOSITORY" --arg sha "$sha" --arg name "$name" --arg path ".github/workflows/$filename" '
[.[] | .workflow_runs[] |
select(.head_sha == $sha and .event == "push" and .name == $name and .path == $path and
.repository.full_name == $repo and .repository.fork == false and
.head_repository.full_name == $repo and .head_repository.fork == false)] |
max_by(.id)
' <<< "$runs")
jq -e '.status == "completed" and .conclusion == "success"' <<< "$latest" >/dev/null ||
fail "$name has no successful latest push run for $sha; finish CI before tagging or retrying Release"
echo "$name: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$(jq -r '.id' <<< "$latest") ($sha)"
done <<'WORKFLOWS'
rust.yml|Rust
wasm.yml|WASM
lint.yml|Lint
coverage-gates.yml|Coverage Gates
policy.yml|Policy
binary-size.yml|Binary Size
WORKFLOWS
(( checked > 0 )) || fail "No required workflows found at $sha"
}

prepare_cascade() {
local run tag sha runs
case "$GITHUB_EVENT_NAME" in
workflow_run)
run=$(jq -c '.workflow_run' "$GITHUB_EVENT_PATH")
if [[ $(jq -r '.conclusion' <<< "$run") != success ]]; then
echo 'Release did not succeed; nothing to dispatch'
return
fi
tag=$(jq -er '.head_branch' <<< "$run")
sha=$(resolve_tag "$tag")
;;
workflow_dispatch)
tag=$(jq -er '.inputs.tag' "$GITHUB_EVENT_PATH")
sha=$(resolve_tag "$tag")
runs=$(release_runs "$sha")
run=$(jq -c --arg tag "$tag" '[.[] | .workflow_runs[] | select(.head_branch == $tag)] | max_by(.id)' <<< "$runs")
;;
*) fail "Unsupported cascade event: $GITHUB_EVENT_NAME" ;;
esac
validate_run "$run" "$tag" "$sha"
check_ci "$tag" "$sha"
echo "tag=$tag" >> "$GITHUB_OUTPUT"
}

wait_for_crate() {
local tag=$1 version deadline now remaining delay=10 response
validate_tag "$tag"
version=${tag#v}
deadline=$(( $(date +%s) + 3600 ))
while true; do
if response=$(curl --fail --silent --show-error --connect-timeout 10 --max-time 30 \
-H 'User-Agent: minigraf-cascade/1.0' -H 'Cache-Control: no-cache' \
--write-out $'\n%{http_code}' 'https://index.crates.io/mi/ni/minigraf') &&
[[ "${response##*$'\n'}" == 200 ]] &&
jq -se --arg version "$version" 'any(.[]; .vers == $version and .yanked == false)' \
<<< "${response%$'\n'*}" >/dev/null; then
echo "minigraf@$version is available in the Cargo sparse index"
return
fi
now=$(date +%s)
remaining=$((deadline - now))
(( remaining > 0 )) || fail "minigraf@$version was not indexed within 60 minutes"
(( delay <= remaining )) || delay=$remaining
echo "Waiting ${delay}s for minigraf@$version"
sleep "$delay"
delay=$((delay * 2))
(( delay <= 120 )) || delay=120
done
}

dispatch_bindings() {
local tag=$1 repo
validate_tag "$tag"
for repo in minigraf-python minigraf-node minigraf-wasm \
minigraf-java minigraf-android minigraf-swift minigraf-c; do
echo "Dispatching core-release to project-minigraf/$repo @ $tag"
gh api "repos/project-minigraf/$repo/dispatches" \
-f event_type=core-release -f "client_payload[version]=$tag"
done
}

GITHUB_SERVER_URL=${GITHUB_SERVER_URL:-https://github.com}
case "${1:-}" in
prepare) prepare_cascade ;;
gate) check_ci "$2" "$3" ;;
wait) wait_for_crate "$2" ;;
dispatch) dispatch_bindings "$2" ;;
*) fail 'Usage: release.sh prepare | gate TAG SHA | wait TAG | dispatch TAG' ;;
esac
Loading
Loading
0