Refactor Docker image workflow for versioning and builds

This commit is contained in:
Luke Gustafson
2025-10-20 16:22:11 -05:00
committed by GitHub
parent 300e0a263f
commit ad1864f062

View File

@@ -3,103 +3,50 @@ name: Build and Push Docker Image
on: on:
workflow_dispatch: workflow_dispatch:
inputs: inputs:
tag_name: version:
description: "Custom tag name for the Docker image" description: "Version to build, e.g. 1.8.0"
required: false
default: ""
registry:
description: "Docker registry to push to"
required: true required: true
default: "ghcr" production:
type: choice description: "Set true for prod build, false for dev build"
options: required: true
- "ghcr" default: false
- "dockerhub"
jobs: jobs:
build: build:
runs-on: blacksmith-4vcpu-ubuntu-2404 runs-on: blacksmith-4vcpu-ubuntu-2404
steps: steps:
- name: Checkout repository - uses: actions/checkout@v5
uses: actions/checkout@v5
with: with:
fetch-depth: 1 fetch-depth: 1
- name: Set up QEMU - uses: docker/setup-qemu-action@v3
uses: docker/setup-qemu-action@v3
with: with:
platforms: arm64 platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
- name: Setup Blacksmith Builder - uses: useblacksmith/setup-docker-builder@v1
uses: useblacksmith/setup-docker-builder@v1
- name: Cache npm dependencies - uses: docker/login-action@v3
uses: actions/cache@v4
with:
path: |
~/.npm
node_modules
*/*/node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.ref_name }}-${{ hashFiles('docker/Dockerfile') }}
restore-keys: |
${{ runner.os }}-buildx-${{ github.ref_name }}-
${{ runner.os }}-buildx-
- name: Login to GitHub Container Registry
if: github.event.inputs.registry != 'dockerhub'
uses: docker/login-action@v3
with: with:
registry: ghcr.io registry: ghcr.io
username: lukegus username: lukegus
password: ${{ secrets.GHCR_TOKEN }} password: ${{ secrets.GHCR_TOKEN }}
- name: Login to Docker Hub - uses: docker/login-action@v3
if: github.event.inputs.registry == 'dockerhub' if: ${{ github.event.inputs.production == 'true' }}
uses: docker/login-action@v3
with: with:
username: bugattiguy527 username: bugattiguy527
password: ${{ secrets.DOCKERHUB_TOKEN }} password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Determine Docker image tag - name: Build and Push to GHCR
run: |
REPO_OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')
echo "REPO_OWNER=$REPO_OWNER" >> $GITHUB_ENV
if [ "${{ github.event.inputs.tag_name }}" != "" ]; then
IMAGE_TAG="${{ github.event.inputs.tag_name }}"
elif [ "${{ github.ref }}" == "refs/heads/main" ]; then
IMAGE_TAG="latest"
elif [ "${{ github.ref }}" == "refs/heads/development" ]; then
IMAGE_TAG="development-latest"
else
IMAGE_TAG="${{ github.ref_name }}"
fi
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
if [ "${{ github.event.inputs.registry }}" == "dockerhub" ]; then
echo "REGISTRY=docker.io" >> $GITHUB_ENV
echo "IMAGE_NAME=bugattiguy527/termix" >> $GITHUB_ENV
else
echo "REGISTRY=ghcr.io" >> $GITHUB_ENV
echo "IMAGE_NAME=lukegus/termix" >> $GITHUB_ENV
fi
- name: Build and Push Multi-Arch Docker Image
uses: useblacksmith/build-push-action@v2 uses: useblacksmith/build-push-action@v2
with: with:
context: . context: .
file: ./docker/Dockerfile file: ./docker/Dockerfile
push: true push: true
platforms: linux/amd64,linux/arm64 platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} tags: |
${{ github.event.inputs.production == 'true' && format('ghcr.io/lukegus/termix:release-{0}', github.event.inputs.version) || format('ghcr.io/lukegus/termix:dev-{0}', github.event.inputs.version) }}
${{ github.event.inputs.production == 'true' && 'ghcr.io/lukegus/termix:latest' || '' }}
labels: | labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }} org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }} org.opencontainers.image.revision=${{ github.sha }}
@@ -108,8 +55,26 @@ jobs:
BUILDKIT_CONTEXT_KEEP_GIT_DIR=1 BUILDKIT_CONTEXT_KEEP_GIT_DIR=1
outputs: type=registry,compression=zstd,compression-level=19 outputs: type=registry,compression=zstd,compression-level=19
- name: Cleanup Docker Images Locally - name: Build and Push to Docker Hub
if: always() if: ${{ github.event.inputs.production == 'true' }}
run: | uses: useblacksmith/build-push-action@v2
with:
context: .
file: ./docker/Dockerfile
push: true
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
tags: |
docker.io/bugattiguy527/termix:release-${{ github.event.inputs.version }}
docker.io/bugattiguy527/termix:latest
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
build-args: |
BUILDKIT_INLINE_CACHE=1
BUILDKIT_CONTEXT_KEEP_GIT_DIR=1
outputs: type=registry,compression=zstd,compression-level=19
- run: |
docker image prune -af docker image prune -af
docker system prune -af --volumes docker system prune -af --volumes
if: always()