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
Some checks failed
Build and Push Docker Image / docker (push) Failing after 0s
This commit is contained in:
@@ -13,12 +13,6 @@ jobs:
|
|||||||
docker:
|
docker:
|
||||||
runs-on: docker
|
runs-on: docker
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
- name: Define Registry Variables
|
- name: Define Registry Variables
|
||||||
id: vars
|
id: vars
|
||||||
run: |
|
run: |
|
||||||
@@ -45,33 +39,72 @@ jobs:
|
|||||||
echo "owner=$OWNER" >> "$GITHUB_OUTPUT"
|
echo "owner=$OWNER" >> "$GITHUB_OUTPUT"
|
||||||
echo "image=$IMAGE_NAME" >> "$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
|
- name: Log in to Gitea Registry
|
||||||
uses: docker/login-action@v3
|
env:
|
||||||
with:
|
REGISTRY: ${{ steps.vars.outputs.registry }}
|
||||||
registry: ${{ steps.vars.outputs.registry }}
|
run: |
|
||||||
username: ${{ gitea.actor }}
|
echo "${{ secrets.GITEA_TOKEN }}" | docker login "$REGISTRY" -u "$GITHUB_ACTOR" --password-stdin
|
||||||
password: ${{ secrets.GITEA_TOKEN }}
|
|
||||||
|
|
||||||
- name: Extract metadata (tags, labels)
|
- name: Ensure buildx builder
|
||||||
id: meta
|
run: |
|
||||||
uses: docker/metadata-action@v5
|
docker buildx inspect >/dev/null 2>&1 || docker buildx create --use
|
||||||
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: Build and push
|
- name: Build and push (linux/amd64)
|
||||||
uses: docker/build-push-action@v6
|
env:
|
||||||
with:
|
DOCKERFILE: ${{ env.DOCKERFILE }}
|
||||||
context: .
|
IMAGE_FULL: ${{ steps.tags.outputs.image_full }}
|
||||||
file: ${{ env.DOCKERFILE }}
|
TAG_ARGS: ${{ steps.tags.outputs.tag_args }}
|
||||||
platforms: linux/amd64
|
run: |
|
||||||
push: true
|
set -euo pipefail
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
echo "Building $IMAGE_FULL with tags: $TAG_ARGS"
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
docker buildx build \
|
||||||
cache-from: type=registry,ref=${{ steps.vars.outputs.registry }}/${{ steps.vars.outputs.owner }}/${{ steps.vars.outputs.image }}:buildcache
|
--platform linux/amd64 \
|
||||||
cache-to: type=registry,ref=${{ steps.vars.outputs.registry }}/${{ steps.vars.outputs.owner }}/${{ steps.vars.outputs.image }}:buildcache,mode=max
|
-f "$DOCKERFILE" \
|
||||||
|
$TAG_ARGS \
|
||||||
|
--cache-from type=registry,ref="$IMAGE_FULL:buildcache" \
|
||||||
|
--cache-to type=registry,ref="$IMAGE_FULL:buildcache",mode=max \
|
||||||
|
--push \
|
||||||
|
.
|
||||||
|
|||||||
Reference in New Issue
Block a user