| 1 | #!/usr/bin/env bash |
| 2 | # Install apt packages on a GitHub-hosted Ubuntu runner with every wait bounded. |
| 3 | # |
| 4 | # `apt-get update` has no overall deadline. When the regional Azure mirror was |
| 5 | # unreachable from one runner it sat silent until the 30-minute job limit |
| 6 | # cancelled "Mobile runtime smoke" twice on the same commit (runs 37673026751 |
| 7 | # and 37673063356), while sibling jobs on other runners finished the same step |
| 8 | # in under a minute. A stall is now a failed attempt, the unreachable mirror is |
| 9 | # dropped in favour of the fallbacks the image already lists, and a package |
| 10 | # that still cannot be installed fails the step with apt's own error. |
| 11 | set -euo pipefail |
| 12 | |
| 13 | if [ "$#" -eq 0 ]; then |
| 14 | echo "usage: ci-apt-install.sh PACKAGE..." >&2 |
| 15 | exit 2 |
| 16 | fi |
| 17 | |
| 18 | mirrors=${CI_APT_MIRRORLIST:-/etc/apt/apt-mirrors.txt} |
| 19 | update_seconds=${CI_APT_UPDATE_SECONDS:-120} |
| 20 | install_seconds=${CI_APT_INSTALL_SECONDS:-600} |
| 21 | retry_sleep=${CI_APT_RETRY_SLEEP:-15} |
| 22 | apt_options=( |
| 23 | -o Acquire::http::Timeout=30 |
| 24 | -o Acquire::https::Timeout=30 |
| 25 | -o Acquire::Retries=2 |
| 26 | -o DPkg::Lock::Timeout=120 |
| 27 | ) |
| 28 | |
| 29 | updated=0 |
| 30 | for attempt in 1 2 3 4 5; do |
| 31 | if sudo timeout --kill-after=10 "$update_seconds" apt-get "${apt_options[@]}" update; then |
| 32 | updated=1 |
| 33 | break |
| 34 | fi |
| 35 | echo "apt-get update failed or stalled (attempt $attempt of 5)" |
| 36 | if [ -f "$mirrors" ] && grep -q 'azure\.archive\.ubuntu\.com' "$mirrors"; then |
| 37 | kept=$(grep -v 'azure\.archive\.ubuntu\.com' "$mirrors" || true) |
| 38 | if printf '%s\n' "$kept" | grep -Eq '^https?://'; then |
| 39 | echo "Dropping the Azure mirror from $mirrors; remaining mirrors:" |
| 40 | printf '%s\n' "$kept" | sudo tee "$mirrors" |
| 41 | fi |
| 42 | fi |
| 43 | sleep "$retry_sleep" |
| 44 | done |
| 45 | if [ "$updated" != 1 ]; then |
| 46 | echo "::warning::apt-get update never completed; installing from the package lists already on the image" |
| 47 | fi |
| 48 | sudo timeout --kill-after=10 "$install_seconds" apt-get "${apt_options[@]}" install -y "$@" |
| 49 |