Skip to content

Commit e1b4261

Browse files
authored
Merge pull request #6107 from crazy-max/bake-images
hack: use bake to build buildkit images
2 parents 35b3879 + 1fa57ee commit e1b4261

File tree

5 files changed

+129
-169
lines changed

5 files changed

+129
-169
lines changed

.github/workflows/buildkit.yml

Lines changed: 78 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,45 +30,24 @@ env:
3030
SETUP_BUILDKIT_IMAGE: "moby/buildkit:latest"
3131
SCOUT_VERSION: "1.13.0"
3232
IMAGE_NAME: "moby/buildkit"
33-
PLATFORMS: "linux/amd64,linux/arm/v7,linux/arm64,linux/s390x,linux/ppc64le,linux/riscv64"
3433
DESTDIR: "./bin"
3534

3635
jobs:
3736
prepare:
3837
runs-on: ubuntu-24.04
3938
outputs:
40-
tag: ${{ steps.prep.outputs.tag }}
41-
push: ${{ steps.prep.outputs.push }}
42-
platforms: ${{ steps.prep.outputs.platforms }}
39+
platforms-includes: ${{ steps.platforms.outputs.matrix }}
4340
steps:
4441
-
4542
name: Checkout
4643
uses: actions/checkout@v5
4744
-
48-
name: Prepare
49-
id: prep
50-
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}
70-
platforms=$(docker buildx bake release --print | jq -cr '.target."release".platforms')
71-
echo "platforms=$platforms" >>${GITHUB_OUTPUT}
45+
name: Platforms matrix
46+
id: platforms
47+
uses: docker/bake-action/subaction/matrix@v6
48+
with:
49+
target: release
50+
fields: platforms
7251

7352
binaries:
7453
runs-on: ubuntu-24.04
@@ -77,12 +56,12 @@ jobs:
7756
strategy:
7857
fail-fast: false
7958
matrix:
80-
platform: ${{ fromJson(needs.prepare.outputs.platforms) }}
59+
include: ${{ fromJson(needs.prepare.outputs.platforms-includes) }}
8160
steps:
8261
-
8362
name: Prepare
8463
run: |
85-
platform=${{ matrix.platform }}
64+
platform=${{ matrix.platforms }}
8665
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
8766
-
8867
name: Checkout
@@ -108,7 +87,7 @@ jobs:
10887
make release
10988
env:
11089
RELEASE: ${{ startsWith(github.ref, 'refs/tags/v') }}
111-
PLATFORMS: ${{ matrix.platform }}
90+
PLATFORMS: ${{ matrix.platforms }}
11291
CACHE_FROM: type=gha,scope=binaries-${{ env.PLATFORM_PAIR }}
11392
CACHE_TO: type=gha,scope=binaries-${{ env.PLATFORM_PAIR }}
11493
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -183,21 +162,29 @@ 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
-
196-
name: Checkout
197-
uses: actions/checkout@v5
198-
-
199-
name: Expose GitHub Runtime
200-
uses: crazy-max/ghaction-github-runtime@v3
174+
name: Prepare
175+
run: |
176+
if [ -n "${{ matrix.target }}" ]; then
177+
echo "TAG_SUFFIX=-${{ matrix.target }}" >> $GITHUB_ENV
178+
fi
179+
if [[ $GITHUB_REF == refs/tags/v* ]]; then
180+
if [[ "${GITHUB_REF#refs/tags/}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
181+
if [ -n "${{ matrix.target }}" ]; then
182+
echo "TAG_LATEST=${{ matrix.target }}" >> $GITHUB_ENV
183+
else
184+
echo "TAG_LATEST=latest" >> $GITHUB_ENV
185+
fi
186+
fi
187+
fi
201188
-
202189
name: Set up QEMU
203190
uses: docker/setup-qemu-action@v3
@@ -208,23 +195,67 @@ jobs:
208195
version: ${{ env.SETUP_BUILDX_VERSION }}
209196
driver-opts: image=${{ env.SETUP_BUILDKIT_IMAGE }}
210197
buildkitd-flags: --debug
198+
-
199+
name: Docker meta
200+
id: meta
201+
uses: docker/metadata-action@v5
202+
with:
203+
images: |
204+
${{ env.IMAGE_NAME }}
205+
# versioning strategy
206+
## push semver tag v0.24.0
207+
### moby/buildkit:v0.24.0
208+
### moby/buildkit:latest
209+
### moby/buildkit:v0.24.0-rootless
210+
### moby/buildkit:rootless
211+
## push semver prerelease tag v0.24.0-rc1
212+
### moby/buildkit:v0.24.0-rc1
213+
### moby/buildkit:v0.24.0-rc1-rootless
214+
## push on master
215+
### moby/buildkit:master
216+
### moby/buildkit:master-rootless
217+
## scheduled event on master
218+
### moby/buildkit:nightly
219+
### moby/buildkit:nightly-rootless
220+
tags: |
221+
type=schedule,pattern=nightly,suffix=${{ env.TAG_SUFFIX }}
222+
type=ref,event=branch,suffix=${{ env.TAG_SUFFIX }}
223+
type=ref,event=pr,suffix=${{ env.TAG_SUFFIX }}
224+
type=semver,pattern={{raw}},suffix=${{ env.TAG_SUFFIX }}
225+
type=raw,value=${{ env.TAG_LATEST }}
226+
flavor: |
227+
latest=false
228+
annotations: |
229+
org.opencontainers.image.title=BuildKit
230+
org.opencontainers.image.vendor=Moby
231+
bake-target: meta-helper
211232
-
212233
name: Login to DockerHub
213-
if: needs.prepare.outputs.push == 'push'
234+
if: ${{ github.repository == 'moby/buildkit' && (github.event_name == 'schedule' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) }}
214235
uses: docker/login-action@v3
215236
with:
216237
username: ${{ secrets.DOCKERHUB_USERNAME }}
217238
password: ${{ secrets.DOCKERHUB_TOKEN }}
218239
-
219-
name: Build ${{ needs.prepare.outputs.tag }}
220-
run: |
221-
./hack/images "${{ needs.prepare.outputs.tag }}" "$IMAGE_NAME" "${{ needs.prepare.outputs.push }}"
240+
name: Build
241+
uses: docker/bake-action@v6
242+
with:
243+
# FIXME: remove context once git context with query string implemented in actions-toolkit
244+
source: ${{ github.server_url }}/${{ github.repository }}.git#${{ github.ref }}
245+
files: |
246+
./docker-bake.hcl
247+
cwd://${{ steps.meta.outputs.bake-file-tags }}
248+
cwd://${{ steps.meta.outputs.bake-file-annotations }}
249+
targets: image-cross
250+
push: ${{ github.repository == 'moby/buildkit' && (github.event_name == 'schedule' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) }}
251+
provenance: mode=max,version=v1
252+
sbom: true
253+
set: |
254+
*.cache-from=type=gha,scope=image${{ matrix.target }}
255+
*.cache-to=type=gha,scope=image${{ matrix.target }}
256+
*.no-cache-filter=${{ (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) && 'buildkit-export-alpine,buildkit-export-ubuntu,gobuild-base,rootless' || '' }}
222257
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 }}
258+
IMAGE_TARGET: ${{ matrix.target }}
228259

229260
scout:
230261
runs-on: ubuntu-24.04
@@ -276,7 +307,6 @@ jobs:
276307
# required to create GitHub release
277308
contents: write
278309
needs:
279-
- prepare
280310
- test
281311
- binaries
282312
- image
@@ -301,4 +331,3 @@ jobs:
301331
with:
302332
draft: true
303333
files: ${{ env.DESTDIR }}/*
304-
name: ${{ needs.prepare.outputs.tag }}

Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ ARG AZURITE_VERSION=3.33.0
1818
ARG GOTESTSUM_VERSION=v1.9.0
1919
ARG DELVE_VERSION=v1.23.1
2020

21-
ARG GO_VERSION=1.25
21+
ARG EXPORT_BASE=alpine
2222
ARG ALPINE_VERSION=3.22
23+
ARG UBUNTU_VERSION=24.04
24+
25+
ARG GO_VERSION=1.25
2326
ARG XX_VERSION=1.7.0
2427
ARG BUILDKIT_DEBUG
25-
ARG EXPORT_BASE=alpine
2628

2729
# minio for s3 integration tests
2830
FROM quay.io/minio/minio:${MINIO_VERSION} AS minio
@@ -208,7 +210,7 @@ RUN apk add --no-cache fuse3 git openssh openssl pigz xz iptables ip6tables \
208210
COPY --link examples/buildctl-daemonless/buildctl-daemonless.sh /usr/bin/
209211
VOLUME /var/lib/buildkit
210212

211-
FROM ubuntu:24.04 AS buildkit-export-ubuntu
213+
FROM ubuntu:${UBUNTU_VERSION} AS buildkit-export-ubuntu
212214
RUN apt-get update \
213215
&& apt-get install -y --no-install-recommends \
214216
fuse3 \

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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
variable "EXPORT_BASE" {
2+
default = null
3+
}
4+
15
variable "ALPINE_VERSION" {
26
default = null
37
}
48

9+
variable "UBUNTU_VERSION" {
10+
default = null
11+
}
12+
513
variable "GO_VERSION" {
614
default = null
715
}
@@ -14,6 +22,10 @@ variable "BUILDKITD_TAGS" {
1422
default = null
1523
}
1624

25+
variable "BUILDKIT_DEBUG" {
26+
default = null
27+
}
28+
1729
variable "HTTP_PROXY" {
1830
default = null
1931
}
@@ -46,6 +58,10 @@ variable "ARCHUTIL_MULTIPLATFORM" {
4658
default = null
4759
}
4860

61+
variable "IMAGE_TARGET" {
62+
default = null
63+
}
64+
4965
# Defines the output folder
5066
variable "DESTDIR" {
5167
default = ""
@@ -78,12 +94,20 @@ function "bindir" {
7894
result = DESTDIR != "" ? DESTDIR : "./bin/${defaultdir}"
7995
}
8096

97+
# Special target: https://github.com/docker/metadata-action#bake-definition
98+
target "meta-helper" {
99+
tags = [IMAGE_TARGET != null && IMAGE_TARGET != "" ? "moby/buildkit:local-${IMAGE_TARGET}" : "moby/buildkit:local"]
100+
}
101+
81102
target "_common" {
82103
args = {
104+
EXPORT_BASE = EXPORT_BASE
83105
ALPINE_VERSION = ALPINE_VERSION
106+
UBUNTU_VERSION = UBUNTU_VERSION
84107
GO_VERSION = GO_VERSION
85108
NODE_VERSION = NODE_VERSION
86109
BUILDKITD_TAGS = BUILDKITD_TAGS
110+
BUILDKIT_DEBUG = BUILDKIT_DEBUG
87111
HTTP_PROXY = HTTP_PROXY
88112
HTTPS_PROXY = HTTPS_PROXY
89113
NO_PROXY = NO_PROXY
@@ -133,6 +157,26 @@ target "release" {
133157
output = [bindir("release")]
134158
}
135159

160+
target "image" {
161+
inherits = ["_common", "meta-helper"]
162+
target = IMAGE_TARGET
163+
cache-to = ["type=inline"]
164+
output = ["type=docker"]
165+
}
166+
167+
target "image-cross" {
168+
inherits = ["image"]
169+
output = ["type=image"]
170+
platforms = [
171+
"linux/amd64",
172+
"linux/arm/v7",
173+
"linux/arm64",
174+
"linux/s390x",
175+
"linux/ppc64le",
176+
"linux/riscv64"
177+
]
178+
}
179+
136180
target "integration-tests-base" {
137181
inherits = ["_common"]
138182
target = "integration-tests-base"

0 commit comments

Comments
 (0)