Skip to content

Update Docker workflow #383

Update Docker workflow

Update Docker workflow #383

Workflow file for this run

name: Docker
on:
push:
branches:
- main
tags:
- 'v*'
pull_request:
branches:
- main
workflow_dispatch:
inputs:
multi-platform:
description: Build multi-platform image (amd64 and arm64)
type: boolean
required: false
default: false
push:
description: Push the image to the registry
type: boolean
required: false
default: false
env:
PLATFORMS: ${{ (inputs.multi-platform || github.event_name == 'push') && 'linux/amd64,linux/arm64' || '' }}
PUSH: ${{ inputs.push || github.event_name == 'push' }}
IMAGES: ${{ vars.DOCKERHUB_REPOSITORY || github.repository }}
jobs:
docker:
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Lowercase repository name
id: lowercase
run: echo "repository=${GITHUB_REPOSITORY@L}" >> "${GITHUB_OUTPUT}"
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGES }}
tags: |
type=pep440,pattern={{version}}
type=raw,value=latest,enable={{is_default_branch}}
flavor: latest=false
- name: Docker meta (slim)
id: meta_slim
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGES }}
tags: |
type=pep440,pattern={{version}}-slim
type=raw,value=slim,enable={{is_default_branch}}
flavor: latest=false
- name: Login to GitHub Container Registry
continue-on-error: ${{ github.event_name == 'pull_request' }}
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to DockerHub
if: vars.DOCKERHUB_REPOSITORY
continue-on-error: ${{ github.event_name == 'pull_request' }}
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v6
with:
platforms: ${{ env.PLATFORMS }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: |
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-amd64
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-arm64
push: ${{ env.PUSH }}
- name: Build and push (slim)
uses: docker/build-push-action@v6
with:
platforms: ${{ env.PLATFORMS }}
tags: ${{ steps.meta_slim.outputs.tags }}
labels: ${{ steps.meta_slim.outputs.labels }}
target: slim
cache-from: |
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-amd64
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-arm64
push: ${{ env.PUSH }}
- name: Push build cache (amd64)
if: env.PUSH == 'true'
uses: docker/build-push-action@v6
with:
platforms: linux/amd64
target: slim
cache-from: |
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-amd64
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-arm64
cache-to: |
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-amd64,mode=max
- name: Push build cache (arm64)
if: env.PUSH == 'true' && env.PLATFORMS != ''
uses: docker/build-push-action@v6
with:
platforms: linux/arm64
target: slim
cache-from: |
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-amd64
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-arm64
cache-to: |
type=registry,ref=ghcr.io/${{ steps.lowercase.outputs.repository }}:buildcache-arm64,mode=max