Publish Docker image workflow (Diaoul/subliminal)
The Publish Docker image workflow from Diaoul/subliminal, explained and optimized by Latchkey.
CI health: C - fair
Point runs-on at Latchkey and get run de-duplication, job timeouts, SHA-pinned actions, self-healing for flaky steps, and up to 58% lower cost, applied automatically.
What it does
This is the Publish Docker image workflow from the Diaoul/subliminal repository, a real project running GitHub Actions. It is shown here with attribution under its MIT license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
name: Publish Docker image
on:
push:
branches:
- main
tags:
- "*.*.*"
workflow_call:
inputs:
tag-name:
description: 'Tag name'
required: true
type: 'string'
workflow_dispatch:
inputs:
tag-name:
description: 'Tag name'
required: true
type: 'string'
pull_request:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
TEST_IMAGE: subliminal
jobs:
build-docker:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
if: github.event_name == 'push' || github.event_name == 'pull_request'
uses: actions/checkout@v7
with:
persist-credentials: false
- name: Checkout repository
if: github.event_name == 'workflow_call' || github.event_name == 'workflow_dispatch'
uses: actions/checkout@v7
with:
ref: ${{ inputs.tag-name }}
persist-credentials: false
- name: Set up QEMU
if: |
github.event_name == 'push'
|| github.event_name == 'workflow_call'
|| github.event_name == 'workflow_dispatch'
uses: docker/setup-qemu-action@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
with:
cache-binary: false
- name: Log in to the Container registry
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/0.') }}
- name: Test building Docker image
uses: docker/build-push-action@v7
if: github.event_name == 'pull_request'
with:
context: .
load: true
tags: ${{ env.TEST_IMAGE }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push Docker image
uses: docker/build-push-action@v7
if: |
github.event_name == 'push'
|| github.event_name == 'workflow_call'
|| github.event_name == 'workflow_dispatch'
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
# vim:ts=2:sw=2:et
The same workflow, on Latchkey
Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.
name: Publish Docker image on: push: branches: - main tags: - "*.*.*" workflow_call: inputs: tag-name: description: 'Tag name' required: true type: 'string' workflow_dispatch: inputs: tag-name: description: 'Tag name' required: true type: 'string' pull_request: env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} TEST_IMAGE: subliminal concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: build-docker: timeout-minutes: 30 runs-on: latchkey-small permissions: contents: read packages: write steps: - name: Checkout repository if: github.event_name == 'push' || github.event_name == 'pull_request' uses: actions/checkout@v7 with: persist-credentials: false - name: Checkout repository if: github.event_name == 'workflow_call' || github.event_name == 'workflow_dispatch' uses: actions/checkout@v7 with: ref: ${{ inputs.tag-name }} persist-credentials: false - name: Set up QEMU if: | github.event_name == 'push' || github.event_name == 'workflow_call' || github.event_name == 'workflow_dispatch' uses: docker/setup-qemu-action@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v4 with: cache-binary: false - name: Log in to the Container registry uses: docker/login-action@v4 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata (tags, labels) for Docker id: meta uses: docker/metadata-action@v6 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=branch type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/0.') }} - name: Test building Docker image uses: docker/build-push-action@v7 if: github.event_name == 'pull_request' with: context: . load: true tags: ${{ env.TEST_IMAGE }} cache-from: type=gha cache-to: type=gha,mode=max - name: Build and push Docker image uses: docker/build-push-action@v7 if: | github.event_name == 'push' || github.event_name == 'workflow_call' || github.event_name == 'workflow_dispatch' with: context: . platforms: linux/amd64,linux/arm64,linux/arm/v7 push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max # vim:ts=2:sw=2:et
What changed
- Run on Latchkey managed runners with one line (
runs-on), which apply the fixes below automatically and self-heal transient failures. This example useslatchkey-small; pick the runner size that fits the job. - Cancel superseded runs when a branch or PR gets a newer push.
- Add a job timeout so a hung step cannot burn hours of runner time.
5 third-party actions are referenced by a movable tag. Pin them to the commit SHA (Latchkey resolves and applies this automatically) so a repointed tag cannot change what runs.
What Latchkey heals here
This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:
- Container pulls and builds
This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.