-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from uc-cdis/feat/native-arm
Add native ARM and AMD build on different runners.
- Loading branch information
Showing
1 changed file
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
name: Build Image and Push to Registries | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
DOCKERFILE_LOCATION: | ||
required: false | ||
type: string | ||
default: "./Dockerfile" | ||
AWS_REGION: | ||
required: false | ||
type: string | ||
default: "us-east-1" | ||
AWS_ECR_REGISTRY: | ||
required: false | ||
type: string | ||
default: "707767160287.dkr.ecr.us-east-1.amazonaws.com" | ||
DOCKERFILE_BUILD_CONTEXT: | ||
required: false | ||
type: string | ||
default: "." | ||
OVERRIDE_REPO_NAME: | ||
required: false | ||
type: string | ||
default: "" | ||
OVERRIDE_TAG_NAME: | ||
required: false | ||
type: string | ||
default: "" | ||
USE_QUAY_ONLY: | ||
required: false | ||
type: boolean | ||
default: false | ||
BUILD_PLATFORMS: | ||
required: false | ||
type: string | ||
default: "linux/amd64, linux/arm64" | ||
secrets: | ||
ECR_AWS_ACCESS_KEY_ID: | ||
required: true | ||
ECR_AWS_SECRET_ACCESS_KEY: | ||
required: true | ||
QUAY_USERNAME: | ||
required: true | ||
QUAY_ROBOT_TOKEN: | ||
required: true | ||
|
||
jobs: | ||
build: | ||
name: Build Images | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
include: | ||
- platform: linux/amd64 | ||
runner: ubuntu-22.04 | ||
- platform: linux/arm64 | ||
runner: ubuntu-22.04-arm | ||
runs-on: ${{ matrix.runner || 'ubuntu-22.04' }} | ||
steps: | ||
- name: Prepare | ||
run: | | ||
platform=${{ matrix.platform }} | ||
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
persist-credentials: false | ||
fetch-depth: 0 | ||
|
||
- name: Set up QEMU | ||
uses: docker/setup-qemu-action@v3 | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/[email protected] | ||
|
||
- name: Set Variables | ||
shell: bash | ||
run: | | ||
echo "OVERRIDE_REPO_NAME = ${{ inputs.OVERRIDE_REPO_NAME }}" | ||
echo "OVERRIDE_TAG_NAME = ${{ inputs.OVERRIDE_TAG_NAME }}" | ||
if [[ -z "${{ inputs.OVERRIDE_TAG_NAME }}" ]] | ||
then | ||
echo "No OVERRIDE_TAG_NAME input provided, defaulting to current branch/tag name..." | ||
echo "IMAGE_TAG=$(echo ${GITHUB_REF#refs/*/} | tr / _)" | ||
echo "IMAGE_TAG=$(echo ${GITHUB_REF#refs/*/} | tr / _)" >> $GITHUB_ENV | ||
else | ||
echo "OVERRIDE_TAG_NAME provided, using it for IMAGE_TAG..." | ||
echo "IMAGE_TAG=${{ inputs.OVERRIDE_TAG_NAME }}" | ||
echo "IMAGE_TAG=${{ inputs.OVERRIDE_TAG_NAME }}" >> $GITHUB_ENV | ||
fi | ||
if [[ -z "${{ inputs.OVERRIDE_REPO_NAME }}" ]] | ||
then | ||
echo "No OVERRIDE_REPO_NAME input provided, defaulting to repo name..." | ||
echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | awk -F / '{print $2}')" | ||
echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | awk -F / '{print $2}')" >> $GITHUB_ENV | ||
else | ||
echo "OVERRIDE_REPO_NAME provided, using it for REPO_NAME..." | ||
echo "REPO_NAME=${{ inputs.OVERRIDE_REPO_NAME }}" | ||
echo "REPO_NAME=${{ inputs.OVERRIDE_REPO_NAME }}" >> $GITHUB_ENV | ||
fi | ||
# https://github.com/docker/login-action#quayio | ||
- name: Login to Quay.io | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: quay.io | ||
username: ${{ secrets.QUAY_USERNAME }} | ||
password: ${{ secrets.QUAY_ROBOT_TOKEN }} | ||
|
||
# https://github.com/docker/login-action#aws-public-elastic-container-registry-ecr | ||
- name: Login to ECR | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ${{ inputs.AWS_ECR_REGISTRY }} | ||
username: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }} | ||
password: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }} | ||
env: | ||
AWS_REGION: ${{ inputs.AWS_REGION }} | ||
|
||
- name: Extract metadata | ||
id: meta | ||
uses: docker/metadata-action@v5 | ||
with: | ||
images: | | ||
quay.io/cdis/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} | ||
${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} | ||
- name: Set image repositories | ||
id: set-repos | ||
run: | | ||
if [ "${{ inputs.QUAY_ONLY }}" = "true" ]; then | ||
echo "repos=quay.io/cdis/${{ env.REPO_NAME }}" >> $GITHUB_ENV | ||
else | ||
echo "repos=quay.io/cdis/${{ env.REPO_NAME }},${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}" >> $GITHUB_ENV | ||
fi | ||
- name: Build and push by digest | ||
id: build | ||
uses: docker/build-push-action@v6 | ||
# You may get ECR-push errors when first adding the workflow to a github repo. | ||
# If so, run the following in dev/qa to create the ECR repository: | ||
# qaplanetv1@cdistest_dev_admin:~$ aws ecr create-repository --repository-name "gen3/<repo name>" --image-scanning-configuration scanOnPush=true | ||
with: | ||
context: ${{ inputs.DOCKERFILE_BUILD_CONTEXT }} | ||
file: ${{ inputs.DOCKERFILE_LOCATION }} | ||
labels: ${{ steps.meta.outputs.labels }} | ||
cache-from: type=gha | ||
cache-to: type=gha,mode=max | ||
platforms: ${{ matrix.platform }} | ||
outputs: type=image,"name=${{ env.repos }}",push-by-digest=true,name-canonical=true,push=true | ||
|
||
- name: Export digest | ||
run: | | ||
mkdir -p ${{ runner.temp }}/digests | ||
digest="${{ steps.build.outputs.digest }}" | ||
touch "${{ runner.temp }}/digests/${digest#sha256:}" | ||
- name: Upload digest | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: digests-${{ env.PLATFORM_PAIR }} | ||
path: ${{ runner.temp }}/digests/* | ||
if-no-files-found: error | ||
retention-days: 1 | ||
merge: | ||
runs-on: ubuntu-latest | ||
needs: | ||
- build | ||
steps: | ||
- name: Download digests | ||
uses: actions/download-artifact@v4 | ||
with: | ||
path: ${{ runner.temp }}/digests | ||
pattern: digests-* | ||
merge-multiple: true | ||
|
||
# https://github.com/docker/login-action#quayio | ||
- name: Login to Quay.io | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: quay.io | ||
username: ${{ secrets.QUAY_USERNAME }} | ||
password: ${{ secrets.QUAY_ROBOT_TOKEN }} | ||
|
||
# https://github.com/docker/login-action#aws-public-elastic-container-registry-ecr | ||
- name: Login to ECR | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ${{ inputs.AWS_ECR_REGISTRY }} | ||
username: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }} | ||
password: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }} | ||
env: | ||
AWS_REGION: ${{ inputs.AWS_REGION }} | ||
|
||
- name: Set Variables | ||
shell: bash | ||
run: | | ||
echo "OVERRIDE_REPO_NAME = ${{ inputs.OVERRIDE_REPO_NAME }}" | ||
echo "OVERRIDE_TAG_NAME = ${{ inputs.OVERRIDE_TAG_NAME }}" | ||
if [[ -z "${{ inputs.OVERRIDE_TAG_NAME }}" ]] | ||
then | ||
echo "No OVERRIDE_TAG_NAME input provided, defaulting to current branch/tag name..." | ||
echo "IMAGE_TAG=$(echo ${GITHUB_REF#refs/*/} | tr / _)" | ||
echo "IMAGE_TAG=$(echo ${GITHUB_REF#refs/*/} | tr / _)" >> $GITHUB_ENV | ||
else | ||
echo "OVERRIDE_TAG_NAME provided, using it for IMAGE_TAG..." | ||
echo "IMAGE_TAG=${{ inputs.OVERRIDE_TAG_NAME }}" | ||
echo "IMAGE_TAG=${{ inputs.OVERRIDE_TAG_NAME }}" >> $GITHUB_ENV | ||
fi | ||
if [[ -z "${{ inputs.OVERRIDE_REPO_NAME }}" ]] | ||
then | ||
echo "No OVERRIDE_REPO_NAME input provided, defaulting to repo name..." | ||
echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | awk -F / '{print $2}')" | ||
echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | awk -F / '{print $2}')" >> $GITHUB_ENV | ||
else | ||
echo "OVERRIDE_REPO_NAME provided, using it for REPO_NAME..." | ||
echo "REPO_NAME=${{ inputs.OVERRIDE_REPO_NAME }}" | ||
echo "REPO_NAME=${{ inputs.OVERRIDE_REPO_NAME }}" >> $GITHUB_ENV | ||
fi | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v3 | ||
|
||
- name: Docker meta | ||
id: meta | ||
uses: docker/metadata-action@v5 | ||
with: | ||
images: | | ||
${{ inputs.USE_QUAY_ONLY != 'true' && format('{0}/gen3/{1}', inputs.AWS_ECR_REGISTRY, env.REPO_NAME) || '' }} | ||
quay.io/cdis/${{ env.REPO_NAME }} | ||
tags: | | ||
type=ref,event=branch | ||
type=ref,event=pr | ||
type=semver,pattern={{version}} | ||
type=semver,pattern={{major}}.{{minor}} | ||
- name: Create manifest list and push to registries | ||
working-directory: ${{ runner.temp }}/digests | ||
run: | | ||
# Push to Quay | ||
docker buildx imagetools create \ | ||
-t quay.io/cdis/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} \ | ||
$(printf 'quay.io/cdis/${{ env.REPO_NAME }}@sha256:%s ' *) | ||
# Conditionally push to ECR | ||
if [ "${{ inputs.USE_QUAY_ONLY }}" != "true" ]; then | ||
docker buildx imagetools create \ | ||
-t ${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} \ | ||
$(printf '${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}@sha256:%s ' *) | ||
fi | ||
- name: Inspect image | ||
run: | | ||
docker buildx imagetools inspect \ | ||
quay.io/cdis/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} | ||
- name: Inspect image (ECR) | ||
if: ${{ !inputs.USE_QUAY_ONLY }} | ||
run: | | ||
docker buildx imagetools inspect \ | ||
${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} |