Skip to content

Commit de4e8a4

Browse files
committed
ci: cache Go and image layers in the connectors preview build
The preview build compiled javo for amd64 and arm64 from an empty Go cache (about 5 minutes) and rebuilt every image stage. Now: - Go's build and module caches are restored before the compile, keyed on Go version, go.mod/go.sum and the week, falling back to older weeks and to main's go-cross cache, and saved when the key was not an exact hit. - The Go tarball is cached; its sha256 is checked either way. - javo compiles for amd64 only (cross-build-javo.sh takes JAVO_ARCHES): the image is linux/amd64 and never used the arm64 binaries. - BuildKit keeps every stage's layers in the Actions cache (mode=max), scoped per branch. No cross-branch fallback: a branch can read only its own and main's caches, and main does not build this image. - The concurrency group is per branch, so a push no longer cancels another branch's build. - ci-fast-preview-image builds too, to measure the cached build. Build flags and the Dockerfile are unchanged.
1 parent b24131f commit de4e8a4

2 files changed

Lines changed: 58 additions & 7 deletions

File tree

‎.github/workflows/connectors-image.yml‎

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ name: Connectors preview image
55
# work on a cluster before it is released. Not signed; not a release.
66
on:
77
push:
8-
branches: [connectors-image, connectors-memory-image]
8+
branches: [connectors-image, connectors-memory-image, ci-fast-preview-image]
99

1010
permissions:
1111
contents: read
1212

13+
# One build at a time per branch: a newer push cancels that branch's older
14+
# build, never another branch's.
1315
concurrency:
14-
group: connectors-image
16+
group: connectors-image-${{ github.ref }}
1517
cancel-in-progress: true
1618

1719
env:
@@ -37,20 +39,63 @@ jobs:
3739
"https://api.github.com/repos/$GITHUB_REPOSITORY/tarball/$GITHUB_SHA" |
3840
tar -xz --strip-components=1
3941
42+
# The Go release tarball, kept between runs; checked against GO_SHA256
43+
# whether it was downloaded or restored.
44+
- name: Cache the Go tarball
45+
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
46+
with:
47+
path: ${{ runner.temp }}/go-dl
48+
key: go-dl-${{ env.GO_VERSION }}-${{ env.GO_SHA256 }}
49+
4050
- name: Install Go
4151
run: |
42-
curl -fsSLo "$RUNNER_TEMP/go.tgz" "https://dl.google.com/go/$GO_VERSION.linux-amd64.tar.gz"
43-
echo "$GO_SHA256 $RUNNER_TEMP/go.tgz" | sha256sum -c -
44-
tar -xzf "$RUNNER_TEMP/go.tgz" -C "$RUNNER_TEMP"
52+
mkdir -p "$RUNNER_TEMP/go-dl"
53+
tgz="$RUNNER_TEMP/go-dl/$GO_VERSION.linux-amd64.tar.gz"
54+
[ -f "$tgz" ] || curl -fsSLo "$tgz" "https://dl.google.com/go/$GO_VERSION.linux-amd64.tar.gz"
55+
echo "$GO_SHA256 $tgz" | sha256sum -c -
56+
tar -xzf "$tgz" -C "$RUNNER_TEMP"
4557
echo "$RUNNER_TEMP/go/bin" >> "$GITHUB_PATH"
4658
echo "GOTOOLCHAIN=local" >> "$GITHUB_ENV"
4759
60+
# Go's build and module caches. The key changes with the Go version,
61+
# go.mod/go.sum and the week, so a source change restores a warm cache
62+
# and only its own packages compile. Falls back to an older week, then
63+
# to the go-cross cache that go.yml saves on main (same paths, both
64+
# architectures), which this branch can read.
65+
- name: Cache week
66+
id: week
67+
run: echo "week=$(date -u +%G-W%V)" >> "$GITHUB_OUTPUT"
68+
- name: Restore the Go caches
69+
id: go-cache
70+
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
71+
with:
72+
path: |
73+
~/.cache/go-build
74+
~/go/pkg/mod/cache/download
75+
key: go-preview-${{ runner.os }}-${{ env.GO_VERSION }}-${{ hashFiles('go.mod', 'go.sum') }}-${{ steps.week.outputs.week }}
76+
restore-keys: |
77+
go-preview-${{ runner.os }}-${{ env.GO_VERSION }}-${{ hashFiles('go.mod', 'go.sum') }}-
78+
go-preview-${{ runner.os }}-${{ env.GO_VERSION }}-
79+
go-cross-${{ runner.os }}-${{ hashFiles('go.mod', 'go.sum') }}-
80+
go-cross-${{ runner.os }}-
81+
82+
# amd64 only: the image is linux/amd64 and uses no other architecture.
4883
- name: Compile javo
4984
env:
5085
JAVO_VERSION: connectors-preview
5186
JAVO_COMMIT: ${{ github.sha }}
87+
JAVO_ARCHES: amd64
5288
run: scripts/build/cross-build-javo.sh "$RUNNER_TEMP/javo-bin"
5389

90+
- name: Save the Go caches
91+
if: steps.go-cache.outputs.cache-hit != 'true'
92+
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
93+
with:
94+
path: |
95+
~/.cache/go-build
96+
~/go/pkg/mod/cache/download
97+
key: ${{ steps.go-cache.outputs.cache-primary-key }}
98+
5499
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
55100
with:
56101
driver-opts: image=${{ env.BUILDKIT_IMAGE }}
@@ -70,6 +115,11 @@ jobs:
70115
platforms: linux/amd64
71116
push: true
72117
tags: ${{ env.IMAGE }}:connectors-${{ github.sha }}
118+
# Layers of every stage kept in the Actions cache, per branch: a
119+
# branch can read only its own caches and main's, and main does not
120+
# build this image, so there is no shared scope to fall back to.
121+
cache-from: type=gha,scope=connectors-image-${{ github.ref_name }}
122+
cache-to: type=gha,mode=max,scope=connectors-image-${{ github.ref_name }},ignore-error=true
73123

74124
- name: Digest
75125
env:

‎scripts/build/cross-build-javo.sh‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
# Compile javo and javo-sandbox for linux/amd64 and linux/arm64 at once into
33
# <dir>/linux_<arch>/, the layout of the Dockerfile's javo-bin stage.
44
#
5-
# Usage: [JAVO_VERSION=v0.2.22] [JAVO_COMMIT=<full sha>] scripts/build/cross-build-javo.sh <dir>
5+
# Usage: [JAVO_VERSION=v0.2.22] [JAVO_COMMIT=<full sha>] [JAVO_ARCHES="amd64"] scripts/build/cross-build-javo.sh <dir>
66
# JAVO_VERSION and JAVO_COMMIT are the release tag and commit the UI shows
77
# (internal/buildinfo); unset, the build says "dev" and knows no commit.
8+
# JAVO_ARCHES narrows the architectures, for an image of only some of them.
89
set -eu
910
dir=${1:?output folder}
1011
pkg=github.com/nebari-dev/javo/internal/buildinfo
1112
ldflags="-s -w -X $pkg.version=${JAVO_VERSION:-} -X $pkg.commit=${JAVO_COMMIT:-}"
1213
pids=""
13-
for arch in amd64 arm64; do
14+
for arch in ${JAVO_ARCHES:-amd64 arm64}; do
1415
CGO_ENABLED=0 GOOS=linux GOARCH=$arch \
1516
go build -trimpath -ldflags="$ldflags" -o "$dir/linux_$arch/javo" ./cmd/javo &
1617
pids="$pids $!"

0 commit comments

Comments
 (0)