Skip to content

Add multi-architecture support and GitHub Actions workflow #2

Add multi-architecture support and GitHub Actions workflow

Add multi-architecture support and GitHub Actions workflow #2

name: Docker
on:
push:
branches: ["master"]
pull_request:
branches: ["master"]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to the Container registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }}
type=sha,format=short
type=ref,event=branch
type=ref,event=tag
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
platforms: linux/amd64,linux/arm64,linux/arm/v7
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push architecture-specific tags
if: github.event_name != 'pull_request'
run: |
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-8)
for arch in amd64 arm64 arm/v7; do
docker buildx build --platform linux/$arch \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-${arch//\//-} \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${SHORT_SHA}-${arch//\//-} \
--push \
.
done