Skip to content

Commit ab6893d

Browse files
authored
Utilize GitHub Actions cache for image builds (#104)
Signed-off-by: Jose R. Gonzalez <komish@flutes.dev>
1 parent 9a6d15a commit ab6893d

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

.github/workflows/release.yml

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,53 @@ on:
1313
- main
1414

1515
jobs:
16+
build-cache:
17+
strategy:
18+
matrix:
19+
arch:
20+
- amd64
21+
- arm64
22+
- ppc64le
23+
- s390x
24+
platform: [linux]
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Check out code into the Go module directory
28+
uses: actions/checkout@v3
29+
with:
30+
fetch-depth: 0
31+
32+
- name: Install Go
33+
uses: actions/setup-go@v4
34+
with:
35+
go-version-file: "go.mod"
36+
37+
- name: Set up QEMU
38+
uses: docker/setup-qemu-action@v2
39+
40+
- name: Set up Docker Buildx
41+
uses: docker/setup-buildx-action@v3
42+
43+
# The Dockerfile expects that ansible-operator will be copied from the
44+
# build context. We stub it here and then don't cache it. When Goreleaser
45+
# runs, it should copy over the right file.
46+
- name: Create ansible-operator stub
47+
run: echo stubbed-in-ci >> ansible-operator
48+
49+
- name: Build
50+
uses: docker/build-push-action@v6
51+
with:
52+
context: .
53+
platforms: "${{ matrix.platform }}/${{ matrix.arch }}"
54+
push: false
55+
tags: "cache:${{ matrix.platform }}-${{ matrix.arch }}"
56+
file: images/ansible-operator/Dockerfile
57+
cache-from: type=gha,scope=${{ matrix.arch }}
58+
cache-to: type=gha,mode=max,scope=${{ matrix.arch }}
59+
no-cache-filters: final # don't cache this because the stubbed ansible-operator bin was copied in.
60+
1661
goreleaser:
17-
name: goreleaser
62+
needs: [build-cache]
1863
runs-on: ubuntu-latest
1964
steps:
2065
- name: Check out code into the Go module directory
@@ -38,6 +83,10 @@ jobs:
3883
- name: Set up QEMU
3984
uses: docker/setup-qemu-action@v2
4085

86+
- name: Set up Docker Buildx
87+
id: setup-buildx
88+
uses: docker/setup-buildx-action@v3
89+
4190
- name: Set the release related variables
4291
run: |
4392
if [[ $GITHUB_REF == refs/tags/* ]]; then
@@ -56,7 +105,13 @@ jobs:
56105
echo IMAGE_TAG="$(git describe --tags --always)" >> $GITHUB_ENV
57106
fi
58107
108+
# This gives us ACTIONS_RUNTIME_TOKEN and ACTIONS_CACHE_URL
109+
# for use in goreleaser
110+
- name: Expose GitHub Runtime
111+
uses: crazy-max/ghaction-github-runtime@v3
112+
59113
- name: Run goreleaser
60114
run: make release
61115
env:
62116
GITHUB_TOKEN: ${{ github.token }}
117+
BUILDX_BUILDER: ${{ steps.setup-buildx.outputs.name }}

.goreleaser.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ dockers:
2626
use: buildx
2727
build_flag_templates:
2828
- "--platform=linux/amd64"
29+
- "--cache-from"
30+
- "type=gha,scope=amd64,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
31+
- "--builder={{ .Env.BUILDX_BUILDER }}"
2932
extra_files:
3033
- "images/ansible-operator/Pipfile"
3134
- "images/ansible-operator/Pipfile.lock"
@@ -37,6 +40,9 @@ dockers:
3740
use: buildx
3841
build_flag_templates:
3942
- "--platform=linux/arm64"
43+
- "--cache-from"
44+
- "type=gha,scope=arm64,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
45+
- "--builder={{ .Env.BUILDX_BUILDER }}"
4046
extra_files:
4147
- "images/ansible-operator/Pipfile"
4248
- "images/ansible-operator/Pipfile.lock"
@@ -48,6 +54,9 @@ dockers:
4854
use: buildx
4955
build_flag_templates:
5056
- "--platform=linux/ppc64le"
57+
- "--cache-from"
58+
- "type=gha,scope=ppc64le,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
59+
- "--builder={{ .Env.BUILDX_BUILDER }}"
5160
extra_files:
5261
- "images/ansible-operator/Pipfile"
5362
- "images/ansible-operator/Pipfile.lock"
@@ -59,6 +68,9 @@ dockers:
5968
use: buildx
6069
build_flag_templates:
6170
- "--platform=linux/s390x"
71+
- "--cache-from"
72+
- "type=gha,scope=s390x,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
73+
- "--builder={{ .Env.BUILDX_BUILDER }}"
6274
extra_files:
6375
- "images/ansible-operator/Pipfile"
6476
- "images/ansible-operator/Pipfile.lock"

images/ansible-operator/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ RUN set -e && dnf clean all && rm -rf /var/cache/dnf/* \
4040
&& dnf clean all \
4141
&& rm -rf /var/cache/dnf
4242

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

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

7070
# Final image.
71-
FROM base
71+
FROM base AS final
7272

7373
ENV HOME=/opt/ansible \
7474
USER_NAME=ansible \

0 commit comments

Comments
 (0)