From 67fad8c2b46fb6125766276fe4f1ef12edacfde5 Mon Sep 17 00:00:00 2001 From: jan-niklasml <182075087+jan-niklasml@users.noreply.github.com> Date: Wed, 19 Feb 2025 16:51:49 +0100 Subject: [PATCH 1/3] Create update_chart_versions.py --- update_chart_versions.py | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 update_chart_versions.py diff --git a/update_chart_versions.py b/update_chart_versions.py new file mode 100644 index 0000000..77cbb3d --- /dev/null +++ b/update_chart_versions.py @@ -0,0 +1,60 @@ +from packaging.version import Version +import ruamel.yaml +from ruamel.yaml.scalarstring import DoubleQuotedScalarString +import argparse +import os + +def increment_version(version): + v = Version(version) + return f"{v.major}.{v.minor}.{v.micro + 1}" + +parser = argparse.ArgumentParser(description='Upgrade Versions') +parser.add_argument('--appVersion', help='New App-Version') +parser.add_argument('--pathToChart', help='Path to updated Chart.yaml') +args = parser.parse_args() + +yaml = ruamel.yaml.YAML() +yaml.preserve_quotes = True + +# Check, whether passed file path is sub-chart or parent-chart +path_to_helm_charts = os.path.abspath('charts/') +target_path = os.path.abspath(args.pathToChart) + +if path_to_helm_charts == os.path.dirname(os.path.dirname(target_path)): + parent_chart_yaml = target_path + sub_chart_yaml = "" +else: + parent_chart_yaml = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(target_path))), "Chart.yaml") + sub_chart_yaml = target_path + +# Load Chart.yaml files +with open(parent_chart_yaml, "r") as file: + parent_chart_file_data = yaml.load(file) +if sub_chart_yaml: + with open(sub_chart_yaml, "r") as file: + sub_chart_file_data = yaml.load(file) + +# Update versions +if sub_chart_yaml: + if sub_chart_file_data["appVersion"] != args.appVersion: + # Sub-Chart was updated + sub_chart_file_data["version"] = increment_version(sub_chart_file_data["version"]) + sub_chart_file_data["appVersion"] = DoubleQuotedScalarString(args.appVersion) + entry_index = parent_chart_file_data["dependencies"].index(next(filter(lambda entry: entry['name'] == sub_chart_file_data["name"], parent_chart_file_data["dependencies"]), None)) + parent_chart_file_data["dependencies"][entry_index]["version"] = sub_chart_file_data["version"] + parent_chart_file_data["version"] = increment_version(parent_chart_file_data["version"]) +else: + # Parent-Chart was updated + if "appVersion" in parent_chart_file_data.keys(): + if parent_chart_file_data["appVersion"] != args.appVersion: + parent_chart_file_data["appVersion"] = args.appVersion + parent_chart_file_data["version"] = increment_version(parent_chart_file_data["version"]) + else: + parent_chart_file_data["version"] = increment_version(parent_chart_file_data["version"]) + +# Write data to Chart.yaml files +with open(parent_chart_yaml, "w") as file: + yaml.dump(parent_chart_file_data, file) +if sub_chart_yaml: + with open(sub_chart_yaml, "w") as file: + yaml.dump(sub_chart_file_data, file) From 768a27e6efa105d6e3ab7ed74a9fc3e4ba1bc4a7 Mon Sep 17 00:00:00 2001 From: jan-niklasml <182075087+jan-niklasml@users.noreply.github.com> Date: Thu, 20 Feb 2025 15:59:26 +0100 Subject: [PATCH 2/3] Updated Workflow --- .github/workflows/updateVersions.yml | 34 ++++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/.github/workflows/updateVersions.yml b/.github/workflows/updateVersions.yml index 8d251b4..e53bfc0 100644 --- a/.github/workflows/updateVersions.yml +++ b/.github/workflows/updateVersions.yml @@ -9,11 +9,14 @@ on: path-to-chart-yaml: description: "Path to Chart.yaml" required: true - repository_dispatch: - types: [trigger-event] permissions: contents: write pull-requests: write +env: + GH_TOKEN: ${{ github.token }} + REPO_URL: "https://github.com/${{ github.repository }}" + APP_VERSION: ${{ inputs.app-version }} + PATH_TO_CHART: ${{ inputs.path-to-chart-yaml }} jobs: update-versions: @@ -29,25 +32,26 @@ jobs: run: | python -m pip install --upgrade pip pip install -r requirements.txt - - name: Run Python script with input from workflow_dispatch - if: github.event_name == 'workflow_dispatch' - run: python update_chart_versions.py --appVersion ${{ inputs.app-version }} --pathToChart ${{ inputs.path-to-chart-yaml }} - - name: Run Python script with input from repository_dispatch - if: github.event_name == 'repository_dispatch' - run: python update_chart_versions.py --appVersion ${{ github.event.client_payload.app-version }} --pathToChart ${{ github.event.client_payload.path-to-chart-yaml }} + - name: Run Python script + run: python update_chart_versions.py --appVersion $APP_VERSION --pathToChart $PATH_TO_CHART - name: Commit Files and create Pull-Request - env: - GH_TOKEN: ${{ github.token }} run: | git config --global user.name 'Your Name' git config --global user.email 'your-username@users.noreply.github.com' if [ -n "$(git status --porcelain)" ]; then - NEW_BRANCH="update-versions-$(date +%Y%m%d%H%M%S)" - git checkout -b $NEW_BRANCH - git commit -am "Updated Versions" + NEW_BRANCH="update-$PATH_TO_CHART-$APP_VERSION" + PR_TITLE="Update Chart $PATH_TO_CHART to v$APP_VERSION" + PR_BODY="This PR updates Chart [$PATH_TO_CHART]($REPO_URL/blob/$NEW_BRANCH/$PATH_TO_CHART) to App-Version $APP_VERSION" + git fetch --all + git commit -am "Update Chart $PATH_TO_CHART to v$APP_VERSION" + if git show-ref --verify --quiet refs/remotes/origin/$NEW_BRANCH; then + git checkout $NEW_BRANCH + git pull + else + git checkout -b $NEW_BRANCH + fi git push --set-upstream origin $NEW_BRANCH - gh pr create --base main --head $NEW_BRANCH --title "Update Versions for Chart" --body "This PR updates the versions in Chart.yaml files." + gh pr create --base main --head $NEW_BRANCH --title "$PR_TITLE" --body "$PR_BODY" else echo "No changes to commit" fi - From dae8fca3bdadcf547d2d494a1ab789aedf6e77ef Mon Sep 17 00:00:00 2001 From: jan-niklasml <182075087+jan-niklasml@users.noreply.github.com> Date: Mon, 24 Feb 2025 08:41:18 +0100 Subject: [PATCH 3/3] Yaml formatting --- update_chart_versions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/update_chart_versions.py b/update_chart_versions.py index 77cbb3d..5d4854c 100644 --- a/update_chart_versions.py +++ b/update_chart_versions.py @@ -15,6 +15,8 @@ def increment_version(version): yaml = ruamel.yaml.YAML() yaml.preserve_quotes = True +yaml.indent(offset=2, sequence=4) +yaml.width = 100 # Check, whether passed file path is sub-chart or parent-chart path_to_helm_charts = os.path.abspath('charts/')