Skip to content

Commit

Permalink
feat: automatically update version tag in deploy.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Dec 24, 2024
1 parent d299b19 commit e4340c0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ on:
workflow_dispatch:
inputs:
version-tag:
description: Version tag
description: 'Version tag'
required: true
default: v0.1.5
default: v0.1.8
dry-run:
description: Dry run
type: boolean
default: false

#### aaaaaaa
jobs:
commit-changelog-and-release:
uses: ./.github/workflows/commit-changelog-and-release.yml
with:
version-tag: ${{ github.event.inputs.version-tag }}
#### aaaaaaa
dry-run: ${{ github.event.inputs.dry-run == 'true' }}
changelog-path: docs/CHANGELOG.md
exclude-types: build,docs,style,other
deploy-yml-path: .github/workflows/_deploy.yml

publish-to-pypi:
if: ${{ github.event.inputs.dry-run == 'false' }}
Expand All @@ -38,5 +41,5 @@ jobs:
- name: Build and upload to PyPI
run: |
cd python-projector
uv build --sdist
uv build
uv publish
35 changes: 35 additions & 0 deletions .github/workflows/commit-changelog-and-release.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
# Example deploy.yml
# on:
# workflow_dispatch:
# inputs:
# version-tag:
# description: Version tag
# required: true
# default: v0.1.8 # IMPORTANT to have this because we update this.
# dry-run:
# description: Dry run
# type: boolean
# default: false
#
# jobs:
# commit-changelog-and-release:
# uses: ./.github/workflows/commit-changelog-and-release.yml
# with:
# version-tag: ${{ github.event.inputs.version-tag }}
# dry-run: ${{ github.event.inputs.dry-run == 'true' }}
# changelog-path: docs/CHANGELOG.md
# exclude-types: build,docs,style,other
# deploy-yml-path: .github/workflows/deploy.yml

name: Commit CHANGELOG.md and create a Release

on:
Expand All @@ -19,6 +42,10 @@ on:
description: Comma-separated list of commit types to exclude from the changelog
type: string
default: build,docs,style,other
deploy-yml-path:
description: Path to the deploy.yml file to modify the next version. It should have on.workflow_call.inputs.version-tag
type: string
default: .github/workflows/deploy.yml

jobs:
dry-run:
Expand Down Expand Up @@ -99,8 +126,16 @@ jobs:
git push origin --delete ${{ inputs.version-tag }}
fi
- name: Modify default next version in deploy.yml inputs
run: |
pip install ruamel.yaml
python <(curl https://raw.githubusercontent.com/deargen/workflows/refs/heads/master/scripts/set_next_version_in_deploy_yml.py) \
${{ inputs.deploy-yml-path }} \
${{ inputs.version-tag }}
- name: Commit ${{ inputs.changelog-path }} and update tag
run: |
git add ${{ inputs.deploy-yml-path }}
git add ${{ inputs.changelog-path }}
git commit -m "docs: update ${{ inputs.changelog-path }} for ${{ inputs.version-tag }} [skip ci]"
git tag -a ${{ inputs.version-tag }} -m ${{ inputs.version-tag }}
Expand Down
70 changes: 70 additions & 0 deletions scripts/set_next_version_in_deploy_yml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
Update the default version tag in the deploy.yml file.
```yaml
on:
workflow_dispatch:
inputs:
version-tag:
description: 'Version tag'
required: true
default: 'v0.1.0' # <-- HERE
# ...
```
"""

import sys
from pathlib import Path

import ruamel.yaml


def unidiff_output(expected: str, actual: str):
"""Returns a string containing the unified diff of two multiline strings."""
import difflib

expected_list = expected.splitlines(keepends=True)
actual_list = actual.splitlines(keepends=True)

diff = difflib.unified_diff(expected_list, actual_list)

return "".join(diff)


if len(sys.argv) != 3:
print(
"Usage: python increase_version_in_deploy_yml.py <deploy.yml> <current_version>"
)
print(
"Example: python increase_version_in_deploy_yml.py .github/workflows/_deploy.yml v0.1.0"
)
print(
"This script will set on.workflow_dispatch.inputs.version-tag.default to v0.1.1"
)
sys.exit(1)

deploy_yml = sys.argv[1]
current_version = sys.argv[2]

split_version = current_version.split(".")
last_digit = int(split_version[-1])
split_version[-1] = str(last_digit + 1)
new_version = ".".join(split_version)

yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
yaml.indent(mapping=2, sequence=4, offset=2)
original_yml_content = Path(deploy_yml).read_text()
with open(deploy_yml) as f:
data = yaml.load(f)
data["on"]["workflow_dispatch"]["inputs"]["version-tag"]["default"] = new_version

with open(deploy_yml, "w") as f:
yaml.dump(data, f)

new_yml_content = Path(deploy_yml).read_text()
if original_yml_content == new_yml_content:
print(f"Version was already set to {new_version}")
else:
print(unidiff_output(original_yml_content, new_yml_content))

0 comments on commit e4340c0

Please sign in to comment.