Skip to content
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

Utilize GitHub Actions cache for image builds #104

Merged
merged 1 commit into from
Oct 15, 2024
Merged
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
57 changes: 56 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,53 @@ on:
- main

jobs:
build-cache:
strategy:
matrix:
arch:
- amd64
- arm64
- ppc64le
- s390x
platform: [linux]
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install Go
uses: actions/setup-go@v4
with:
go-version-file: "go.mod"

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

# The Dockerfile expects that ansible-operator will be copied from the
# build context. We stub it here and then don't cache it. When Goreleaser
# runs, it should copy over the right file.
- name: Create ansible-operator stub
run: echo stubbed-in-ci >> ansible-operator

- name: Build
uses: docker/build-push-action@v6
with:
context: .
platforms: "${{ matrix.platform }}/${{ matrix.arch }}"
push: false
tags: "cache:${{ matrix.platform }}-${{ matrix.arch }}"
file: images/ansible-operator/Dockerfile
cache-from: type=gha,scope=${{ matrix.arch }}
cache-to: type=gha,mode=max,scope=${{ matrix.arch }}
no-cache-filters: final # don't cache this because the stubbed ansible-operator bin was copied in.

goreleaser:
name: goreleaser
needs: [build-cache]
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
Expand All @@ -38,6 +83,10 @@ jobs:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
id: setup-buildx
uses: docker/setup-buildx-action@v3

- name: Set the release related variables
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
Expand All @@ -56,7 +105,13 @@ jobs:
echo IMAGE_TAG="$(git describe --tags --always)" >> $GITHUB_ENV
fi

# This gives us ACTIONS_RUNTIME_TOKEN and ACTIONS_CACHE_URL
# for use in goreleaser
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v3

- name: Run goreleaser
run: make release
env:
GITHUB_TOKEN: ${{ github.token }}
BUILDX_BUILDER: ${{ steps.setup-buildx.outputs.name }}
12 changes: 12 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ dockers:
use: buildx
build_flag_templates:
- "--platform=linux/amd64"
- "--cache-from"
- "type=gha,scope=amd64,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
- "--builder={{ .Env.BUILDX_BUILDER }}"
extra_files:
- "images/ansible-operator/Pipfile"
- "images/ansible-operator/Pipfile.lock"
Expand All @@ -37,6 +40,9 @@ dockers:
use: buildx
build_flag_templates:
- "--platform=linux/arm64"
- "--cache-from"
- "type=gha,scope=arm64,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
- "--builder={{ .Env.BUILDX_BUILDER }}"
extra_files:
- "images/ansible-operator/Pipfile"
- "images/ansible-operator/Pipfile.lock"
Expand All @@ -48,6 +54,9 @@ dockers:
use: buildx
build_flag_templates:
- "--platform=linux/ppc64le"
- "--cache-from"
- "type=gha,scope=ppc64le,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
- "--builder={{ .Env.BUILDX_BUILDER }}"
extra_files:
- "images/ansible-operator/Pipfile"
- "images/ansible-operator/Pipfile.lock"
Expand All @@ -59,6 +68,9 @@ dockers:
use: buildx
build_flag_templates:
- "--platform=linux/s390x"
- "--cache-from"
- "type=gha,scope=s390x,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
- "--builder={{ .Env.BUILDX_BUILDER }}"
extra_files:
- "images/ansible-operator/Pipfile"
- "images/ansible-operator/Pipfile.lock"
Expand Down
4 changes: 2 additions & 2 deletions images/ansible-operator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ RUN set -e && dnf clean all && rm -rf /var/cache/dnf/* \
&& dnf clean all \
&& rm -rf /var/cache/dnf

FROM registry.access.redhat.com/ubi9/ubi:9.4-1214.1726694543 as base
FROM registry.access.redhat.com/ubi9/ubi:9.4-1214.1726694543 AS base
ARG TARGETARCH

# Label this image with the repo and commit that built it, for freshmaking purposes.
Expand Down Expand Up @@ -68,7 +68,7 @@ RUN curl -L -o /tini https://github.com/krallin/tini/releases/download/${TINI_VE
&& chmod +x /tini && /tini --version

# Final image.
FROM base
FROM base AS final

ENV HOME=/opt/ansible \
USER_NAME=ansible \
Expand Down
Loading