Skip to content

Build and publish container image #409

Build and publish container image

Build and publish container image #409

Workflow file for this run

name: Build and publish container image
on:
# run when tagging or pushing to master
push:
branches: [master]
tags: ["*"]
permissions:
packages: write # ghcr access
contents: write # create releases
env:
# GitHub Container Registry hostname
GHCR_HOSTNAME: ghcr.io
jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout the latest SHA for this branch
uses: actions/checkout@v4
if: github.event_name != 'workflow_dispatch'
- name: Checkout the specified tag
uses: actions/checkout@v4
if: github.event_name == 'workflow_dispatch'
with:
ref: ${{ github.event.inputs.tag }}
- name: Get the latest Dockerfile if needed
if: github.event_name == 'workflow_dispatch'
run: |
if [[ ! -f Dockerfile ]]; then
git fetch --depth=1 origin master
git checkout origin/master -- Dockerfile
fi
- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Prepare
id: prep
run: |
VERSION="${{ github.event.inputs.tag }}"
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION="${GITHUB_REF/refs\/tags\//}"
fi
if [[ -z "$VERSION" ]]; then
VERSION="ref-${GITHUB_SHA::8}"
fi
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
- name: Generate images meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.GHCR_HOSTNAME }}/${{ github.repository }}
tags: |
type=raw,value=${{ steps.prep.outputs.VERSION }}
- name: Build the container image
uses: docker/build-push-action@v5
with:
push: true
builder: ${{ steps.buildx.outputs.name }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max