Skip to content

Commit a9364e2

Browse files
committed
Only apply latest tag to container if it is the latest stable tag
Add a program to determine the latest stable tag and compare it to the tag that triggered the workflow run. Only apply the latest tag if they match. Update the workflow to apply the latest tag based on the results of that program. This prevents new tags for older releases from getting the latst tag applied.
1 parent 31b6399 commit a9364e2

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

.github/workflows/build_and_push_release.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ jobs:
3131
with:
3232
# Fetch submodules (required for lightspeed-providers)
3333
submodules: 'recursive'
34+
# Fetch all tags to determine latest stable version
35+
fetch-tags: true
36+
- name: Determine if latest tag should be applied
37+
id: check_latest
38+
run: ./scripts/latest-tag.py
3439
- name: Build image with Buildah
3540
id: build_image
3641
uses: redhat-actions/buildah-build@v2
3742
with:
3843
image: ${{ env.IMAGE_NAME }}
3944
tags: |
4045
${{ env.GIT_TAG }}
41-
${{ env.LATEST_TAG }}
46+
${{ steps.check_latest.outputs.apply_latest == 'true' && env.LATEST_TAG || '' }}
4247
containerfiles: |
4348
${{ env.CONTAINER_FILE }}
4449
archs: amd64, arm64

scripts/latest-tag.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import subprocess
5+
6+
7+
def version_split(value: str) -> tuple[int, ...]:
8+
"""Split string into a tuple of ints."""
9+
try:
10+
return tuple(int(n) for n in value.split("."))
11+
except ValueError:
12+
return (-1,)
13+
14+
15+
def is_prerelease(tag: str) -> bool:
16+
"""Determine if a tag is a pre-release version."""
17+
omit = {"rc", "alpha", "beta", "dev"}
18+
19+
return any(n in tag for n in omit)
20+
21+
22+
def get_latest_stable() -> str | None:
23+
"""Return the latest stable tag."""
24+
stdout = subprocess.check_output(["git", "tag"], text=True)
25+
tags = [tag for tag in stdout.splitlines() if not is_prerelease(tag)]
26+
tags.sort(key=version_split)
27+
28+
return tags[-1] if tags else None
29+
30+
31+
def main() -> None:
32+
if not (current_tag := os.environ.get("GIT_TAG")):
33+
reason = "GIT_TAG environment variable not set, skipping latest tag"
34+
apply_latest = "false"
35+
elif is_prerelease(current_tag):
36+
reason = f"{current_tag} is a pre-release"
37+
apply_latest = "false"
38+
else:
39+
latest_stable = get_latest_stable()
40+
if current_tag == latest_stable:
41+
reason = f"{current_tag} is the latest stable"
42+
apply_latest = "true"
43+
else:
44+
reason = f"{current_tag} is not the latest stable ({latest_stable} is)"
45+
apply_latest = "false"
46+
47+
print(reason)
48+
49+
if github_output := os.environ.get("GITHUB_OUTPUT"):
50+
with open(github_output, "a") as f:
51+
f.write(f"apply_latest={apply_latest}\n")
52+
53+
54+
if __name__ == "__main__":
55+
main()

0 commit comments

Comments
 (0)