Files
actions-docker/.gitea/actions/docker-build/action.yml

118 lines
3.8 KiB
YAML

name: Docker Build + Push
description: Build and push Docker images for Gitea Actions using the docker daemon at DOCKER_HOST
author: eric
inputs:
dockerfile:
description: Path to Dockerfile (relative to build context)
default: Dockerfile
context:
description: Build context path (relative to repo root)
default: .
image-prefix:
description: Registry and org prefix (e.g. registry.io/user). Defaults to DOCKER_REGISTRY/owner.
default: ${{ secrets.DOCKER_REGISTRY }}/${{ github.repository_owner }}
image-name:
description: Image name (just the repo part, or full name if image-prefix not used)
required: true
tags:
description: Additional tags as CSV appended to defaults
default: ""
registry:
description: Docker registry for login (e.g. ghcr.io)
default: ${{ secrets.DOCKER_REGISTRY }}
registry-username:
description: Docker registry username
default: ${{ secrets.DOCKER_USERNAME }}
registry-password:
description: Docker registry password (use secrets.DOCKER_PASSWORD)
default: ${{ secrets.DOCKER_PASSWORD }}
push:
description: Push images to registry after build
default: "false"
build-args:
description: Build args as CSV KEY=VALUE
default: ""
outputs:
image:
description: Full image name with tag
value: ${{ steps.meta.outputs.image }}
tag:
description: The primary tag used (date-SHA)
value: ${{ steps.meta.outputs.tag }}
runs:
using: composite
env:
DOCKER_HOST: tcp://docker.local:2375
DOCKER_BUILDKIT: "1"
steps:
- name: Compute metadata
id: meta
shell: bash
run: |
# Generate tags
TAGS="latest"
if [ -n "${{ inputs.tags }}" ]; then
TAGS="$TAGS,${{ inputs.tags }}"
fi
SHORT_SHA="$(git rev-parse --short HEAD 2>/dev/null || echo 'local')"
DATE_TAG="$(date +%Y-%m-%d)"
TAG="$DATE_TAG-$SHORT_SHA"
TAGS="$TAGS,$TAG"
# Add git tag if this is a tag push
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
TAG_NAME="${{ github.ref_name }}"
TAGS="$TAGS,$TAG_NAME"
fi
FULL_IMAGE="${{ inputs.image-prefix }}/${{ inputs.image-name }}"
echo "image=$FULL_IMAGE" >> $GITHUB_OUTPUT
echo "tag=$TAG" >> $GITHUB_OUTPUT
echo "tags=$TAGS" >> $GITHUB_OUTPUT
- name: Log in to Docker registry
shell: bash
run: |
if [ -n "${{ inputs.registry }}" ] && [ -n "${{ inputs.registry-password }}" ]; then
echo "${{ inputs.registry-password }}" | docker login ${{ inputs.registry }} -u ${{ inputs.registry-username }} --password-stdin
fi
- name: Build image
id: build
shell: bash
run: |
BUILD_ARGS_FLAG=""
if [ -n "${{ inputs.build-args }}" ]; then
for arg in $(echo "${{ inputs.build-args }}" | tr ',' ' '); do
BUILD_ARGS_FLAG="$BUILD_ARGS_FLAG --build-arg=$arg"
done
fi
FULL_IMAGE="${{ inputs.image-prefix }}/${{ inputs.image-name }}"
set -x
docker build \
--tag "$FULL_IMAGE:${{ steps.meta.outputs.tag }}" \
--tag "$FULL_IMAGE:latest" \
$(for t in $(echo ${{ steps.meta.outputs.tags }} | tr ',' ' '); do echo -n "--tag $FULL_IMAGE:$t "; done) \
--file ${{ inputs.context }}/${{ inputs.dockerfile }} \
$BUILD_ARGS_FLAG \
${{ inputs.context }}
- name: Push image
id: push
if: inputs.push == 'true'
shell: bash
run: |
FULL_IMAGE="${{ inputs.image-prefix }}/${{ inputs.image-name }}"
set -x
docker push "$FULL_IMAGE:${{ steps.meta.outputs.tag }}"
docker push "$FULL_IMAGE:latest"
for t in $(echo ${{ steps.meta.outputs.tags }} | tr ',' ' '); do
docker push "$FULL_IMAGE:$t"
done