Skip to content

build multiarch

build multiarch #4

Workflow file for this run

name: Build & Push Image
on:
push:
branches:
- main
workflow_dispatch:
env:
IMAGE_NAME: aws-cli-jq
jobs:
build-and-push:
strategy:
matrix:
arch: [amd64, arm64]
fail-fast: false
runs-on: ${{ matrix.arch == 'arm64' && format('codebuild-dicoding-arm-runner-{0}-{1}', github.run_id, github.run_attempt) || 'ubuntu-latest' }}
environment: prod
permissions:
contents: read
packages: write
outputs:
branch: ${{ steps.version.outputs.branch }}
commit: ${{ steps.version.outputs.commit }}
currdate: ${{ steps.version.outputs.currdate }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
ref: '${{ github.head_ref }}'
fetch-depth: 0
- name: Generate build version
run: |
echo "branch=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_OUTPUT
echo "commit=$(git rev-parse --short=8 HEAD)" >> $GITHUB_OUTPUT
echo "currdate=$(date +'%Y.%m.%d')" >> $GITHUB_OUTPUT
id: version
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: |
dcr.dicoding.space/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ steps.version.outputs.branch }}-${{ matrix.arch }}
type=raw,value=${{ steps.version.outputs.currdate }}-${{ matrix.arch }}
type=raw,value=${{ steps.version.outputs.commit }}-${{ matrix.arch }}
type=raw,value=latest-${{ matrix.arch }}
- name: Generate build cache tag
run: |
echo "ghcr=ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:buildcache-${{ matrix.arch }}" >> $GITHUB_OUTPUT
id: buildcache
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: |
image=moby/buildkit:latest
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Dicoding Container Registry
uses: docker/login-action@v3
with:
registry: dcr.dicoding.space
username: ${{ secrets.DCR_USER }}
password: ${{ secrets.DCR_PASS }}
- name: Build and Push Docker image
id: docker_build
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
platforms: linux/${{ matrix.arch }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ steps.buildcache.outputs.ghcr }}
cache-to: type=registry,ref=${{ steps.buildcache.outputs.ghcr }},mode=max
create-and-push-manifest:
needs: build-and-push
runs-on: ubuntu-latest
environment: prod
permissions:
contents: read
packages: write
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
ref: '${{ github.head_ref }}'
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Dicoding Container Registry
uses: docker/login-action@v3
with:
registry: dcr.dicoding.space
username: ${{ secrets.DCR_USER }}
password: ${{ secrets.DCR_PASS }}
- name: Create and Push Manifest
run: |
# Define registries and tags
REGISTRIES=("ghcr.io" "dcr.dicoding.space")
TAGS=("${{ needs.build-and-push.outputs.branch }}" "${{ needs.build-and-push.outputs.currdate }}" "${{ needs.build-and-push.outputs.commit }}" "latest")
# Loop through registries and tags to create manifests
for registry in "${REGISTRIES[@]}"; do
for tag in "${TAGS[@]}"; do
# For the 'latest' tag, use branch images as source
if [ "$tag" == "latest" ]; then
source_tag="${{ needs.build-and-push.outputs.branch }}"
else
source_tag="$tag"
fi
echo "Creating manifest for $registry/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:$tag"
docker buildx imagetools create -t $registry/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:$tag \
$registry/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:$source_tag-amd64 \
$registry/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:$source_tag-arm64
done
done