Skip to content

Commit af5002f

Browse files
committed
Tag :latest only for the newest released version.
1 parent b4b050c commit af5002f

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

.github/workflows/docker.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
with:
4545
ref: ${{ github.event_name == 'workflow_dispatch' && inputs.ref || github.ref }}
4646
path: source
47+
fetch-depth: 0
4748

4849
- name: Check out Dockerfile ref
4950
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
@@ -52,7 +53,9 @@ jobs:
5253
path: dockerfile
5354

5455
# Resolve both refs to SHAs and compute Docker tags from the source ref.
55-
# - Version tag (e.g. v1.2.3): push versioned + latest tags.
56+
# - Highest version tag (e.g. v1.2.3): push versioned + latest tags.
57+
# - Older version tag: push only the versioned tag, so :latest never
58+
# moves back to an older line (e.g. a 25.x patch after 26.x ships).
5659
# - Any other ref: push a tag for the resolved source commit SHA.
5760
- name: Resolve refs and tags
5861
id: resolve
@@ -65,7 +68,17 @@ jobs:
6568
6669
if [[ "$ref" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
6770
version="${ref#v}"
68-
echo "tags=-t ${REGISTRY_IMAGE}:${version} -t ${REGISTRY_IMAGE}:latest" >> $GITHUB_OUTPUT
71+
highest="$(git -C source tag --list 'v[0-9]*.[0-9]*.[0-9]*' \
72+
| grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n1)"
73+
if [[ -z "$highest" ]]; then
74+
echo "::error::Could not determine the highest version tag."
75+
exit 1
76+
fi
77+
if [[ "$ref" == "$highest" ]]; then
78+
echo "tags=-t ${REGISTRY_IMAGE}:${version} -t ${REGISTRY_IMAGE}:latest" >> $GITHUB_OUTPUT
79+
else
80+
echo "tags=-t ${REGISTRY_IMAGE}:${version}" >> $GITHUB_OUTPUT
81+
fi
6982
elif [[ "${{ github.event_name }}" == "release" ]]; then
7083
echo "::error::Release tag '${ref}' is not a valid version tag (expected vX.Y.Z)."
7184
exit 1

0 commit comments

Comments
 (0)