Skip to content

Instantly share code, notes, and snippets.

@timtorChen
Last active February 5, 2021 09:27
Show Gist options
  • Save timtorChen/30d5ad2e014b2d4876d19797e1e02847 to your computer and use it in GitHub Desktop.
Save timtorChen/30d5ad2e014b2d4876d19797e1e02847 to your computer and use it in GitHub Desktop.
[Template] Github action build multi-arch docker images
name: Build multi-arch docker images
on:
push:
branches:
- "master"
tags:
- "v*.*.*"
jobs:
build:
runs-on: ubuntu-latest
steps:
# Change IMAGE="username/repo" to match your project
# Three kinds of image tags will be generated:
# master, latest, v*.*.*
- name: Prepare
id: prep
run: |
TAGS=""
IMAGE="username/repo"
if [[ "$GITHUB_REF" == refs/heads/master ]]; then
TAGS="$IMAGE:master"
elif [[ "$GITHUB_REF" == refs/tags/* ]]; then
VERSION="${GITHUB_REF#refs/tags/}"
TAGS="$IMAGE:$VERSION,$IMAGE:latest"
fi
echo "📚 building ref: $GITHUB_REF"
echo "🏷 image tags: $TAGS"
echo ::set-output name=tags::${TAGS}
- name: Checkout
uses: actions/checkout@v2
- name: Setup QEMU
uses: docker/setup-qemu-action@v1
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v1
with:
version: latest
driver-opts: image=moby/buildkit:master
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login Docker hub
uses: docker/login-action@v1
# Remember to add DOCKER_USERNAME, DOCKER_TOKEN to your project secrets
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
# Change parameters to match your project
with:
context: . # here
file: /path/to/Dockerfile # here
platforms: linux/amd64,linux/arm64,linux/armv7
push: true # here
tags: ${{ steps.prep.outputs.tags }} # here
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment