Merge pull request #150 from gerlero/ci #364
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
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 (default) | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.IMAGES }} | |
tags: | | |
type=raw,value=latest,enable={{is_default_branch}} | |
flavor: latest=false | |
- name: Docker meta (run) | |
id: meta_run | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.IMAGES }} | |
tags: | | |
type=raw,value=run,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 (default) | |
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 (run) | |
uses: docker/build-push-action@v6 | |
with: | |
platforms: ${{ env.PLATFORMS }} | |
tags: ${{ steps.meta_run.outputs.tags }} | |
labels: ${{ steps.meta_run.outputs.labels }} | |
target: run | |
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: run | |
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: run | |
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 |