Skip to content

ci(docker): publish docker image to GHCR #9

ci(docker): publish docker image to GHCR

ci(docker): publish docker image to GHCR #9

name: Publish to Docker
on:
push:
branches: ["main"]
tags:
- 'v*'
env:
DOCKERHUB_REGISTRY: docker.io
GHCR_REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
BUILD_PLATFORMS: linux/amd64,linux/arm64
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: ${{ env.BUILD_PLATFORMS }}
- name: Login to ${{ env.DOCKERHUB_REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.DOCKERHUB_REGISTRY }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to ${{ env.GHCR_REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.GHCR_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
# output: edge,latest,1.2.3
- name: Extract Docker metadata
id: docker-meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.DOCKERHUB_REGISTRY }}/${{ env.IMAGE_NAME }}
${{ env.GHCR_REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# TODO: edge
type=edge
# 1.2.3
type=semver,pattern={{version}}
- name: Build and push
id: build-and-push
uses: docker/build-push-action@v6
with:
context: .
tags: ${{ steps.docker-meta.outputs.tags }}
labels: ${{ steps.docker-meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: ${{ env.BUILD_PLATFORMS }}
push: true
build-args: |
VERSION=${{ fromJSON(steps.docker-meta.outputs.json).labels['org.opencontainers.image.version'] }}
REVISION=${{ fromJSON(steps.docker-meta.outputs.json).labels['org.opencontainers.image.revision'] }}