Skip to content
Open
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
73 changes: 73 additions & 0 deletions .github/actions/apt-install/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

name: Install apt packages
description: >
apt-get update and install for the GitHub Actions jobs: the apt timers of
the runner image are stopped first, every attempt has a time budget, and a
failed attempt is retried against the canonical Ubuntu archive instead of
the runner's mirror list. See install.sh for the details.

inputs:
packages:
description: Packages to install, separated by spaces.
required: true
attempts:
description: How many times to try before the step fails.
required: false
default: "3"
update-timeout:
description: Seconds one apt-get update may take.
required: false
default: "45"
install-timeout:
description: Seconds one apt-get install may take.
required: false
default: "120"

runs:
using: composite
steps:
- name: Configure apt retries and timeouts
shell: bash
run: sudo cp "$GITHUB_ACTION_PATH/../../apt/99-ci-network" /etc/apt/apt.conf.d/

# The runner image boots with apt-daily.timer and apt-daily-upgrade.timer
# armed. Either one can start apt-daily.service (or the upgrade service,
# which runs unattended-upgrade) right under our apt-get and hold the dpkg
# lock for minutes. Disarm the timers so nothing new starts, then wait for
# a run that is already going instead of killing it mid-dpkg.
- name: Stop the apt timers
shell: bash
run: |
sudo systemctl disable --now apt-daily.timer apt-daily-upgrade.timer 2>/dev/null || true
# Bound the wait: an in-flight unattended-upgrade must not eat the
# job's budget before apt-get even starts. timeout runs under sudo so
# that it can signal the root systemd-run directly.
sudo timeout 120 systemd-run --quiet --wait \
--property=After=apt-daily.service \
--property=After=apt-daily-upgrade.service \
true 2>/dev/null || true

- name: apt-get update and install
shell: bash
env:
PACKAGES: ${{ inputs.packages }}
ATTEMPTS: ${{ inputs.attempts }}
UPDATE_TIMEOUT: ${{ inputs.update-timeout }}
INSTALL_TIMEOUT: ${{ inputs.install-timeout }}
run: "$GITHUB_ACTION_PATH/install.sh"
100 changes: 100 additions & 0 deletions .github/actions/apt-install/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# apt-get update and install with a time budget per attempt and a mirror
# fallback. Driven by action.yml; reads its parameters from the environment:
#
# PACKAGES packages to install, separated by spaces
# ATTEMPTS how many times to try before giving up
# UPDATE_TIMEOUT seconds one apt-get update may take
# INSTALL_TIMEOUT seconds one apt-get install may take
#
# Why a budget: .github/apt/99-ci-network already bounds a mirror that stops
# answering, but a mirror that keeps trickling bytes never trips apt's
# inactivity timeout, and apt-get then runs until the job's timeout-minutes
# cancels it, with nothing in the log to say what it was waiting for. The
# `timeout` here ends such an attempt itself, and the retry gets a fair chance
# at a different mirror.
#
# Why a fallback mirror: the runner images resolve the Ubuntu archive through
# a mirror list (mirror+file:/etc/apt/apt-mirrors.txt, Azure first). From the
# second attempt on, the sources point straight at archive.ubuntu.com, so a
# bad day on the Azure mirror costs one attempt, not the job.

set -u

read -r -d '' -a packages <<< "$PACKAGES"
archive=https://archive.ubuntu.com/ubuntu/

# One attempt: update, then install. Both run under `timeout`, which sends
# SIGTERM at the deadline and SIGKILL ten seconds later.
run_apt() {
sudo env DEBIAN_FRONTEND=noninteractive \
timeout --kill-after=10 "$UPDATE_TIMEOUT" \
apt-get update -yq || return
sudo env DEBIAN_FRONTEND=noninteractive \
timeout --kill-after=10 "$INSTALL_TIMEOUT" \
apt-get install -y --no-install-recommends "${packages[@]}"
}

# Rewrite every apt source that goes through the mirror list so that it goes
# to the canonical archive instead. Covers the one-line sources.list of the
# ubuntu-22.04 image and the deb822 ubuntu.sources of ubuntu-24.04.
use_canonical_archive() {
local file changed=0
for file in /etc/apt/sources.list /etc/apt/sources.list.d/*.sources /etc/apt/sources.list.d/*.list; do
[ -f "$file" ] || continue
if grep -q 'mirror+file:' "$file"; then
sudo sed -i "s|mirror+file:[^ ]*|$archive|g" "$file"
changed=1
fi
done
if [ "$changed" = 1 ]; then
echo "::notice::apt sources switched to $archive"
fi
}

for ((attempt = 1; attempt <= ATTEMPTS; attempt++)); do
if [ "$attempt" -gt 1 ]; then
sleep 15
use_canonical_archive
# An install that `timeout` ended may have left dpkg half-way through
# configuring a package; apt refuses to run until that is finished.
sudo env DEBIAN_FRONTEND=noninteractive \
timeout --kill-after=10 "$INSTALL_TIMEOUT" \
dpkg --configure -a || true
fi

echo "::group::apt-get attempt $attempt of $ATTEMPTS"
run_apt
status=$?
echo "::endgroup::"

if [ "$status" = 0 ]; then
exit 0
fi
if [ "$status" = 124 ] || [ "$status" = 137 ]; then
echo "::warning::apt-get attempt $attempt of $ATTEMPTS ran out of time (update ${UPDATE_TIMEOUT}s, install ${INSTALL_TIMEOUT}s)"
else
echo "::warning::apt-get attempt $attempt of $ATTEMPTS failed with status $status"
fi
done

echo "::error::apt-get failed after $ATTEMPTS attempts"
exit 1
18 changes: 11 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -848,14 +848,18 @@ jobs:
with:
persist-credentials: false

- name: Configure apt retries and timeouts
run: sudo cp .github/apt/99-ci-network /etc/apt/apt.conf.d/

# The action stops the apt timers, bounds each attempt and falls back
# to archive.ubuntu.com, so a stalled mirror costs minutes, not the
# job. At the action's default budgets the timer wait and the apt-get
# calls of all three attempts fit inside this step, and so does one
# stalled dpkg --configure between attempts; two do not. The step timeout
# is the backstop, so a run that exceeds it fails here with the
# attempts in the log, not as a cancelled job.
- name: Install dependencies
run: |
sudo apt-get update -yq
sudo apt-get install -y --no-install-recommends $BUILD_DEPS
sudo apt-get install -y --no-install-recommends curl openssl ca-certificates
uses: $/.github/actions/apt-install
timeout-minutes: 15
with:
packages: ${{ env.BUILD_DEPS }} curl openssl ca-certificates

- name: Set up Python
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
Expand Down
13 changes: 6 additions & 7 deletions .github/workflows/sca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,13 @@ jobs:
with:
persist-credentials: false

- name: Configure apt retries and timeouts
run: sudo cp .github/apt/99-ci-network /etc/apt/apt.conf.d/

# Same apt install as lib-python in build.yml: per-attempt budgets, a
# fallback to archive.ubuntu.com, and a step timeout as the backstop.
- name: Install dependencies
run: |
sudo apt-get update -yq
# shellcheck disable=SC2086
sudo apt-get install -y --no-install-recommends g++ $BUILD_DEPS
uses: $/.github/actions/apt-install
timeout-minutes: 15
with:
packages: g++ ${{ env.BUILD_DEPS }}

- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
Expand Down
Loading