Last active
February 7, 2025 19:44
-
-
Save thalesmg/44a089acaa9f73d692c13d3d321f232d to your computer and use it in GitHub Desktop.
github actions retry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -exuo pipefail | |
BRANCH=$1 | |
GITHUB_REPO=emqx/emqx | |
function infer_home_repo() { | |
local branch="$1" | |
IFS=":" read -ra REF <<< "$branch" | |
if [[ "${#REF[@]}" -eq 2 ]]; then | |
echo "${REF[0]}/emqx" | |
else | |
echo "${GITHUB_REPO}" | |
fi | |
} | |
function normalize_branch() { | |
local branch="$1" | |
IFS=":" read -ra REF <<< "$branch" | |
if [[ "${#REF[@]}" -eq 2 ]]; then | |
echo "${REF[1]}" | |
else | |
echo "${branch}" | |
fi | |
} | |
function get_sha() { | |
local branch="$1" | |
local home_repo=$(infer_home_repo "$branch") | |
branch=$(normalize_branch "$branch") | |
output=$(git rev-parse "$branch") | |
if [[ $? -eq 0 ]]; then | |
echo "$output" | |
else | |
echo $(gh api --method GET "/repos/$home_repo/branches/$branch" | jq -r '.commit.sha') | |
fi | |
} | |
while : | |
do | |
HEAD_SHA=$(get_sha "$BRANCH") | |
RUNS=$(gh api --method GET -f status=completed -f head_sha="${HEAD_SHA}" /repos/${GITHUB_REPO}/actions/runs) | |
if jq -e '.workflow_runs[] | select(.conclusion == "success") | .id' <<< ${RUNS} | |
then | |
echo success | |
break | |
else | |
for i in $(jq '.workflow_runs[] | select(.conclusion == "failure" or .conclusion == "cancelled") | .id' <<< ${RUNS}); do | |
echo rerunning "rerun https://github.com/${GITHUB_REPO}/actions/runs/$i" | |
gh api --method POST /repos/${GITHUB_REPO}/actions/runs/$i/rerun-failed-jobs >/dev/null || true | |
done | |
date +'%Y-%m-%d %H:%M:%S' | |
echo "$BRANCH" | |
sleep 45 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment