Skip to content

Commit 350a58b

Browse files
kadelnickboldt
andauthored
chore(ci): build multiplatform next container image (#2036)
* chore(ci): build multiplatform next container image * fix wrong conflict resolution * remove empty lines --------- Co-authored-by: Nick Boldt <[email protected]>
1 parent ce55f73 commit 350a58b

File tree

4 files changed

+145
-23
lines changed

4 files changed

+145
-23
lines changed

.github/actions/docker-build/action.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ inputs:
3636
push:
3737
description: Whether to push the image
3838
required: true
39+
platform:
40+
description: "Target given CPU platform architecture (default: linux/amd64)"
41+
required: false
42+
default: linux/amd64
43+
44+
outputs:
45+
digest:
46+
value: ${{ steps.build.outputs.digest }}
47+
3948

4049
runs:
4150
using: composite
@@ -77,10 +86,12 @@ runs:
7786
7887
- name: Build and push Docker image
7988
uses: docker/build-push-action@v5
89+
id: build
8090
with:
8191
context: .
8292
file: docker/Dockerfile
8393
push: ${{ inputs.push }}
8494
provenance: false
8595
tags: ${{ steps.meta.outputs.tags }}
8696
labels: ${{ steps.meta.outputs.labels }}
97+
platforms: ${{ inputs.platform }}

.github/actions/get-sha/action.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2024 The Janus IDP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: Get the last commit short SHA
16+
description: Get the last commit short SHA of the main branch or PR
17+
18+
outputs:
19+
short_sha:
20+
description: The short SHA of the last commit
21+
value: ${{ steps.build.outputs.sha }}
22+
23+
24+
runs:
25+
using: composite
26+
steps:
27+
- name: Get the last commit short SHA
28+
shell: bash
29+
run: |
30+
if [[ -n "${{ github.event.pull_request.head.sha }}" ]]; then
31+
# running on a PR
32+
REF="${{ github.event.pull_request.head.sha }}"
33+
REPO="${{ github.repository }}/pull/${{ github.event.number }}"
34+
else
35+
# running on a main branch
36+
# todo: handle other branches than main
37+
REF="HEAD"
38+
REPO="${{ github.repository }}"
39+
fi
40+
SHORT_SHA=$(git rev-parse --short=8 $REF)
41+
echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_ENV
42+
if [[ -f packages/app/src/build-metadata.json ]]; then
43+
now="$(date -u +%FT%TZ)"
44+
sed -i packages/app/src/build-metadata.json -r \
45+
-e 's|("Last Commit:.+)|"Last Commit: '$REPO' @ '$SHORT_SHA'"|'
46+
fi
47+
echo "short_sha=$SHORT_SHA" >> "$GITHUB_OUTPUT"

.github/workflows/next-build-image.yaml

+85-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
name: Next
15+
name: Build Next Image
1616

1717
on:
1818
push:
@@ -25,11 +25,18 @@ concurrency:
2525

2626
env:
2727
REGISTRY: quay.io
28+
REGISTRY_IMAGE: rhdh-community/rhdh
2829

2930
jobs:
3031
build-image:
3132
name: Build Image
3233
runs-on: ubuntu-latest
34+
strategy:
35+
fail-fast: false
36+
matrix:
37+
platform:
38+
- linux/amd64
39+
- linux/arm64
3340
permissions:
3441
contents: read
3542
packages: write
@@ -40,26 +47,91 @@ jobs:
4047
with:
4148
fetch-depth: 0
4249

43-
- name: Get the last commit short SHA
50+
- name: Prepare
4451
run: |
45-
SHORT_SHA=$(git rev-parse --short=8 HEAD)
46-
echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_ENV
47-
if [[ -f packages/app/src/build-metadata.json ]]; then
48-
repo="${{ github.repository }}"
49-
now="$(date -u +%FT%TZ)"
50-
sed -i packages/app/src/build-metadata.json -r \
51-
-e 's|("Last Commit:.+)|"Last Commit: '$repo' @ '$SHORT_SHA'"|'
52-
fi
52+
platform=${{ matrix.platform }}
53+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
54+
echo "PLATFORM_ARCH=${platform#*/}" >> $GITHUB_ENV
55+
56+
- name: Get the last commit short SHA
57+
uses: ./.github/actions/get-sha
58+
5359

5460
- name: Build and Push with Buildx
5561
uses: ./.github/actions/docker-build
62+
id: build
5663
with:
5764
registry: ${{ env.REGISTRY }}
5865
username: ${{ secrets.QUAY_USERNAME }}
5966
password: ${{ secrets.QUAY_TOKEN }}
60-
imageName: rhdh-community/rhdh
67+
imageName: ${{ env.REGISTRY_IMAGE }}
6168
imageTags: |
62-
type=raw,value=next
63-
type=sha,prefix=next-
69+
type=raw,value=next-${{ env.PLATFORM_ARCH }}
70+
type=sha,prefix=next-,suffix=-${{ env.PLATFORM_ARCH }}
6471
imageLabels: quay.expires-after=14d
6572
push: true
73+
platform: ${{ matrix.platform }}
74+
75+
- name: Export digest
76+
run: |
77+
mkdir -p /tmp/digests
78+
digest="${{ steps.build.outputs.digest }}"
79+
touch "/tmp/digests/${digest#sha256:}"
80+
81+
- name: Upload digest
82+
uses: actions/upload-artifact@v4
83+
with:
84+
name: digests-${{ env.PLATFORM_PAIR }}
85+
path: /tmp/digests/*
86+
if-no-files-found: error
87+
retention-days: 1
88+
89+
90+
merge:
91+
runs-on: ubuntu-latest
92+
needs:
93+
- build-image
94+
steps:
95+
- name: Checkout
96+
uses: actions/checkout@v4
97+
with:
98+
fetch-depth: 0
99+
100+
- name: Download digests
101+
uses: actions/download-artifact@v4
102+
with:
103+
path: /tmp/digests
104+
pattern: digests-*
105+
merge-multiple: true
106+
107+
- name: Get the last commit short SHA
108+
uses: ./.github/actions/get-sha
109+
110+
- name: Set up Docker Buildx
111+
uses: docker/setup-buildx-action@v3
112+
113+
- name: Docker meta
114+
id: meta
115+
uses: docker/metadata-action@v5
116+
with:
117+
images: ${{ env.REGISTRY }}/${{ env.REGISTRY_IMAGE }}
118+
tags: |
119+
type=raw,value=next
120+
type=ref,prefix=pr-,suffix=-${{ env.SHORT_SHA }},event=pr
121+
122+
- name: Login to Docker Hub
123+
uses: docker/login-action@v3
124+
with:
125+
registry: ${{ env.REGISTRY }}
126+
username: ${{ vars.QUAY_USERNAME }}
127+
password: ${{ secrets.QUAY_TOKEN }}
128+
129+
- name: Create manifest list and push
130+
working-directory: /tmp/digests
131+
run: |
132+
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
133+
$(printf '${{ env.REGISTRY }}/${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
134+
135+
- name: Inspect image
136+
run: |
137+
docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}

.github/workflows/pr-build-image.yaml

+2-10
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,7 @@ jobs:
7272
fi
7373
7474
- name: Get the last commit short SHA of the PR
75-
if: env.proceed_with_build == 'true'
76-
run: |
77-
SHORT_SHA=$(git rev-parse --short=8 ${{ github.event.pull_request.head.sha }})
78-
echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_ENV
79-
if [[ -f packages/app/src/build-metadata.json ]]; then
80-
repoPR="${{ github.repository }}/pull/${{ github.event.number }}"
81-
now="$(date -u +%FT%TZ)"
82-
sed -i packages/app/src/build-metadata.json -r \
83-
-e 's|("Last Commit:.+)|"Last Commit: '$repoPR' @ '$SHORT_SHA'"|'
84-
fi
75+
uses: ./.github/actions/get-sha
8576

8677
- name: Check if Image Already Exists
8778
if: env.proceed_with_build == 'true'
@@ -112,6 +103,7 @@ jobs:
112103
type=ref,prefix=pr-,suffix=-${{ env.SHORT_SHA }},event=pr
113104
imageLabels: quay.expires-after=14d
114105
push: true
106+
platform: linux/amd64
115107

116108
- name: Comment the image pull link
117109
if: env.proceed_with_build == 'true' && env.image_exists == 'false'

0 commit comments

Comments
 (0)