Allows callers to specify a Docker build target stage (e.g. target: production) for multi-stage Dockerfiles. Empty default preserves backward compatibility. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
119 lines
4.2 KiB
YAML
119 lines
4.2 KiB
YAML
# Reusable workflow: Build Docker image and push to Gitea registry
|
|
# Usage: uses: wectrl-net/ci-templates/.gitea/workflows/build-push.yaml@main
|
|
name: Build & Push Docker Image
|
|
|
|
on:
|
|
workflow_call:
|
|
inputs:
|
|
image-name:
|
|
description: "Full image name (e.g. git.wectrl.net/wectrl-net/my-service)"
|
|
required: true
|
|
type: string
|
|
context:
|
|
description: "Docker build context path"
|
|
required: false
|
|
type: string
|
|
default: "."
|
|
dockerfile:
|
|
description: "Path to Dockerfile (relative to context)"
|
|
required: false
|
|
type: string
|
|
default: "Dockerfile"
|
|
platforms:
|
|
description: "Target platforms (e.g. linux/arm64, linux/amd64)"
|
|
required: false
|
|
type: string
|
|
default: "linux/arm64"
|
|
build-args:
|
|
description: "Docker build args (newline-separated KEY=VALUE)"
|
|
required: false
|
|
type: string
|
|
default: ""
|
|
target:
|
|
description: "Docker build target stage (for multi-stage builds)"
|
|
required: false
|
|
type: string
|
|
default: ""
|
|
runner:
|
|
description: "Runner label to use (e.g. ubuntu-latest, self-hosted-arm64). ARM64 builds use QEMU emulation on amd64 runners by default — set this to a native ARM64 runner for faster Rust/heavy builds."
|
|
required: false
|
|
type: string
|
|
default: "ubuntu-latest"
|
|
environment-tag:
|
|
description: "Mutable environment tag to push (e.g. prod, staging, dev). If empty, auto-detected from branch: main/master→prod, staging→staging, dev→dev."
|
|
required: false
|
|
type: string
|
|
default: ""
|
|
version-tag:
|
|
description: "Pinned version tag to push (e.g. dev-v123). Used for node-specific testing."
|
|
required: false
|
|
type: string
|
|
default: ""
|
|
secrets:
|
|
REGISTRY_USER:
|
|
required: true
|
|
REGISTRY_TOKEN:
|
|
required: true
|
|
outputs:
|
|
image-tag:
|
|
description: "The sha-based image tag that was pushed"
|
|
value: ${{ jobs.build.outputs.image-tag }}
|
|
|
|
jobs:
|
|
build:
|
|
name: Build & Push
|
|
runs-on: ${{ inputs.runner }}
|
|
container:
|
|
image: catthehacker/ubuntu:act-22.04@sha256:52581951350bf4f1137d44883626850bdfa35a8e5318b95dcb22226caece3bc9
|
|
options: --privileged
|
|
outputs:
|
|
image-tag: ${{ steps.tag.outputs.tag }}
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Compute image tag
|
|
id: tag
|
|
run: |
|
|
SHORT_SHA="${{ gitea.sha }}"
|
|
SHORT_SHA="${SHORT_SHA:0:7}"
|
|
echo "tag=sha-${SHORT_SHA}" >> "$GITEA_OUTPUT"
|
|
|
|
- name: Log in to Gitea registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: git.wectrl.net
|
|
username: ${{ secrets.REGISTRY_USER }}
|
|
password: ${{ secrets.REGISTRY_TOKEN }}
|
|
|
|
- name: Docker metadata
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ inputs.image-name }}
|
|
tags: |
|
|
type=sha,prefix=sha-
|
|
type=raw,value=prod,enable=${{ gitea.ref == 'refs/heads/main' || gitea.ref == 'refs/heads/master' }}
|
|
type=raw,value=staging,enable=${{ gitea.ref == 'refs/heads/staging' }}
|
|
type=raw,value=dev,enable=${{ gitea.ref == 'refs/heads/dev' }}
|
|
type=raw,value=${{ inputs.environment-tag }},enable=${{ inputs.environment-tag != '' }}
|
|
type=raw,value=${{ inputs.version-tag }},enable=${{ inputs.version-tag != '' }}
|
|
type=semver,pattern={{version}}
|
|
type=semver,pattern={{major}}.{{minor}}
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Build and push
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: ${{ inputs.context }}
|
|
file: ${{ inputs.context }}/${{ inputs.dockerfile }}
|
|
target: ${{ inputs.target || '' }}
|
|
platforms: ${{ inputs.platforms }}
|
|
push: true
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
build-args: ${{ inputs.build-args }}
|
|
cache-from: type=registry,ref=${{ inputs.image-name }}:buildcache
|
|
cache-to: type=registry,ref=${{ inputs.image-name }}:buildcache,mode=max
|