refactor: streamline Docker workflow by removing unnecessary steps and enhancing tag computation
Some checks failed
Build and Push Docker Image / docker (push) Failing after 0s

This commit is contained in:
2025-09-04 16:09:10 +02:00
parent 94f870b1ef
commit 8152a16cc7

View File

@@ -13,12 +13,6 @@ jobs:
docker:
runs-on: docker
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Define Registry Variables
id: vars
run: |
@@ -45,33 +39,72 @@ jobs:
echo "owner=$OWNER" >> "$GITHUB_OUTPUT"
echo "image=$IMAGE_NAME" >> "$GITHUB_OUTPUT"
- name: Compute Tags
id: tags
env:
REGISTRY: ${{ steps.vars.outputs.registry }}
OWNER: ${{ steps.vars.outputs.owner }}
IMAGE: ${{ steps.vars.outputs.image }}
run: |
set -euo pipefail
IMAGE_FULL="$REGISTRY/$OWNER/$IMAGE"
REF="$GITHUB_REF"
SHA_SHORT=$(echo "$GITHUB_SHA" | cut -c1-8)
TAGS=()
case "$REF" in
refs/heads/*)
BRANCH=${REF#refs/heads/}
# latest for main/master
if [ "$BRANCH" = "main" ] || [ "$BRANCH" = "master" ]; then
TAGS+=("latest")
fi
# branch tag
SAFE_BRANCH=$(echo "$BRANCH" | tr '/' '-' )
TAGS+=("$SAFE_BRANCH")
;;
refs/tags/*)
TAG=${REF#refs/tags/}
TAGS+=("$TAG")
;;
esac
# always include short sha
TAGS+=("$SHA_SHORT")
# Build -t args
TAG_ARGS=""
for t in "${TAGS[@]}"; do
TAG_ARGS="$TAG_ARGS -t $IMAGE_FULL:$t"
done
echo "image_full=$IMAGE_FULL" >> "$GITHUB_OUTPUT"
echo "tag_args=$TAG_ARGS" >> "$GITHUB_OUTPUT"
- name: Log in to Gitea Registry
uses: docker/login-action@v3
with:
registry: ${{ steps.vars.outputs.registry }}
username: ${{ gitea.actor }}
password: ${{ secrets.GITEA_TOKEN }}
env:
REGISTRY: ${{ steps.vars.outputs.registry }}
run: |
echo "${{ secrets.GITEA_TOKEN }}" | docker login "$REGISTRY" -u "$GITHUB_ACTOR" --password-stdin
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ steps.vars.outputs.registry }}/${{ steps.vars.outputs.owner }}/${{ steps.vars.outputs.image }}
tags: |
type=raw,value=latest,enable=${{ gitea.ref_type == 'branch' && (gitea.ref == 'refs/heads/main' || gitea.ref == 'refs/heads/master') }}
type=ref,event=branch
type=ref,event=tag
type=sha,format=short
- name: Ensure buildx builder
run: |
docker buildx inspect >/dev/null 2>&1 || docker buildx create --use
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ${{ env.DOCKERFILE }}
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ steps.vars.outputs.registry }}/${{ steps.vars.outputs.owner }}/${{ steps.vars.outputs.image }}:buildcache
cache-to: type=registry,ref=${{ steps.vars.outputs.registry }}/${{ steps.vars.outputs.owner }}/${{ steps.vars.outputs.image }}:buildcache,mode=max
- name: Build and push (linux/amd64)
env:
DOCKERFILE: ${{ env.DOCKERFILE }}
IMAGE_FULL: ${{ steps.tags.outputs.image_full }}
TAG_ARGS: ${{ steps.tags.outputs.tag_args }}
run: |
set -euo pipefail
echo "Building $IMAGE_FULL with tags: $TAG_ARGS"
docker buildx build \
--platform linux/amd64 \
-f "$DOCKERFILE" \
$TAG_ARGS \
--cache-from type=registry,ref="$IMAGE_FULL:buildcache" \
--cache-to type=registry,ref="$IMAGE_FULL:buildcache",mode=max \
--push \
.