Skip to content

Commit 33ebe20

Browse files
build multiarch
1 parent a5ca074 commit 33ebe20

File tree

1 file changed

+80
-15
lines changed

1 file changed

+80
-15
lines changed

.github/workflows/build.yml

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
name: Build & Push Image
2-
'on':
2+
3+
on:
34
push:
45
branches:
56
- main
7+
workflow_dispatch:
8+
9+
env:
10+
IMAGE_NAME: aws-cli-jq
11+
612
jobs:
7-
build:
8-
runs-on: ubuntu-latest
13+
build-and-push:
14+
strategy:
15+
matrix:
16+
arch: [amd64, arm64]
17+
fail-fast: false
18+
runs-on: ${{ matrix.arch == 'arm64' && format('codebuild-dicoding-arm-runner-{0}-{1}', github.run_id, github.run_attempt) || 'ubuntu-latest' }}
919
environment: prod
1020
permissions:
11-
id-token: write
1221
contents: read
1322
packages: write
23+
outputs:
24+
branch: ${{ steps.version.outputs.branch }}
25+
commit: ${{ steps.version.outputs.commit }}
26+
currdate: ${{ steps.version.outputs.currdate }}
1427
steps:
1528
- name: Checkout Code
1629
uses: actions/checkout@v4
@@ -21,7 +34,7 @@ jobs:
2134
- name: Generate build version
2235
run: |
2336
echo "branch=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_OUTPUT
24-
echo "commit=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
37+
echo "commit=$(git rev-parse --short=8 HEAD)" >> $GITHUB_OUTPUT
2538
echo "currdate=$(date +'%Y.%m.%d')" >> $GITHUB_OUTPUT
2639
id: version
2740

@@ -30,22 +43,19 @@ jobs:
3043
uses: docker/metadata-action@v5
3144
with:
3245
images: |
33-
dcr.dicoding.space/${{ github.repository_owner }}/aws-cli-jq
34-
ghcr.io/${{ github.repository_owner }}/aws-cli-jq
46+
dcr.dicoding.space/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
47+
ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
3548
tags: |
36-
type=raw,value=${{ steps.version.outputs.branch }}
37-
type=raw,value=${{ steps.version.outputs.currdate }}
38-
type=raw,value=${{ steps.version.outputs.commit }}
39-
flavor: latest=true
49+
type=raw,value=${{ steps.version.outputs.branch }}-${{ matrix.arch }}
50+
type=raw,value=${{ steps.version.outputs.currdate }}-${{ matrix.arch }}
51+
type=raw,value=${{ steps.version.outputs.commit }}-${{ matrix.arch }}
52+
type=raw,value=latest-${{ matrix.arch }}
4053
4154
- name: Generate build cache tag
4255
run: |
43-
echo "ghcr=ghcr.io/${{ github.repository_owner }}/aws-cli-jq:buildcache" >> $GITHUB_OUTPUT
56+
echo "ghcr=ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:buildcache-${{ matrix.arch }}" >> $GITHUB_OUTPUT
4457
id: buildcache
4558

46-
- name: Set up QEMU
47-
uses: docker/setup-qemu-action@v3
48-
4959
- name: Set up Docker Buildx
5060
uses: docker/setup-buildx-action@v3
5161
with:
@@ -73,7 +83,62 @@ jobs:
7383
context: .
7484
file: ./Dockerfile
7585
push: true
86+
platforms: linux/${{ matrix.arch }}
7687
tags: ${{ steps.meta.outputs.tags }}
7788
labels: ${{ steps.meta.outputs.labels }}
7889
cache-from: type=registry,ref=${{ steps.buildcache.outputs.ghcr }}
7990
cache-to: type=registry,ref=${{ steps.buildcache.outputs.ghcr }},mode=max
91+
92+
create-and-push-manifest:
93+
needs: build-and-push
94+
runs-on: ubuntu-latest
95+
environment: prod
96+
permissions:
97+
contents: read
98+
packages: write
99+
steps:
100+
- name: Checkout Code
101+
uses: actions/checkout@v4
102+
with:
103+
ref: '${{ github.head_ref }}'
104+
fetch-depth: 0
105+
106+
- name: Set up Docker Buildx
107+
uses: docker/setup-buildx-action@v3
108+
109+
- name: Login to GitHub Container Registry
110+
uses: docker/login-action@v3
111+
with:
112+
registry: ghcr.io
113+
username: ${{ github.repository_owner }}
114+
password: ${{ secrets.GITHUB_TOKEN }}
115+
116+
- name: Login to Dicoding Container Registry
117+
uses: docker/login-action@v3
118+
with:
119+
registry: dcr.dicoding.space
120+
username: ${{ secrets.DCR_USER }}
121+
password: ${{ secrets.DCR_PASS }}
122+
123+
- name: Create and Push Manifest
124+
run: |
125+
# Define registries and tags
126+
REGISTRIES=("ghcr.io" "dcr.dicoding.space")
127+
TAGS=("${{ needs.build-and-push.outputs.branch }}" "${{ needs.build-and-push.outputs.currdate }}" "${{ needs.build-and-push.outputs.commit }}" "latest")
128+
129+
# Loop through registries and tags to create manifests
130+
for registry in "${REGISTRIES[@]}"; do
131+
for tag in "${TAGS[@]}"; do
132+
# For the 'latest' tag, use branch images as source
133+
if [ "$tag" == "latest" ]; then
134+
source_tag="${{ needs.build-and-push.outputs.branch }}"
135+
else
136+
source_tag="$tag"
137+
fi
138+
139+
echo "Creating manifest for $registry/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:$tag"
140+
docker buildx imagetools create -t $registry/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:$tag \
141+
$registry/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:$source_tag-amd64 \
142+
$registry/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:$source_tag-arm64
143+
done
144+
done

0 commit comments

Comments
 (0)