Files
gitea-actions/docker-build-push/action.yml
T
2026-10-19 10:25:23 +02:00

219 lines
9.6 KiB
YAML

name: Docker Build & Push
description: Computes the next version (via get-next-version), builds a Docker image (optionally for a specific multi-stage target), logs in to the registry, pushes the image, and optionally tags the commit with the version.
inputs:
main_branch:
description: Name of the main branch (e.g., master or main). Forwarded to get-next-version.
required: true
major:
description: Major version number. Forwarded to get-next-version.
required: false
default: '1'
minor:
description: Minor version number. Forwarded to get-next-version.
required: false
default: '0'
image_registry:
description: Optional container registry used to build the image tag and to log in to. Forwarded to get-next-version, which defaults to gitea.livingeach.day when empty.
required: false
default: ''
image_repository:
description: "Optional image repository used to build the image tag. Forwarded to get-next-version, which defaults to the full repository path (owner/repo) when empty — that matches a scoped registry namespace like gitea.livingeach.day/owner/repo. Registries with a flat namespace can override this."
required: false
default: ''
target:
description: "Optional Dockerfile build stage/target to build (docker build --target). Also used as the default image_name, so each target gets its own registry/repository/image:version tag and Git version tag. Leave empty for a single-image Dockerfile, or use a warm-up-only target (e.g. build-all) together with push set to 'false' to prime the layer cache before building the real per-target images."
required: false
default: ''
image_name:
description: Optional image name below image_repository. When empty, target is used. Image tags are built as image_registry/image_repository/image_name:version when either image_name or target is set.
required: false
default: ''
registry_username:
description: Username (or service principal app ID) used to log in to the registry.
required: true
registry_password:
description: Password (or service principal secret) used to log in to the registry.
required: true
dockerfile:
description: Path to the Dockerfile to build.
required: false
default: Dockerfile
context:
description: Docker build context path.
required: false
default: '.'
push:
description: Whether to log in, push the built image to the registry, and (after a successful push) create and push a Git tag matching version_tag. Requires workflow permissions contents write.
required: false
default: 'true'
cache:
description: Whether to use registry-backed Docker BuildKit cache. Cache refs are stored below the computed image repository in a dedicated /cache repository and tagged with safe base/branch names; merge commits also try the merged source branch cache when it can be detected.
required: false
default: 'true'
cache_registry:
description: Optional registry to use for BuildKit cache refs. Defaults to the computed IMAGE_REGISTRY when empty.
required: false
default: ''
lfs:
description: Whether to fetch Git LFS files during checkout. Enable this for repositories that store Docker build inputs, such as WAR/ZIP artifacts, in Git LFS.
required: false
default: 'false'
platforms:
description: Comma-separated list of target platforms to build for (docker buildx build --platform), e.g. linux/amd64,linux/arm64. QEMU and Buildx are always set up so non-native platforms (even a single one, e.g. just linux/arm64) can be emulated. Push always happens inline via buildx --push; with push 'false' no image output is produced (the build only warms the cache), which is the only option anyway for more than one platform since buildx cannot load a multi-platform image into the local Docker daemon.
required: false
default: 'linux/amd64'
outputs:
version:
description: Next application version computed by get-next-version (e.g., 1.0.6 or 1.0.6-feature.1).
value: ${{ steps.next-version.outputs.version }}
version_tag:
description: Version string used for the Git tag (equals version, or target-version when target is set).
value: ${{ steps.next-version.outputs.version_tag }}
image_tag:
description: Full image tag that was built and pushed (e.g., gitea.livingeach.day/owner/repo:1.0.6, or gitea.livingeach.day/owner/repo-frontend:1.0.6 when target is set).
value: ${{ steps.next-version.outputs.image_tag }}
runs:
using: composite
steps:
- name: Checkout
uses: actions/checkout@v7
with:
lfs: ${{ inputs.lfs }}
- name: Get next version
id: next-version
uses: self:captbrick/gitea-actions/get-next-version@master
with:
main_branch: ${{ inputs.main_branch }}
major: ${{ inputs.major }}
minor: ${{ inputs.minor }}
image_registry: ${{ inputs.image_registry }}
image_repository: ${{ inputs.image_repository }}
image_name: ${{ inputs.image_name || inputs.target }}
- name: Prepare cache refs
if: inputs.cache == 'true'
shell: bash
run: |
set -Eeuo pipefail
sanitize_ref() {
local value="$1"
value="$(printf '%s' "$value" \
| tr '[:upper:]' '[:lower:]' \
| sed -E 's#[^a-z0-9._-]+#-#g; s#^-+##; s#-+$##')"
if [[ -z "$value" ]]; then
printf 'ERROR: Cache ref value cannot be empty after sanitization.\n' >&2
exit 1
fi
printf '%s' "$value"
}
detect_merge_source_ref() {
local message source
message="$(git log -1 --pretty=%B 2>/dev/null || true)"
if [[ "$message" =~ ^Merge[[:space:]]pull[[:space:]]request[[:space:]]#[0-9]+[[:space:]]from[[:space:]]([^[:space:]]+) ]]; then
source="${BASH_REMATCH[1]}"
printf '%s' "${source#*/}"
return 0
fi
if [[ "$message" =~ ^Merge[[:space:]]branch[[:space:]]\'([^\']+)\' ]]; then
printf '%s' "${BASH_REMATCH[1]}"
return 0
fi
return 1
}
base_ref="${GITHUB_BASE_REF:-$MAIN_BRANCH}"
branch_ref="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-$MAIN_BRANCH}}"
safe_base_ref="$(sanitize_ref "$base_ref")"
safe_branch_ref="$(sanitize_ref "$branch_ref")"
cache_registry="${CACHE_REGISTRY:-$IMAGE_REGISTRY}"
cache_repository="${IMAGE_REPOSITORY}/cache"
printf 'DOCKER_CACHE_REGISTRY=%s\n' "$cache_registry" >> "$GITEA_ENV"
printf 'DOCKER_BASE_CACHE=%s/%s:%s\n' "$cache_registry" "$cache_repository" "$safe_base_ref" >> "$GITEA_ENV"
printf 'DOCKER_BRANCH_CACHE=%s/%s:%s\n' "$cache_registry" "$cache_repository" "$safe_branch_ref" >> "$GITEA_ENV"
if merge_source_ref="$(detect_merge_source_ref)" && [[ -n "$merge_source_ref" && "$merge_source_ref" != "$branch_ref" && "$merge_source_ref" != "$base_ref" ]]; then
safe_merge_source_ref="$(sanitize_ref "$merge_source_ref")"
printf 'DOCKER_MERGE_SOURCE_CACHE=%s/%s:%s\n' "$cache_registry" "$cache_repository" "$safe_merge_source_ref" >> "$GITEA_ENV"
fi
env:
MAIN_BRANCH: ${{ inputs.main_branch }}
CACHE_REGISTRY: ${{ inputs.cache_registry }}
- name: Docker login
if: inputs.push == 'true' || inputs.cache == 'true'
shell: bash
run: |
set -Eeuo pipefail
if [[ "$PUSH" == 'true' ]]; then
docker login --username "$REGISTRY_USERNAME" --password "$REGISTRY_PASSWORD" "$IMAGE_REGISTRY"
fi
if [[ "$CACHE" == 'true' && "${DOCKER_CACHE_REGISTRY:-$IMAGE_REGISTRY}" != "$IMAGE_REGISTRY" ]]; then
docker login --username "$REGISTRY_USERNAME" --password "$REGISTRY_PASSWORD" "${DOCKER_CACHE_REGISTRY:-$IMAGE_REGISTRY}"
elif [[ "$CACHE" == 'true' && "$PUSH" != 'true' ]]; then
docker login --username "$REGISTRY_USERNAME" --password "$REGISTRY_PASSWORD" "$IMAGE_REGISTRY"
fi
env:
PUSH: ${{ inputs.push }}
CACHE: ${{ inputs.cache }}
REGISTRY_USERNAME: ${{ inputs.registry_username }}
REGISTRY_PASSWORD: ${{ inputs.registry_password }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Build and push image
shell: bash
run: |
set -Eeuo pipefail
args=(--build-arg "APP_VERSION=$VERSION" -f "$DOCKERFILE" --tag "$IMAGE_TAG" --platform "$PLATFORMS")
if [[ -n "$TARGET" ]]; then
args+=(--target "$TARGET")
fi
if [[ "$CACHE" == 'true' ]]; then
args+=(--cache-from "type=registry,ref=$DOCKER_BASE_CACHE")
if [[ -n "${DOCKER_MERGE_SOURCE_CACHE:-}" ]]; then
args+=(--cache-from "type=registry,ref=$DOCKER_MERGE_SOURCE_CACHE")
fi
args+=(--cache-from "type=registry,ref=$DOCKER_BRANCH_CACHE")
args+=(--cache-to "type=registry,ref=$DOCKER_BRANCH_CACHE,mode=max")
fi
if [[ "$PUSH" == 'true' ]]; then
# Push straight from buildx so the result (single image or
# multi-arch manifest list) is published to the registry.
args+=(--push)
fi
docker buildx build "${args[@]}" "$CONTEXT"
env:
DOCKERFILE: ${{ inputs.dockerfile }}
CONTEXT: ${{ inputs.context }}
TARGET: ${{ inputs.target }}
CACHE: ${{ inputs.cache }}
PUSH: ${{ inputs.push }}
PLATFORMS: ${{ inputs.platforms }}
- name: Create version tag
if: inputs.push == 'true'
shell: bash
run: |
set -Eeuo pipefail
git config user.name "gitea-actions[bot]"
git config user.email "actions@gitea.livingeach.day"
git tag "$VERSION_TAG"
git push origin "$VERSION_TAG"