Skip to content

Commit a7954fc

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

File tree

4 files changed

+83
-149
lines changed

4 files changed

+83
-149
lines changed

.github/workflows/buildkit.yml

Lines changed: 53 additions & 32 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
@@ -195,9 +174,23 @@ jobs:
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,51 @@ 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+
tags: |
212+
type=schedule,pattern=nightly,suffix=${{ env.TAG_SUFFIX }}
213+
type=ref,event=branch,suffix=${{ env.TAG_SUFFIX }}
214+
type=ref,event=pr,suffix=${{ env.TAG_SUFFIX }}
215+
type=semver,pattern={{raw}},suffix=${{ env.TAG_SUFFIX }}
216+
type=raw,value=${{ env.TAG_LATEST }}
217+
flavor: |
218+
latest=false
219+
annotations: |
220+
org.opencontainers.image.title=BuildKit
221+
org.opencontainers.image.vendor=Moby
222+
bake-target: meta-helper
211223
-
212224
name: Login to DockerHub
213-
if: needs.prepare.outputs.push == 'push'
225+
if: ${{ github.repository == 'moby/buildkit' && (github.event_name == 'schedule' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) }}
214226
uses: docker/login-action@v3
215227
with:
216228
username: ${{ secrets.DOCKERHUB_USERNAME }}
217229
password: ${{ secrets.DOCKERHUB_TOKEN }}
218230
-
219-
name: Build ${{ needs.prepare.outputs.tag }}
220-
run: |
221-
./hack/images "${{ needs.prepare.outputs.tag }}" "$IMAGE_NAME" "${{ needs.prepare.outputs.push }}"
231+
name: Build
232+
uses: docker/bake-action@v6
233+
with:
234+
source: .
235+
files: |
236+
./docker-bake.hcl
237+
${{ steps.meta.outputs.bake-file-tags }}
238+
${{ steps.meta.outputs.bake-file-annotations }}
239+
targets: image-cross
240+
push: ${{ github.repository == 'moby/buildkit' && (github.event_name == 'schedule' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) }}
241+
provenance: mode=max
242+
sbom: true
243+
set: |
244+
*.cache-from=type=gha,scope=image${{ matrix.target-stage }}
245+
*.cache-to=type=gha,scope=image${{ matrix.target-stage }}
246+
*.no-cache-filter=${{ (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) && 'buildkit-export,gobuild-base,rootless' || '' }}
222247
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 }}
248+
IMAGE_TARGET: ${{ matrix.target }}
228249

229250
scout:
230251
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)