Skip to content

Commit 2ba5402

Browse files
committed
hack: use bake to build buildkit images
Signed-off-by: CrazyMax <[email protected]>
1 parent ec7a152 commit 2ba5402

File tree

4 files changed

+99
-151
lines changed

4 files changed

+99
-151
lines changed

.github/workflows/buildkit.yml

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ jobs:
3737
prepare:
3838
runs-on: ubuntu-24.04
3939
outputs:
40-
tag: ${{ steps.prep.outputs.tag }}
41-
push: ${{ steps.prep.outputs.push }}
4240
platforms: ${{ steps.prep.outputs.platforms }}
4341
steps:
4442
-
@@ -48,25 +46,6 @@ jobs:
4846
name: Prepare
4947
id: prep
5048
run: |
51-
TAG=pr
52-
PUSH=false
53-
if [ "${{ github.event_name }}" = "schedule" ]; then
54-
TAG=nightly
55-
PUSH=push
56-
elif [[ $GITHUB_REF == refs/tags/v* ]]; then
57-
TAG=${GITHUB_REF#refs/tags/}
58-
PUSH=push
59-
elif [[ $GITHUB_REF == refs/heads/* ]]; then
60-
TAG=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
61-
if [ $GITHUB_REF = "refs/heads/${{ github.event.repository.default_branch }}" ]; then
62-
PUSH=push
63-
fi
64-
fi
65-
if [ "$GITHUB_REPOSITORY" != "moby/buildkit" ]; then
66-
PUSH=false
67-
fi
68-
echo "tag=${TAG}" >>${GITHUB_OUTPUT}
69-
echo "push=${PUSH}" >>${GITHUB_OUTPUT}
7049
platforms=$(docker buildx bake release --print | jq -cr '.target."release".platforms')
7150
echo "platforms=$platforms" >>${GITHUB_OUTPUT}
7251
@@ -183,21 +162,34 @@ jobs:
183162
image:
184163
runs-on: ubuntu-24.04
185164
needs:
186-
- prepare
187165
- test
188166
strategy:
189167
fail-fast: false
190168
matrix:
191-
target-stage:
169+
target:
192170
- ''
193171
- rootless
194172
steps:
195173
-
196174
name: Checkout
197175
uses: actions/checkout@v5
176+
with:
177+
fetch-depth: 0
198178
-
199-
name: Expose GitHub Runtime
200-
uses: crazy-max/ghaction-github-runtime@v3
179+
name: Prepare
180+
run: |
181+
if [ -n "${{ matrix.target }}" ]; then
182+
echo "TAG_SUFFIX=-${{ matrix.target }}" >> $GITHUB_ENV
183+
fi
184+
if [[ $GITHUB_REF == refs/tags/v* ]]; then
185+
if [[ "${GITHUB_REF#refs/tags/}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
186+
if [ -n "${{ matrix.target }}" ]; then
187+
echo "TAG_LATEST=${{ matrix.target }}" >> $GITHUB_ENV
188+
else
189+
echo "TAG_LATEST=latest" >> $GITHUB_ENV
190+
fi
191+
fi
192+
fi
201193
-
202194
name: Set up QEMU
203195
uses: docker/setup-qemu-action@v3
@@ -208,23 +200,66 @@ jobs:
208200
version: ${{ env.SETUP_BUILDX_VERSION }}
209201
driver-opts: image=${{ env.SETUP_BUILDKIT_IMAGE }}
210202
buildkitd-flags: --debug
203+
-
204+
name: Docker meta
205+
id: meta
206+
uses: docker/metadata-action@v5
207+
with:
208+
images: |
209+
${{ env.IMAGE_NAME }}
210+
# versioning strategy
211+
## push semver tag v0.24.0
212+
### moby/buildkit:v0.24.0
213+
### moby/buildkit:latest
214+
### moby/buildkit:v0.24.0-rootless
215+
### moby/buildkit:rootless
216+
## push semver prerelease tag v0.24.0-rc1
217+
### moby/buildkit:v0.24.0-rc1
218+
### moby/buildkit:v0.24.0-rc1-rootless
219+
## push on master
220+
### moby/buildkit:master
221+
### moby/buildkit:master-rootless
222+
## scheduled event on master
223+
### moby/buildkit:nightly
224+
### moby/buildkit:nightly-rootless
225+
tags: |
226+
type=schedule,pattern=nightly,suffix=${{ env.TAG_SUFFIX }}
227+
type=ref,event=branch,suffix=${{ env.TAG_SUFFIX }}
228+
type=ref,event=pr,suffix=${{ env.TAG_SUFFIX }}
229+
type=semver,pattern={{raw}},suffix=${{ env.TAG_SUFFIX }}
230+
type=raw,value=${{ env.TAG_LATEST }}
231+
flavor: |
232+
latest=false
233+
annotations: |
234+
org.opencontainers.image.title=BuildKit
235+
org.opencontainers.image.vendor=Moby
236+
bake-target: meta-helper
211237
-
212238
name: Login to DockerHub
213-
if: needs.prepare.outputs.push == 'push'
239+
if: ${{ github.repository == 'moby/buildkit' && (github.event_name == 'schedule' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) }}
214240
uses: docker/login-action@v3
215241
with:
216242
username: ${{ secrets.DOCKERHUB_USERNAME }}
217243
password: ${{ secrets.DOCKERHUB_TOKEN }}
218244
-
219-
name: Build ${{ needs.prepare.outputs.tag }}
220-
run: |
221-
./hack/images "${{ needs.prepare.outputs.tag }}" "$IMAGE_NAME" "${{ needs.prepare.outputs.push }}"
245+
name: Build
246+
uses: docker/bake-action@v6
247+
with:
248+
source: .
249+
files: |
250+
./docker-bake.hcl
251+
${{ steps.meta.outputs.bake-file-tags }}
252+
${{ steps.meta.outputs.bake-file-annotations }}
253+
targets: image-cross
254+
push: ${{ github.repository == 'moby/buildkit' && (github.event_name == 'schedule' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) }}
255+
provenance: mode=max
256+
sbom: true
257+
set: |
258+
*.cache-from=type=gha,scope=image${{ matrix.target }}
259+
*.cache-to=type=gha,scope=image${{ matrix.target }}
260+
*.no-cache-filter=${{ (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) && 'buildkit-export,gobuild-base,rootless' || '' }}
222261
env:
223-
RELEASE: ${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v') }}
224-
TARGET: ${{ matrix.target-stage }}
225-
CACHE_FROM: type=gha,scope=image${{ matrix.target-stage }}
226-
CACHE_TO: type=gha,scope=image${{ matrix.target-stage }}
227-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
262+
IMAGE_TARGET: ${{ matrix.target }}
228263

229264
scout:
230265
runs-on: ubuntu-24.04

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ cross:
2222
.PHONY: images
2323
images:
2424
# moby/buildkit:local and moby/buildkit:local-rootless are created on Docker
25-
hack/images local moby/buildkit
26-
TARGET=rootless hack/images local moby/buildkit
25+
$(BUILDX_CMD) bake image
26+
IMAGE_TARGET=rootless $(BUILDX_CMD) bake image
2727

2828
.PHONY: install
2929
install:

docker-bake.hcl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ variable "ARCHUTIL_MULTIPLATFORM" {
4646
default = null
4747
}
4848

49+
variable "IMAGE_TARGET" {
50+
default = null
51+
}
52+
4953
# Defines the output folder
5054
variable "DESTDIR" {
5155
default = ""
@@ -78,6 +82,11 @@ function "bindir" {
7882
result = DESTDIR != "" ? DESTDIR : "./bin/${defaultdir}"
7983
}
8084

85+
# Special target: https://github.com/docker/metadata-action#bake-definition
86+
target "meta-helper" {
87+
tags = [IMAGE_TARGET != null && IMAGE_TARGET != "" ? "moby/buildkit:local-${IMAGE_TARGET}" : "moby/buildkit:local"]
88+
}
89+
8190
target "_common" {
8291
args = {
8392
ALPINE_VERSION = ALPINE_VERSION
@@ -133,6 +142,25 @@ target "release" {
133142
output = [bindir("release")]
134143
}
135144

145+
target "image" {
146+
inherits = ["_common", "meta-helper"]
147+
target = IMAGE_TARGET
148+
output = ["type=docker"]
149+
}
150+
151+
target "image-cross" {
152+
inherits = ["image"]
153+
output = ["type=image"]
154+
platforms = [
155+
"linux/amd64",
156+
"linux/arm/v7",
157+
"linux/arm64",
158+
"linux/s390x",
159+
"linux/ppc64le",
160+
"linux/riscv64"
161+
]
162+
}
163+
136164
target "integration-tests-base" {
137165
inherits = ["_common"]
138166
target = "integration-tests-base"

hack/images

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)