workflows/docker: build and publish ARM64 Docker images #3255
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
name: Docker | |
on: | |
pull_request: | |
push: | |
branches: | |
- master | |
merge_group: | |
release: | |
types: | |
- published | |
permissions: | |
contents: read | |
defaults: | |
run: | |
shell: bash -xeuo pipefail {0} | |
jobs: | |
build: | |
if: github.repository_owner == 'Homebrew' | |
name: docker (${{ matrix.arch }} Ubuntu ${{ matrix.version }}) | |
runs-on: ${{ matrix.arch == 'arm64' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }} | |
strategy: | |
fail-fast: false | |
matrix: | |
version: ["18.04", "20.04", "22.04", "24.04"] | |
arch: ["x86_64", "arm64"] | |
outputs: | |
tags: ${{ steps.attributes.outputs.tags }} | |
push: ${{ steps.attributes.outputs.push }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
with: | |
fetch-depth: 0 | |
persist-credentials: false | |
- name: Fetch origin/master from Git | |
run: git fetch origin master | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@f7ce87c1d6bead3e36075b2ce75da1f6cc28aaca # v3.9.0 | |
with: | |
cache-binary: false | |
- name: Determine build attributes | |
id: attributes | |
run: | | |
date="$(date --rfc-3339=seconds --utc)" | |
brew_version="$(git describe --tags --dirty --abbrev=7)" | |
DELIMITER="END_LABELS_$(uuidgen)" | |
cat <<EOS | tee -a "${GITHUB_OUTPUT}" | |
labels<<${DELIMITER} | |
org.opencontainers.image.created=${date} | |
org.opencontainers.image.url=https://brew.sh | |
org.opencontainers.image.documentation=https://docs.brew.sh | |
org.opencontainers.image.source=https://github.com/${GITHUB_REPOSITORY} | |
org.opencontainers.image.version=${brew_version} | |
org.opencontainers.image.revision=${GITHUB_SHA} | |
org.opencontainers.image.vendor=${GITHUB_REPOSITORY_OWNER} | |
org.opencontainers.image.licenses=BSD-2-Clause | |
${DELIMITER} | |
EOS | |
tags=() | |
if [[ "${GITHUB_EVENT_NAME}" == "release" ]]; then | |
tags+=( | |
"ghcr.io/homebrew/ubuntu${{matrix.version}}:${brew_version}" | |
"ghcr.io/homebrew/ubuntu${{matrix.version}}:latest" | |
"homebrew/ubuntu${{matrix.version}}:${brew_version}" | |
"homebrew/ubuntu${{matrix.version}}:latest" | |
) | |
if [[ "${{ matrix.version }}" == "22.04" ]]; then | |
tags+=( | |
"ghcr.io/homebrew/brew:${brew_version}" | |
"ghcr.io/homebrew/brew:latest" | |
"homebrew/brew:${brew_version}" | |
"homebrew/brew:latest" | |
) | |
fi | |
elif [[ "${GITHUB_EVENT_NAME}" == "push" && | |
"${GITHUB_REF}" == "refs/heads/master" && | |
"${{ matrix.version }}" == "22.04" ]]; then | |
tags+=( | |
"ghcr.io/homebrew/brew:master" | |
"ghcr.io/homebrew/ubuntu${{ matrix.version }}:master" | |
"homebrew/brew:master" | |
"homebrew/ubuntu${{matrix.version}}:master" | |
) | |
fi | |
if [[ "${{ matrix.version }}" == "18.04" ]]; then | |
# odeprecated: remove this in Homebrew >=4.5 | |
echo "The homebrew/ubuntu18.04 image is deprecated and will soon be retired. Use homebrew/ubuntu22.04 or homebrew/ubuntu24.04 or homebrew/ubuntu20.04 or homebrew/brew." > .docker-deprecate | |
fi | |
{ | |
if [[ "${#tags[@]}" -ne 0 ]]; then | |
DELIMITER="END_TAGS_$(uuidgen)" | |
echo "tags<<${DELIMITER}" | |
printf "%s\n" "${tags[@]}" | |
echo "${DELIMITER}" | |
echo "push=true" | |
else | |
echo "push=false" | |
fi | |
} | tee -a "${GITHUB_OUTPUT}" | |
- name: Log in to GitHub Packages (github-actions[bot]) | |
uses: docker/#-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 | |
with: | |
registry: ghcr.io | |
username: github-actions[bot] | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build Docker image | |
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6.13.0 | |
with: | |
context: . | |
load: true | |
tags: brew | |
cache-from: type=registry,ref=ghcr.io/homebrew/ubuntu${{ matrix.version }}:cache | |
build-args: version=${{ matrix.version }} | |
labels: ${{ steps.attributes.outputs.labels }} | |
- name: Run brew test-bot --only-setup | |
# TODO: Remove this conditional when `brew doctor` no longer throws an error on ARM64 Linux. | |
if: matrix.arch == 'x86_64' | |
run: docker run --rm brew brew test-bot --only-setup | |
- name: Log in to GitHub Packages (BrewTestBot) | |
if: steps.attributes.outputs.push == 'true' | |
uses: docker/#-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 | |
with: | |
registry: ghcr.io | |
username: BrewTestBot | |
password: ${{ secrets.HOMEBREW_BREW_GITHUB_PACKAGES_TOKEN }} | |
- name: Deploy the Docker image by digest | |
id: digest | |
if: steps.attributes.outputs.push == 'true' | |
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6.13.0 | |
with: | |
context: . | |
cache-from: type=registry,ref=ghcr.io/homebrew/ubuntu${{ matrix.version }}:cache | |
cache-to: type=registry,ref=ghcr.io/homebrew/ubuntu${{ matrix.version }}:cache,mode=max | |
build-args: version=${{ matrix.version }} | |
labels: ${{ steps.attributes.outputs.labels }} | |
outputs: type=image,name=ghcr.io/homebrew/ubuntu${{ matrix.version }},name-canonical=true,push=true,push-by-digest=true | |
- name: Export the Docker image digest | |
if: steps.attributes.outputs.push == 'true' | |
run: | | |
mkdir -p "${RUNNER_TEMP}"/digests | |
echo "${DIGEST#sha256:}" >"${RUNNER_TEMP}/digests/${VERSION}-${ARCH}" | |
env: | |
DIGEST: ${{ steps.digest.outputs.digest }} | |
VERSION: ${{ matrix.version }} | |
ARCH: ${{ matrix.arch }} | |
- name: Upload the Docker image digest | |
if: steps.attributes.outputs.push == 'true' | |
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0 | |
with: | |
name: digest-${{ matrix.version }}-${{ matrix.arch }} | |
path: ${{ runner.temp }}/digests/* | |
merge: | |
needs: build | |
if: github.repository_owner == 'Homebrew' && needs.build.outputs.push == 'true' | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
matrix: | |
version: ["18.04", "20.04", "22.04", "24.04"] | |
steps: | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0 | |
with: | |
cache-binary: false | |
- name: Download Docker image digests | |
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 | |
with: | |
path: ${{ runner.temp }}/digests | |
pattern: digest-${{ matrix.version }}-* | |
merge-multiple: true | |
- name: Log in to Docker Hub | |
uses: docker/#-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 | |
with: | |
username: brewtestbot | |
password: ${{ secrets.HOMEBREW_BREW_DOCKER_TOKEN }} | |
- name: Log in to GitHub Packages (BrewTestBot) | |
uses: docker/#-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 | |
with: | |
registry: ghcr.io | |
username: BrewTestBot | |
password: ${{ secrets.HOMEBREW_BREW_GITHUB_PACKAGES_TOKEN }} | |
- name: Merge and push Docker image | |
run: | | |
tag_args=() | |
while IFS=$'\n' read -r tag; do | |
[[ -n "${tag}" ]] || continue | |
tag_args+=("--tag=${tag}") | |
done <<<"${TAGS}" | |
docker buildx imagetools create \ | |
"${tag_args[@]}" \ | |
"ghcr.io/homebrew/ubuntu${VERSION}@sha256:$(cat "${RUNNER_TEMP}/digests/${VERSION}-x86_64")" \ | |
"ghcr.io/homebrew/ubuntu${VERSION}@sha256:$(cat "${RUNNER_TEMP}/digests/${VERSION}-arm64")" | |
env: | |
TAGS: ${{ needs.build.outputs.tags }} | |
VERSION: ${{ matrix.version }} |