-
Notifications
You must be signed in to change notification settings - Fork 15
Harden pull image action against GHA connectivity instability #7255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cbeauchesne
wants to merge
5
commits into
main
Choose a base branch
from
cbeauchesne/APMSP-3622
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−31
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/usr/bin/env bash | ||
| # Logs in to Docker Hub (if credentials are provided) and pulls the images | ||
| # listed in compose.yaml, retrying on failure. | ||
| # | ||
| # When a command fails, unless the error is known to not be retryable, the command will be retried | ||
| # | ||
| # Expects DOCKERHUB_USERNAME / DOCKERHUB_TOKEN in the environment (may be empty). | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Backoff delay (seconds) indexed by attempt number (1-based) | ||
| # since github action | ||
| BACKOFF_SECONDS=(10 20 40 80 160 320 320 320 320 320 320 320 320 320 320 320) | ||
| MAX_ATTEMPTS="${#BACKOFF_SECONDS[@]}" | ||
|
|
||
| # Error patterns that should never be retried (auth/permission/not-found errors | ||
| # won't fix themselves on a retry). | ||
| NON_RETRYABLE_PATTERNS=( | ||
| "incorrect username or password" | ||
| "unauthorized" | ||
| "unknown command: docker compose" | ||
| "no configuration file provided: not found" | ||
| "empty compose file" | ||
| "yaml: construct errors" | ||
| ) | ||
|
|
||
| retry_with_timeout() { | ||
| local description="$1" | ||
| local timeout_seconds="$2" | ||
| shift 2 | ||
|
|
||
| local attempt=1 | ||
| local retry_wait_seconds | ||
| local output_file | ||
| output_file="$(mktemp)" | ||
| trap 'rm -f "${output_file}"' RETURN | ||
|
|
||
| while true; do | ||
| echo "${description} (attempt ${attempt}/${MAX_ATTEMPTS}, timeout ${timeout_seconds}s)..." | ||
|
|
||
| # Both save the output in output_file and print it to stdout. | ||
| # exit function if the command succeed | ||
| if timeout "${timeout_seconds}" "$@" 2>&1 | tee "${output_file}"; then | ||
| return 0 | ||
| fi | ||
|
|
||
| for pattern in "${NON_RETRYABLE_PATTERNS[@]}"; do | ||
| if grep -q "${pattern}" "${output_file}"; then | ||
| echo "${description} failed with a non-retryable error: ${pattern}" | ||
| return 1 | ||
| fi | ||
| done | ||
|
|
||
| echo "${description} failed (attempt ${attempt}/${MAX_ATTEMPTS})" | ||
| if [ "${attempt}" -ge "${MAX_ATTEMPTS}" ]; then | ||
| echo "${description} failed after ${attempt} attempts" | ||
| return 1 | ||
| fi | ||
|
|
||
| retry_wait_seconds="${BACKOFF_SECONDS[attempt - 1]}" | ||
| attempt=$((attempt + 1)) | ||
|
|
||
| echo "retrying in ${retry_wait_seconds}s..." | ||
| sleep "${retry_wait_seconds}" | ||
| done | ||
| } | ||
|
|
||
| docker_login() { | ||
| echo "${DOCKERHUB_TOKEN}" | docker login --username "${DOCKERHUB_USERNAME}" --password-stdin | ||
| } | ||
|
|
||
| if [ -n "${DOCKERHUB_USERNAME:-}" ] && [ -n "${DOCKERHUB_TOKEN:-}" ]; then | ||
| export -f docker_login # makes the function docker_login available in any child process spawned from it | ||
| retry_with_timeout "Logging in to Docker Hub" 60 bash -c docker_login | ||
| fi | ||
|
|
||
| retry_with_timeout "Running docker compose pull" 900 docker compose --progress plain pull |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.