Skip to content

Commit

Permalink
retry loop
Browse files Browse the repository at this point in the history
  • Loading branch information
bukzor committed Oct 17, 2024
1 parent 205c98d commit 8d18fb8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 43 deletions.
44 changes: 1 addition & 43 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,7 @@ runs:

# required for the below API call
GH_TOKEN: ${{ github.token }}
run: |
exec 2>&1
while true; do
if ! jobs=$(
gh api repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/attempts/$GITHUB_RUN_ATTEMPT
); then
echo retrying failed command, with debug:
if ! jobs=$(
GH_DEBUG=api gh api repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/attempts/$GITHUB_RUN_ATTEMPT
); then
tee -a "$GITHUB_STEP_SUMMARY" <<EOF
Error: Please update your Github Actions job definition to include the
actions.read permission:
permissions:
actions: "read"
EOF
exit 1
fi
fi
if [[ "$jobs" = "null" ]]; then
sleep 1
else
break
fi
done
job=$(
jq <<<"$jobs" --arg runner_name "$RUNNER_NAME" \
'.jobs[] | select(.runner_name == $runner_name)'
)
echo The missing GITHUB_ environment variables:
tee -a "$GITHUB_ENV" <<EOF
GITHUB_JOB_ID=$(jq -r .id <<< "$job")
GITHUB_JOB_NAME=$(jq -r .name <<< "$job")
GITHUB_MATRIX=$(jq -c <<<"$GITHUB_MATRIX")
GITHUB_MATRIX_INDEX=$GITHUB_MATRIX_INDEX
GITHUB_MATRIX_TOTAL=$GITHUB_MATRIX_TOTAL
EOF
run: ${{ github.action_path }}/lib/gha/set-job-vars

- name: Setup Python
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
Expand Down
50 changes: 50 additions & 0 deletions lib/gha/set-job-vars
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
set -ueo pipefail

gha_get_jobs() {
gh api "repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/attempts/$GITHUB_RUN_ATTEMPT"
}

jobs=null
limit=3
sleep=1
while :; do
if ! jobs=$(gha_get_jobs); then
echo retrying failed command, with debug:
if ! jobs=$(set -x; GH_DEBUG=api gha_get_jobs); then
tee -a "$GITHUB_STEP_SUMMARY" <<EOF
Error: Please update your Github Actions job definition to include the
actions.read permission:
permissions:
actions: "read"
EOF
exit 1
fi
fi
if ! [[ "$jobs" = "null" ]]; then
break
elif ((limit-=sleep)); then
sleep "$sleep"
echo $limit $sleep
else
echo timeout
exit 1
fi
done

job=$(
jq <<<"$jobs" --arg runner_name "$RUNNER_NAME" \
'.jobs[] | select(.runner_name == $runner_name)'
)

echo The missing GITHUB_ environment variables:
tee -a "$GITHUB_ENV" <<EOF
GITHUB_JOB_ID=$(jq -r .id <<< "$job")
GITHUB_JOB_NAME=$(jq -r .name <<< "$job")
GITHUB_MATRIX=$(jq -c <<<"$GITHUB_MATRIX")
GITHUB_MATRIX_INDEX=$GITHUB_MATRIX_INDEX
GITHUB_MATRIX_TOTAL=$GITHUB_MATRIX_TOTAL
EOF

0 comments on commit 8d18fb8

Please sign in to comment.