Skip to content

Commit 02f9ad6

Browse files
authored
feat: Remove deprecated Helm Charts Release action and add new charts-values-update-action (#266)
* feat: Remove deprecated Helm Charts Release action and add new charts-values-update-action * feat: Enhance image version retrieval with stable version checks and regex support * feat: Add debug logging for regex pattern in version retrieval * feat: Replace logging with print statements for regex pattern debugging * feat: Add error handling for missing tags in replace_tag_regexp function * feat: Improve error handling in regex functions with graceful exits * docs: Update README to clarify regex syntax for version retrieval
1 parent e656199 commit 02f9ad6

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

actions/helm-charts-release/README.md renamed to actions/charts-values-update-action/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ Can be `replace` or `parse`. Defaults to `parse`.
3232
If set to `replace` the action will just replace the versions of docker images with `release-version` value.
3333
If set to `parse` the action read provided `config-file` and substitute any environment variables provided in the version part.
3434
For example if you have some 3-rd party image in `values.yaml` file and want to manage it's version, you can add repository level variable and use it in the config file: `some-thirg-party-image:${THIRD_PARTY_VERSION}`.
35-
Also if you want the action to find the latest version of some image (supplimentary service for instance), you can set it to something like `#4.*.*` or `#latest`.
36-
In that case the action will find the latest tag of an image which satisfy the regular expression. The regular expression of a tag must start with `#` symbol and follow the `jq` syntax.
37-
**Special word `#latest` will result the latest SemVer tag of the image, not the one which marked with `latest` tag.**
35+
Also if you want the action to find the latest version of some image (supplimentary service for instance), you can set it to something like `#4\.\d+\.\d+` or `#latest`.
36+
In that case the action will find the latest tag of an image which satisfy the regular expression. The regular expression of a tag must start with `#` symbol and follow the Python `re` syntax.
37+
**Special word `#latest` will result the latest SemVer tag of the image ('2.1.0','v4.3.2', etc.), not the one which marked with `latest` tag.**
3838

3939
### `working-directory`
4040

@@ -82,7 +82,7 @@ jobs:
8282
runs-on: ubuntu-latest
8383
steps:
8484
- name: Release Helm Charts
85-
uses: netcracker/qubership-workflow-hub/actions/helm-charts-release@main
85+
uses: netcracker/qubership-workflow-hub/actions/charts-values-update-action@main
8686
with:
8787
release-version: '1.0.0'
8888
chart-version: '1.0.0'
@@ -113,4 +113,4 @@ jobs:
113113
- `version`: Template for the image version (e.g., `my-image:${release}`).
114114
- `image`: List of image keys to update in `values.yaml`.
115115

116-
> Example: [helm-charts-release-config.yaml](./helm-charts-release-config.yaml).
116+
> Example: [charts-values-update-config.yaml](./charts-values-update-config.yaml).
File renamed without changes.

actions/helm-charts-release/helm-charts-release-config.yaml renamed to actions/charts-values-update-action/charts-values-update-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ charts:
1212
image:
1313
- ghcr.io/netcracker/module1:${release} # This will replace the image version with the release version
1414
- ghcr.io/netcracker/module1-service1:${release}-${THIRD_PARTY_VERSION} # This will replace the image version with the release version and append the THIRD_PARTY_VERSION variable
15-
- ghcr.io/netcracker/module1-service2:#5.*.* # This will select the latest tag matching the jq regular expression
15+
- ghcr.io/netcracker/module1-service2:#5\.\d+\.\d+ # This will select the latest tag matching the jq regular expression
1616
- ghcr.io/netcracker/module1-service2:#latest # This will select the latest SemVer tag

actions/helm-charts-release/image-versions-replace.py renamed to actions/charts-values-update-action/image-versions-replace.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import argparse
44
import json
55
import os
6+
from packaging import version
67
import re
78
import subprocess
9+
import sys
810
import yaml
911

1012
def replace_env_variables(input_string):
@@ -18,15 +20,64 @@ def replacer(match):
1820

1921
return pattern.sub(replacer, input_string)
2022

23+
def get_latest_stable_version(versions):
24+
stable_versions = []
25+
26+
for v in versions:
27+
try:
28+
ver = version.parse(v)
29+
# Check that version is stable
30+
if not ver.is_prerelease:
31+
stable_versions.append((v, ver))
32+
except version.InvalidVersion:
33+
continue
34+
35+
if not stable_versions:
36+
return None
37+
38+
latest = sorted(stable_versions, key=lambda x: x[1])[-1][0]
39+
return latest
40+
41+
def get_latest_version_by_regex(versions, pattern_str):
42+
try:
43+
print(f"Using regex pattern: {pattern_str}")
44+
pattern = r'' + pattern_str
45+
compiled_pattern = re.compile(pattern)
46+
except re.error as e:
47+
print(f"Invalid regular expression '{pattern}': {str(e)}")
48+
sys.exit(1)
49+
#raise ValueError(f"Incorrect regular expression: {str(e)}") from None
50+
51+
matched_versions = []
52+
53+
for v in versions:
54+
try:
55+
if compiled_pattern.fullmatch(v):
56+
ver = version.parse(v)
57+
if not ver.is_prerelease:
58+
matched_versions.append((v, ver))
59+
except version.InvalidVersion:
60+
continue
61+
62+
if not matched_versions:
63+
return None
64+
65+
return sorted(matched_versions, key=lambda x: x[1])[-1][0]
66+
2167
def replace_tag_regexp(image_str, tag_re):
2268
# Try to find the requested tag for given image_str
2369
if tag_re.startswith("#"):
2470
try:
2571
os.system(f"skopeo login -u $GITHUB_ACTOR -p $GITHUB_TOKEN ghcr.io")
72+
tags = subprocess.run(f"skopeo list-tags docker://{image_str} | jq -r '.Tags[]'", shell=True, text=True, check=True, capture_output=True).stdout.split()
2673
if tag_re[1:] == 'latest':
27-
result_tag = subprocess.run(f"skopeo list-tags docker://{image_str} | jq -r '.Tags[]' | grep -e \"^[0-9]*\.[0-9]*\.[0-9]*\" | sort -V | tail -n 1", shell=True, text=True, check=True, capture_output=True).stdout.rstrip()
74+
result_tag = get_latest_stable_version(tags)
2875
else:
29-
result_tag = subprocess.run(f"skopeo list-tags docker://{image_str} | jq -r '.Tags[] | select(test(\"^{tag_re[1:]}\"))' | sort -V | tail -n 1", shell=True, text=True, check=True, capture_output=True).stdout.rstrip()
76+
result_tag = get_latest_version_by_regex(tags, tag_re[1:])
77+
if not result_tag:
78+
print(f"No matching tag found for {image_str} with pattern {tag_re}")
79+
#raise ValueError(f"No matching tag found for {image_str} with pattern {tag_re}")
80+
sys.exit(1)
3081
return(result_tag)
3182
except Exception as e:
3283
print(f"Error: {e}")

0 commit comments

Comments
 (0)