Skip to content

Publish Docker Image #2

Publish Docker Image

Publish Docker Image #2

name: Publish Docker Image
on:
workflow_call:
inputs:
environment:
type: string
required: false
description: 'The environment to publish the Docker image to.'
tags:
type: string
required: true
description: 'The tags to apply to the Docker image.'
images:
type: string
required: true
description: 'The images to publish'
workflow_dispatch:
jobs:
build-and-push-image:
name: Build and Push Docker Image
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
environment: ${{ inputs.environment }}
steps:
- name: Checkout Repo
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
- name: Log in to Docker Hub
uses: docker/login-action@v3
# Only log in to Docker Hub if the event is a release
if: ${{ inputs.environment == 'docker-publish' }}
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
# default to ghcr.io for workflow_dispatch
images: ${{ inputs.images || format('ghcr.io/{0}', github.repository) }}
# use the branch + sha if workflow_dispatch
tags: ${{ inputs.tags || format('type=raw,value={0}-{1}', github.ref_name, github.sha) }}
- name: Push to Registry(s)
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
provenance: false
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}