Skip to content

Instantly share code, notes, and snippets.

@lordmauve
Created February 16, 2024 23:46
Show Gist options
  • Save lordmauve/bc99c313a37bc397ff7270215f55b377 to your computer and use it in GitHub Desktop.
Save lordmauve/bc99c313a37bc397ff7270215f55b377 to your computer and use it in GitHub Desktop.
Detect new versions of Python packages in GHA
name: Check Version Change and Tag
on: [push]
jobs:
tag-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Fetches all history for branches and tags
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install toml
- name: Check version change and tag
run: |
import toml
import subprocess
def run_cmd(cmd):
return subprocess.run(cmd, check=True, shell=True, capture_output=True, text=True).stdout.strip()
def read_version_from_commit(commit_hash):
pyproject_content = run_cmd(f"git show {commit_hash}:pyproject.toml")
return toml.loads(pyproject_content)['tool']['poetry']['version']
old_commit = '${{ github.event.before }}'
new_commit = '${{ github.event.after }}'
old_version = read_version_from_commit(old_commit)
new_version = read_version_from_commit(new_commit)
if old_version != new_version:
print(f"Version changed from {old_version} to {new_version}")
tag_name = f"v{new_version}"
tag_message = f"Release {tag_name}"
run_cmd(f"git config user.name 'github-actions'")
run_cmd(f"git config user.email 'github-actions@github.com'")
run_cmd(f"git tag -a {tag_name} -m '{tag_message}'")
run_cmd(f"git push origin {tag_name}")
else:
print("Version did not change.")
shell: python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment