Skip to content

Commit

Permalink
feat: automatically update changelog after release
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmn2 committed Feb 9, 2025
1 parent 0d4b10a commit ec97cb4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 97 deletions.
37 changes: 23 additions & 14 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,38 @@ jobs:
mrmn/pdfding:${{ github.event.release.tag_name }}
mrmn/pdfding:latest
platforms: linux/amd64,linux/arm64
update_helm_chart:
name: Update helm chart
update_files:
name: Update files after release
# Don't run for release created by the chart-releaser-action
if: ${{ ! startsWith(github.ref, 'refs/tags/pdfding') }}
runs-on: ubuntu-latest
needs: publish_release
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.13'
cache: 'pip'
- run: pip install -r .github/workflows/scripts/requirements.txt
# we need to use a gh app as the default actions github_token cannot trigger further workflows
# like e.g. the chart release workflow which should be running afterwards
- uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.PDFDING_APP_ID }}
private-key: ${{ secrets.PDFDING_PEM_KEY }}
- run: python .github/workflows/scripts/update_chart_yaml_after_release.py
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
RELEASE_TAG: ${{ github.event.release.tag_name }}
- uses: actions/checkout@v4
with:
ref: ${{ github.event.repository.default_branch }}
token: ${{ steps.app-token.outputs.token }}
- uses: actions/setup-python@v5
with:
python-version: '3.13'
- name: Update files
run: |
python .github/workflows/scripts/update_files_after_release.py \
--release_tag "${{ github.event.release.tag_name }}" \
--release_body "${{ github.event.release.body }}" \
--changelog_path "CHANGELOG.MD" \
--chart_yaml_path "helm-charts/pdfding/Chart.yaml"
- name: Commit and push changes
run: |
git config --local user.email "197665213+pdfding[bot]@users.noreply.github.com"
git config --local user.name "pdfding[bot]"
git add CHANGELOG.MD helm-charts/pdfding/Chart.yaml
git commit -m "chore: update files for release ${{ github.event.release.tag_name }}"
git push
1 change: 0 additions & 1 deletion .github/workflows/scripts/requirements.txt

This file was deleted.

82 changes: 0 additions & 82 deletions .github/workflows/scripts/update_chart_yaml_after_release.py

This file was deleted.

80 changes: 80 additions & 0 deletions .github/workflows/scripts/update_files_after_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Module for updating the changelog and Chart.yaml files after a release. The chart.yaml will only be upgraded if the
appVersion has changed.
"""

from argparse import ArgumentParser
from datetime import datetime
from pathlib import Path


def update_changelog(changelog_path: Path, release_tag: str, release_body: str):
"""Update the changelog based on the release tag and the release_body."""

with open(changelog_path, 'r') as f:
lines = f.readlines()

now = datetime.now()
release_date = now.strftime('%b %d, %Y')

# construct new changelog entry
changelog_str = '# Changelog\n\n'
release_header = f'## {release_tag} ({release_date})\n'
release_body = f'{release_body}\n'

# construct changelog
lines = [changelog_str, release_header, release_body] + lines[1:]

with open(changelog_path, 'w') as f:
f.writelines(lines)


def update_chart_yaml_after_release(chart_yaml_path: Path, release_tag: 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()

app_version_line = lines.pop()
chart_version_line = lines.pop()

# check if the appVersion needs to be updated
if f'appVersion: {release_tag}' != app_version_line.strip():
# update chart version
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 chart.yaml
lines.extend([updated_chart_version_line, updated_app_version_line])

with open(chart_yaml_path, 'w') as f:
f.writelines(lines)


def main():
parser = ArgumentParser()
for argument in ['release_tag', 'release_body', 'changelog_path', 'chart_yaml_path']:
parser.add_argument(f'--{argument}')
args = parser.parse_args()

root_path = Path(__file__).parents[3]
changelog_path = root_path / args.changelog_path
chart_yaml_path = root_path / args.chart_yaml_path

release_tag = args.release_tag
release_body = args.release_body

update_chart_yaml_after_release(chart_yaml_path, release_tag)
update_changelog(changelog_path, release_tag, release_body)


if __name__ == '__main__':
main()

0 comments on commit ec97cb4

Please sign in to comment.