Skip to content

Commit faf213d

Browse files
committed
hack: use bake to build the dockerfile frontend
Signed-off-by: CrazyMax <[email protected]>
1 parent 720251d commit faf213d

File tree

7 files changed

+145
-218
lines changed

7 files changed

+145
-218
lines changed

.github/workflows/frontend.yml

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ env:
2828
SETUP_BUILDKIT_TAG: "moby/buildkit:latest"
2929
SCOUT_VERSION: "1.13.0"
3030
IMAGE_NAME: "docker/dockerfile-upstream"
31-
PLATFORMS: "linux/386,linux/amd64,linux/arm/v7,linux/arm64,linux/mips,linux/mipsle,linux/mips64,linux/mips64le,linux/s390x,linux/ppc64le,linux/riscv64"
3231

3332
jobs:
3433
test:
@@ -44,39 +43,19 @@ jobs:
4443

4544
prepare:
4645
runs-on: ubuntu-24.04
47-
if: github.event_name != 'schedule'
4846
outputs:
49-
typ: ${{ steps.prep.outputs.typ }}
50-
push: ${{ steps.prep.outputs.push }}
51-
tag: ${{ steps.prep.outputs.tag }}
52-
tags: ${{ steps.prep.outputs.tags }}
47+
channels: ${{ steps.channels.outputs.matrix }}
5348
steps:
5449
-
55-
name: Prepare
56-
id: prep
50+
name: Channels matrix
51+
id: channels
5752
run: |
58-
TYP=master
59-
TAG=mainline
60-
PUSH=false
6153
if [[ $GITHUB_REF == refs/tags/dockerfile/* ]]; then
62-
TYP=tag
63-
TAG=${GITHUB_REF#refs/tags/}
64-
PUSH=push
65-
elif [[ $GITHUB_REF == refs/heads/* ]]; then
66-
if [ $GITHUB_REF = "refs/heads/${{ github.event.repository.default_branch }}" ]; then
67-
PUSH=push
68-
fi
69-
fi
70-
if [ "$GITHUB_REPOSITORY" != "moby/buildkit" ]; then
71-
PUSH=false
72-
fi
73-
echo "typ=${TYP}" >>${GITHUB_OUTPUT}
74-
echo "push=${PUSH}" >>${GITHUB_OUTPUT}
75-
echo "tag=${TAG}" >>${GITHUB_OUTPUT}
76-
if [ "${TYP}" = "master" ]; then
77-
echo "tags=$(jq -cn --arg tag "$TAG" '[$tag, "labs"]')" >>${GITHUB_OUTPUT}
54+
version=${GITHUB_REF#refs/tags/dockerfile/}
55+
channel=$(echo "$version" | awk -F- '{print $NF}')
56+
echo "matrix=$(jq -cn --arg channel "$channel" '[$channel]')" >>${GITHUB_OUTPUT}
7857
else
79-
echo "tags=$(jq -cn --arg tag "$TAG" '[$tag]')" >>${GITHUB_OUTPUT}
58+
echo "matrix=$(jq -cn '["mainline", "labs"]')" >>${GITHUB_OUTPUT}
8059
fi
8160
8261
image:
@@ -87,22 +66,29 @@ jobs:
8766
strategy:
8867
fail-fast: false
8968
matrix:
90-
tag: ${{ fromJson(needs.prepare.outputs.tags) }}
69+
channel: ${{ fromJson(needs.prepare.outputs.channels) }}
9170
steps:
9271
-
9372
name: Prepare
9473
run: |
95-
if [[ "${{ matrix.tag }}" = "labs" ]] || [[ "${{ matrix.tag }}" == *-labs ]]; then
96-
echo "CACHE_SCOPE=frontend-labs" >>${GITHUB_ENV}
97-
else
98-
echo "CACHE_SCOPE=frontend-mainline" >>${GITHUB_ENV}
74+
if [ "${{ matrix.channel }}" != "mainline" ]; then
75+
echo "TAG_SUFFIX=-${{ matrix.channel }}" >> $GITHUB_ENV
76+
fi
77+
if [[ $GITHUB_REF == refs/tags/dockerfile/* ]]; then
78+
version=${GITHUB_REF#refs/tags/dockerfile/}
79+
channel=$(echo "$version" | awk -F- '{print $NF}')
80+
if [[ "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
81+
echo "TAG_LATEST=latest" >> $GITHUB_ENV
82+
elif [[ "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+-$channel$ ]]; then
83+
echo "TAG_LATEST=$channel" >> $GITHUB_ENV
84+
fi
85+
echo "TAG_VERSION=${version%%-*}" >> $GITHUB_ENV
9986
fi
10087
-
10188
name: Checkout
10289
uses: actions/checkout@v4
103-
-
104-
name: Expose GitHub Runtime
105-
uses: crazy-max/ghaction-github-runtime@v3
90+
with:
91+
fetch-depth: 0
10692
-
10793
name: Set up QEMU
10894
uses: docker/setup-qemu-action@v3
@@ -113,22 +99,70 @@ jobs:
11399
version: ${{ env.SETUP_BUILDX_VERSION }}
114100
driver-opts: image=${{ env.SETUP_BUILDKIT_TAG }}
115101
buildkitd-flags: --debug
102+
-
103+
name: Docker meta
104+
id: meta
105+
uses: docker/metadata-action@v5
106+
with:
107+
images: |
108+
${{ env.IMAGE_NAME }}
109+
# versioning strategy
110+
## push tag dockerfile/1.17.0
111+
### docker/dockerfile-upstream:1.17.0
112+
### docker/dockerfile-upstream:1.17
113+
### docker/dockerfile-upstream:1
114+
### docker/dockerfile-upstream:latest
115+
## push tag dockerfile/1.17.0-labs
116+
### docker/dockerfile-upstream:1.17.0-labs
117+
### docker/dockerfile-upstream:1.17-labs
118+
### docker/dockerfile-upstream:1-labs
119+
### docker/dockerfile-upstream:labs
120+
## push prerelease tag dockerfile/1.17.0-rc1
121+
### docker/dockerfile-upstream:1.17.0-rc1
122+
## push prerelease tag dockerfile/1.17.0-rc1-labs
123+
### docker/dockerfile-upstream:1.17.0-rc1-labs
124+
## push on master
125+
### docker/dockerfile-upstream:master
126+
### docker/dockerfile-upstream:master-labs
127+
tags: |
128+
type=ref,event=branch,suffix=${{ env.TAG_SUFFIX }}
129+
type=ref,event=pr,suffix=${{ env.TAG_SUFFIX }}
130+
type=semver,pattern={{version}},value=${{ env.TAG_VERSION }},suffix=${{ env.TAG_SUFFIX }}
131+
type=semver,pattern={{major}}.{{minor}},value=${{ env.TAG_VERSION }},suffix=${{ env.TAG_SUFFIX }}
132+
type=semver,pattern={{major}},value=${{ env.TAG_VERSION }},suffix=${{ env.TAG_SUFFIX }}
133+
type=raw,value=${{ env.TAG_LATEST }}
134+
flavor: |
135+
latest=false
136+
annotations: |
137+
org.opencontainers.image.title=Dockerfile Frontend
138+
org.opencontainers.image.vendor=Moby
139+
bake-target: frontend-meta-helper
116140
-
117141
name: Login to DockerHub
118142
uses: docker/login-action@v3
119-
if: needs.prepare.outputs.push == 'push'
143+
if: ${{ github.repository == 'moby/buildkit' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/dockerfile/')) }}
120144
with:
121145
username: ${{ secrets.DOCKERHUB_USERNAME }}
122146
password: ${{ secrets.DOCKERHUB_TOKEN }}
123147
-
124148
name: Build
125-
run: |
126-
./frontend/dockerfile/cmd/dockerfile-frontend/hack/release "${{ needs.prepare.outputs.typ }}" "${{ matrix.tag }}" "$IMAGE_NAME" "${{ needs.prepare.outputs.push }}"
149+
uses: docker/bake-action@v6
150+
with:
151+
source: .
152+
files: |
153+
./docker-bake.hcl
154+
${{ steps.meta.outputs.bake-file-tags }}
155+
${{ steps.meta.outputs.bake-file-annotations }}
156+
targets: frontend-image-cross
157+
push: ${{ github.repository == 'moby/buildkit' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/dockerfile/')) }}
158+
provenance: mode=max
159+
sbom: true
160+
set: |
161+
*.cache-from=type=gha,scope=frontend-${{ matrix.channel }}
162+
*.cache-to=type=gha,scope=frontend-${{ matrix.channel }}
163+
*.no-cache-filter=${{ (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/dockerfile/')) && 'base' || '' }}
127164
env:
128-
RELEASE: ${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v') }}
129-
CACHE_FROM: type=gha,scope=${{ env.CACHE_SCOPE }}
130-
CACHE_TO: type=gha,scope=${{ env.CACHE_SCOPE }}
131-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
165+
FRONTEND_CHANNEL: ${{ matrix.channel }}
132166

133167
scout:
134168
runs-on: ubuntu-24.04
@@ -181,7 +215,6 @@ jobs:
181215
# required to create GitHub release
182216
contents: write
183217
needs:
184-
- prepare
185218
- test
186219
- image
187220
steps:

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ images:
2525
$(BUILDX_CMD) bake image
2626
IMAGE_TARGET=rootless $(BUILDX_CMD) bake image
2727

28+
.PHONY: frontends
29+
frontends:
30+
# docker/dockerfile:local and docker/dockerfile:local-labs are created on Docker
31+
$(BUILDX_CMD) bake frontend-image
32+
FRONTEND_CHANNEL=labs $(BUILDX_CMD) bake frontend-image
33+
2834
.PHONY: install
2935
install:
3036
mkdir -p $(DESTDIR)$(bindir)

docker-bake.hcl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ variable "IMAGE_TARGET" {
5050
default = null
5151
}
5252

53+
variable "FRONTEND_CHANNEL" {
54+
default = "mainline"
55+
}
56+
57+
variable "FRONTEND_BUILDTAGS" {
58+
default = null
59+
}
60+
5361
# Defines the output folder
5462
variable "DESTDIR" {
5563
default = ""
@@ -86,6 +94,9 @@ function "bindir" {
8694
target "meta-helper" {
8795
tags = [IMAGE_TARGET != null && IMAGE_TARGET != "" ? "moby/buildkit:local-${IMAGE_TARGET}" : "moby/buildkit:local"]
8896
}
97+
target "frontend-meta-helper" {
98+
tags = [FRONTEND_CHANNEL != null && FRONTEND_CHANNEL != "" && FRONTEND_CHANNEL != "mainline" ? "docker/dockerfile:local-${FRONTEND_CHANNEL}" : "docker/dockerfile:local"]
99+
}
89100

90101
target "_common" {
91102
args = {
@@ -161,6 +172,34 @@ target "image-cross" {
161172
]
162173
}
163174

175+
target "frontend-image" {
176+
inherits = ["_common", "frontend-meta-helper"]
177+
dockerfile = "./frontend/dockerfile/cmd/dockerfile-frontend/Dockerfile"
178+
args = {
179+
CHANNEL = FRONTEND_CHANNEL
180+
BUILDTAGS = FRONTEND_BUILDTAGS
181+
}
182+
output = ["type=docker"]
183+
}
184+
185+
target "frontend-image-cross" {
186+
inherits = ["frontend-image"]
187+
output = ["type=image"]
188+
platforms = [
189+
"linux/386",
190+
"linux/amd64",
191+
"linux/arm/v7",
192+
"linux/arm64",
193+
"linux/mips",
194+
"linux/mipsle",
195+
"linux/mips64",
196+
"linux/mips64le",
197+
"linux/s390x",
198+
"linux/ppc64le",
199+
"linux/riscv64"
200+
]
201+
}
202+
164203
target "integration-tests-base" {
165204
inherits = ["_common"]
166205
target = "integration-tests-base"

frontend/dockerfile/cmd/dockerfile-frontend/Dockerfile

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,34 @@ WORKDIR /src
1414
ENV GOFLAGS=-mod=vendor
1515

1616
FROM base AS version
17-
ARG CHANNEL
18-
# TODO: PKG should be inferred from go modules
19-
RUN --mount=target=. \
20-
PKG=github.com/moby/buildkit/frontend/dockerfile/cmd/dockerfile-frontend VERSION=$(./frontend/dockerfile/cmd/dockerfile-frontend/hack/detect "$CHANNEL") REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi) \
21-
&& echo "-X main.Version=${VERSION} -X main.Revision=${REVISION} -X main.Package=${PKG}" | tee /tmp/.ldflags \
22-
&& echo -n "${VERSION}" | tee /tmp/.version
17+
ARG CHANNEL=mainline
18+
ARG BUILDTAGS
19+
RUN --mount=target=. <<EOT
20+
set -e
21+
tagsFile="./frontend/dockerfile/release/$CHANNEL/tags"
22+
if [ ! -f "$tagsFile" ]; then
23+
echo "No build tags found for $CHANNEL."
24+
exit 1
25+
fi
26+
if [ "$CHANNEL" = "mainline" ]; then
27+
VERSION=$(git describe --always --tags --match "dockerfile/[0-9]*")
28+
else
29+
VERSION=$(git describe --always --tags --match "dockerfile/[0-9]*-$CHANNEL")
30+
fi
31+
PKG=github.com/moby/buildkit/frontend/dockerfile/cmd/dockerfile-frontend
32+
REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi)
33+
echo "-X main.Version=${VERSION} -X main.Revision=${REVISION} -X main.Package=${PKG}" | tee /tmp/.ldflags
34+
echo -n "$BUILDTAGS $(cat $tagsFile)" | tee /tmp/.buildtags
35+
EOT
2336

2437
FROM base AS build
2538
RUN apk add --no-cache file
26-
ARG BUILDTAGS=""
2739
ARG TARGETPLATFORM
2840
RUN --mount=target=. --mount=type=cache,target=/root/.cache \
2941
--mount=target=/go/pkg/mod,type=cache \
3042
--mount=source=/tmp/.ldflags,target=/tmp/.ldflags,from=version \
31-
CGO_ENABLED=0 xx-go build -o /dockerfile-frontend -ldflags "-d $(cat /tmp/.ldflags)" -tags "$BUILDTAGS netgo static_build osusergo" ./frontend/dockerfile/cmd/dockerfile-frontend && \
43+
--mount=source=/tmp/.buildtags,target=/tmp/.buildtags,from=version \
44+
CGO_ENABLED=0 xx-go build -o /dockerfile-frontend -ldflags "-d $(cat /tmp/.ldflags)" -tags "$(cat /tmp/.buildtags) netgo static_build osusergo" ./frontend/dockerfile/cmd/dockerfile-frontend && \
3245
xx-verify --static /dockerfile-frontend
3346

3447
FROM scratch AS release

frontend/dockerfile/cmd/dockerfile-frontend/hack/detect

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

0 commit comments

Comments
 (0)