Skip to content

Commit 67938eb

Browse files
committed
hack: use bake to build buildkit images
Signed-off-by: CrazyMax <[email protected]>
1 parent 0396f3e commit 67938eb

File tree

4 files changed

+97
-150
lines changed

4 files changed

+97
-150
lines changed

.github/workflows/buildkit.yml

Lines changed: 67 additions & 33 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
@@ -188,16 +167,30 @@ jobs:
188167
strategy:
189168
fail-fast: false
190169
matrix:
191-
target-stage:
170+
target:
192171
- ''
193172
- rootless
194173
steps:
195174
-
196175
name: Checkout
197176
uses: actions/checkout@v4
177+
with:
178+
fetch-depth: 0
198179
-
199-
name: Expose GitHub Runtime
200-
uses: crazy-max/ghaction-github-runtime@v3
180+
name: Prepare
181+
run: |
182+
if [ -n "${{ matrix.target }}" ]; then
183+
echo "TAG_SUFFIX=-${{ matrix.target }}" >> $GITHUB_ENV
184+
fi
185+
if [[ $GITHUB_REF == refs/tags/v* ]]; then
186+
if [[ "${GITHUB_REF#refs/tags/}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
187+
if [ -n "${{ matrix.target }}" ]; then
188+
echo "TAG_LATEST=${{ matrix.target }}" >> $GITHUB_ENV
189+
else
190+
echo "TAG_LATEST=latest" >> $GITHUB_ENV
191+
fi
192+
fi
193+
fi
201194
-
202195
name: Set up QEMU
203196
uses: docker/setup-qemu-action@v3
@@ -208,23 +201,64 @@ jobs:
208201
version: ${{ env.SETUP_BUILDX_VERSION }}
209202
driver-opts: image=${{ env.SETUP_BUILDKIT_IMAGE }}
210203
buildkitd-flags: --debug
204+
-
205+
name: Docker meta
206+
id: meta
207+
uses: docker/metadata-action@v5
208+
with:
209+
images: |
210+
${{ env.IMAGE_NAME }}
211+
# versioning strategy
212+
## push semver tag v0.24.0
213+
### moby/buildkit:v0.24.0
214+
### moby/buildkit:latest
215+
### moby/buildkit:v0.24.0-rootless
216+
### moby/buildkit:rootless
217+
## push semver prerelease tag v0.24.0-rc1
218+
### moby/buildkit:v0.24.0-rc1
219+
### moby/buildkit:v0.24.0-rc1-rootless
220+
## push on master
221+
### moby/buildkit:master
222+
## scheduled event on master
223+
### moby/buildkit:nightly
224+
tags: |
225+
type=schedule,pattern=nightly,suffix=${{ env.TAG_SUFFIX }}
226+
type=ref,event=branch,suffix=${{ env.TAG_SUFFIX }}
227+
type=ref,event=pr,suffix=${{ env.TAG_SUFFIX }}
228+
type=semver,pattern={{raw}},suffix=${{ env.TAG_SUFFIX }}
229+
type=raw,value=${{ env.TAG_LATEST }}
230+
flavor: |
231+
latest=false
232+
annotations: |
233+
org.opencontainers.image.title=BuildKit
234+
org.opencontainers.image.vendor=Moby
235+
bake-target: meta-helper
211236
-
212237
name: Login to DockerHub
213-
if: needs.prepare.outputs.push == 'push'
238+
if: ${{ github.repository == 'moby/buildkit' && (github.event_name == 'schedule' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) }}
214239
uses: docker/login-action@v3
215240
with:
216241
username: ${{ secrets.DOCKERHUB_USERNAME }}
217242
password: ${{ secrets.DOCKERHUB_TOKEN }}
218243
-
219-
name: Build ${{ needs.prepare.outputs.tag }}
220-
run: |
221-
./hack/images "${{ needs.prepare.outputs.tag }}" "$IMAGE_NAME" "${{ needs.prepare.outputs.push }}"
244+
name: Build
245+
uses: docker/bake-action@v6
246+
with:
247+
source: .
248+
files: |
249+
./docker-bake.hcl
250+
${{ steps.meta.outputs.bake-file-tags }}
251+
${{ steps.meta.outputs.bake-file-annotations }}
252+
targets: image-cross
253+
push: ${{ github.repository == 'moby/buildkit' && (github.event_name == 'schedule' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) }}
254+
provenance: mode=max
255+
sbom: true
256+
set: |
257+
*.cache-from=type=gha,scope=image${{ matrix.target }}
258+
*.cache-to=type=gha,scope=image${{ matrix.target }}
259+
*.no-cache-filter=${{ (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) && 'buildkit-export,gobuild-base,rootless' || '' }}
222260
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 }}
261+
IMAGE_TARGET: ${{ matrix.target }}
228262

229263
scout:
230264
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)