From e4340c03d80e22bb60762c364644b5e6bed57954 Mon Sep 17 00:00:00 2001 From: Kiyoon Kim Date: Tue, 24 Dec 2024 18:43:41 +0900 Subject: [PATCH] feat: automatically update version tag in deploy.yml --- .github/workflows/_deploy.yml | 9 ++- .../commit-changelog-and-release.yml | 35 ++++++++++ scripts/set_next_version_in_deploy_yml.py | 70 +++++++++++++++++++ 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 scripts/set_next_version_in_deploy_yml.py diff --git a/.github/workflows/_deploy.yml b/.github/workflows/_deploy.yml index 51602b7..63083b3 100644 --- a/.github/workflows/_deploy.yml +++ b/.github/workflows/_deploy.yml @@ -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' }} @@ -38,5 +41,5 @@ jobs: - name: Build and upload to PyPI run: | cd python-projector - uv build --sdist + uv build uv publish diff --git a/.github/workflows/commit-changelog-and-release.yml b/.github/workflows/commit-changelog-and-release.yml index d607ae5..e452864 100644 --- a/.github/workflows/commit-changelog-and-release.yml +++ b/.github/workflows/commit-changelog-and-release.yml @@ -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: @@ -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: @@ -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 }} diff --git a/scripts/set_next_version_in_deploy_yml.py b/scripts/set_next_version_in_deploy_yml.py new file mode 100644 index 0000000..3d54fb5 --- /dev/null +++ b/scripts/set_next_version_in_deploy_yml.py @@ -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 " + ) + 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))