Skip to content

Added Docker Hub login #8

Added Docker Hub login

Added Docker Hub login #8

Workflow file for this run

# https://docs.docker.com/build/ci/github-actions/multi-platform/
name: CI
on:
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows
push:
tags:
# match on year.month[.day]: YY.MM[.dd]
# https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet
- '[2-9][0-9].[0-1][0-9].?[0-3]?[0-9]?'
env:
REGISTRY_IMAGE: erhhung/al2023-devops
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm64
steps:
# https://github.com/actions/checkout
- name: Checkout Source Code
id: checkout
uses: actions/checkout@v4
- name: Set Environment Variables
id: env
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-an-environment-variable
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/variables#default-environment-variables
run: |
platform=${{ matrix.platform }}
cat <<EOF >> $GITHUB_ENV
PLATFORM_PAIR=${platform//\//-}
EOF
# https://github.com/docker/setup-qemu-action
- name: Set up QEMU Emulator
id: qemu
uses: docker/setup-qemu-action@v3
with:
platforms: ${{ matrix.platform }}
# https://github.com/docker/setup-buildx-action
- name: Set up Docker BuildX
id: buildx
uses: docker/setup-buildx-action@v3
with:
platforms: ${{ matrix.platform }}
# https://github.com/docker/metadata-action
- name: Extract Metadata for Docker
id: metadata
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
# https://github.com/aws-actions/configure-aws-credentials
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: us-east-1
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# https://github.com/aws-actions/amazon-ecr-login
- name: Log in to ECR Public
id: ecr
uses: aws-actions/amazon-ecr-login@v2
with:
registry-type: public
# https://github.com/docker/login-action
- name: Log in to Docker Hub
id: dockerhub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
# https://github.com/docker/build-push-action
- name: Build and Push by Digest
id: build
uses: docker/build-push-action@v6
with:
platforms: ${{ matrix.platform }}
labels: ${{ steps.metadata.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
# create 0-byte file named /tmp/digests/<digest>
- name: Export Digest
id: export
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
# https://github.com/actions/upload-artifact
- name: Upload Digest
id: upload
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: ubuntu-latest
needs:
- build
steps:
# https://github.com/actions/download-artifact
- name: Download Digests
id: download
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true
# https://github.com/docker/setup-buildx-action
- name: Set up Docker BuildX
id: buildx
uses: docker/setup-buildx-action@v3
# https://github.com/docker/metadata-action
- name: Extract Metadata for Docker
id: metadata
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
# https://github.com/docker/login-action
- name: Log in to Docker Hub
id: dockerhub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Create Manifests and Push
id: manifests
working-directory: /tmp/digests
run: |
tags=($(jq -cr '[.tags[] | "-t \(.)"] | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON"))
images=($(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *))
docker buildx imagetools create "${tags[@]}" "${images[@]}"
# confirm merged image manifests
- name: Inspect Image
id: inspect
run: |
tag="${{ env.REGISTRY_IMAGE }}:$DOCKER_METADATA_OUTPUT_VERSION"
docker buildx imagetools inspect $tag