Skip to content

Commit

Permalink
feat: add job for updating Chart.yaml after release
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmn2 committed Feb 3, 2025
1 parent f7b7103 commit 7cd2d34
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 19 deletions.
34 changes: 16 additions & 18 deletions .github/workflows/publish.yaml → .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
name: PdfDing Publish
name: PdfDing Release

on:
pull_request:
paths: [ 'pdfding/**/*.py', 'pdfding/**/*.html', 'pyproject.toml', 'package.json' , 'Dockerfile' ]
release:
types:
- released

jobs:
release:
name: Release
if: github.event_name == 'release'
publish_release:
name: Publish release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -34,19 +31,20 @@ jobs:
mrmn/pdfding:${{ github.event.release.tag_name }}
mrmn/pdfding:latest
platforms: linux/amd64,linux/arm64
dry_run:
name: Dry run
if: github.event_name == 'pull_request'
update_helm_chart:
name: Update helm chart
runs-on: ubuntu-latest
needs: publish_release
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build
uses: docker/build-push-action@v6
- uses: actions/setup-python@v5
with:
context: .
push: false
outputs: type=cacheonly
python-version: '3.13'
cache: 'pip'
- run: pip install -r .github/workflows/scripts/requirements.txt
- run: python .github/workflows/scripts/update_chart_yaml_after_release.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_TAG: ${{ github.event.release.tag_name }}
1 change: 1 addition & 0 deletions .github/workflows/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyGithub == 2.5.0
72 changes: 72 additions & 0 deletions .github/workflows/scripts/update_chart_yaml_after_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Module for updating the Chart.yaml file PdfDing's helm chart after a release"""

from os import environ
from pathlib import Path

from github import Auth, Github


def update_versions_after_release(chart_yaml_path: Path, release_tag: str) -> str:
"""
Update the chart version and app version of the helm chart after a release. The chart version's patch will
be incremented by 1, the app version will be replaced by the 'release_tag'.
"""

with open(chart_yaml_path, "r") as f:
lines = f.readlines()[:-1]

# update chart version
chart_version_line = lines.pop()
chart_version_line_split = chart_version_line.rsplit(".", maxsplit=1)
patch = chart_version_line.split(".")[-1]
updated_patch = int(patch) + 1
updated_chart_version_line = f"{chart_version_line_split[0]}.{updated_patch}\n"

# update appVersion
updated_app_version_line = f"appVersion: {release_tag}\n"

# update lines
lines.extend([updated_chart_version_line, updated_app_version_line])

return "".join(lines)


def gh_update_chart_yaml(
repo: str,
branch: str,
file_path: str,
chart_yaml_content: str,
release_tag: str,
github_token: str,
):
"""
Update the Chart.yaml after a release, create a commit and push it to the specified branch. The chart version's
patch will be incremented by 1, the app version will be replaced by the 'release_tag'.
"""

auth = Auth.Token(github_token)
g = Github(auth=auth)

repo = g.get_repo(repo)
contents = repo.get_contents(file_path, ref=branch)
repo.update_file(
contents.path,
f'Update helm chart for release "{release_tag}"',
chart_yaml_content,
contents.sha,
branch=branch,
)


def main():
file_path = "helm-charts/pdfding/Chart.yaml"
chart_yaml_path = Path(__file__).parents[3] / file_path
release_tag = environ.get("RELEASE_TAG")
github_token = environ.get("GITHUB_TOKEN")

chart_yaml_content = update_versions_after_release(chart_yaml_path, release_tag)
gh_update_chart_yaml("mrmn2/pdfding", "master", file_path, chart_yaml_content, release_tag, github_token)


if __name__ == "__main__":
main()
24 changes: 23 additions & 1 deletion .github/workflows/test.yaml → .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ name: PdfDing Tests

on:
pull_request:
paths: [ 'pdfding/**/*.py', 'pdfding/**/*.html', 'pyproject.toml', 'package.json' , '.github/workflows/test.yaml' ]
paths:
- 'pdfding/**/*.py'
- 'pdfding/**/*.html'
- 'pyproject.toml'
- 'package.json'
- '.github/workflows/test.yaml'
- 'Dockerfile'

jobs:
code_quality:
Expand Down Expand Up @@ -73,3 +79,19 @@ jobs:
npx --yes @tailwindcss/cli -i pdfding/static/css/input.css -o pdfding/static/css/tailwind.css --minify
- name: Run tests
run: poetry run python -m pytest pdfding/e2e

publish_dry_run:
name: Publish dry run
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build
uses: docker/build-push-action@v6
with:
context: .
push: false
outputs: type=cacheonly

0 comments on commit 7cd2d34

Please sign in to comment.