Skip to content

Commit

Permalink
Add multi-platform build job
Browse files Browse the repository at this point in the history
This change adds a new build job that allows building a multi-platform
image.
  • Loading branch information
niconosenzo committed Oct 16, 2024
1 parent 77a54b7 commit 4bc4bde
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ on:
type: string
default: 'non-prod'
description: 'Runner type'
multiplatform:
required: false
type: boolean
default: false
description: 'Multiplatform build'

secrets:
API_TOKEN_GITHUB:
Expand All @@ -45,6 +50,7 @@ on:
jobs:
release:
name: Build Docker
if: ${{ ! inputs.multiplatform }}
runs-on: ${{ inputs.runner }}
steps:
- name: Setup | Checkout
Expand All @@ -64,3 +70,115 @@ jobs:
registry: ${{ inputs.registry }}
target: ${{ inputs.docker_target }}
file: ${{ inputs.dockerfile_path }}

mp-build:
name: Build multiplatform Image (${{ matrix.platform }})
if: ${{ inputs.multiplatform }}
runs-on: [ "self-hosted", "${{ matrix.runs_on }}" ]
strategy:
fail-fast: false
matrix:
platform: [ "amd64", "arm64" ]
include:
- platform: amd64
runs_on: dev-us-east-1
- platform: arm64
runs_on: arm64
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Prepare
run: |
platform_slash_pair=linux/${{ matrix.platform }}
echo "PLATFORM_DASH_PAIR=${platform_slash_pair//\//-}" >> $GITHUB_ENV
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
tags: |
type=raw,value=${{ inputs.tags }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.ORG_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.ORG_AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v2

- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true

- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

merge:
runs-on: ${{ inputs.runner }}
needs:
- mp-build
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
tags: |
type=raw,value=${{ inputs.tags }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.ORG_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.ORG_AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v2

- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ inputs.tags }}

0 comments on commit 4bc4bde

Please sign in to comment.