Skip to content

Commit a627183

Browse files
committed
Refactor
1 parent e43d186 commit a627183

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

.github/workflows/build_and_publish.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and Publish Demo Image
1+
name: Build and Push Docker Image (Branch-Aware Tags)
22

33
on:
44
push:
@@ -30,28 +30,37 @@ jobs:
3030
username: ${{ github.actor }}
3131
password: ${{ secrets.GITHUB_TOKEN }}
3232

33-
- name: Extract version info
33+
- name: Extract version info (tag or branch fallback)
3434
id: version
3535
run: |
3636
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
3737
git fetch --tags
38-
TAG_NAME=$(git describe --tags "$(git rev-list --tags --max-count=1)")
38+
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
39+
40+
TAG_NAME=$(git describe --tags --abbrev=0 "$(git rev-list --tags --no-walk --branches=$BRANCH_NAME | head -n 1)" 2>/dev/null || true)
41+
42+
if [[ -z "$TAG_NAME" ]]; then
43+
TAG_NAME="${BRANCH_NAME//\//-}" # sanitize: replace slashes with dashes
44+
fi
3945
else
4046
TAG_NAME="${GITHUB_REF#refs/tags/}"
4147
fi
4248
4349
MAJOR_MINOR=$(echo "$TAG_NAME" | cut -d. -f1,2)
4450
45-
echo "Using tag: $TAG_NAME"
4651
echo "TAG=$TAG_NAME" >> $GITHUB_ENV
4752
echo "MAJOR_MINOR=$MAJOR_MINOR" >> $GITHUB_ENV
4853
54+
- name: Normalize repository name (lowercase)
55+
run: |
56+
echo "REPO_LOWER=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
57+
4958
- name: Build and push Docker image
5059
uses: docker/build-push-action@v2
5160
with:
5261
context: .
62+
file: .docker/Dockerfile
5363
push: true
5464
tags: |
55-
ghcr.io/${{ github.repository_owner }}/$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]'):${{ env.TAG }}
56-
ghcr.io/${{ github.repository_owner }}/$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]'):${{ env.MAJOR_MINOR }}
57-
65+
ghcr.io/${{ env.REPO_LOWER }}:${{ env.TAG }}
66+
ghcr.io/${{ env.REPO_LOWER }}:${{ env.MAJOR_MINOR }}

0 commit comments

Comments
 (0)