Skip to content

Commit

Permalink
Utilize GitHub Actions cache for image builds
Browse files Browse the repository at this point in the history
Signed-off-by: Jose R. Gonzalez <[email protected]>
  • Loading branch information
komish committed Oct 10, 2024
1 parent 36380eb commit 754d628
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
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 @@ -38,7 +38,7 @@ RUN set -e && yum clean all && rm -rf /var/cache/yum/* \
&& yum clean all \
&& rm -rf /var/cache/yum

FROM registry.access.redhat.com/ubi8/ubi:8.9-1107 as base
FROM registry.access.redhat.com/ubi8/ubi:8.9-1107 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

0 comments on commit 754d628

Please sign in to comment.