Skip to content

Commit 86ab0bc

Browse files
committed
Add new workflow for releasing
1 parent 8da53e0 commit 86ab0bc

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

.github/workflows/publish.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: Publish
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
version_type:
6+
type: choice
7+
description: Version type
8+
default: minor
9+
options:
10+
- major
11+
- minor
12+
- patch
13+
jobs:
14+
publish:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v3
18+
- name: Setup git repo
19+
run: |
20+
git config user.name $GITHUB_ACTOR
21+
git config user.email gh-actions-${GITHUB_ACTOR}@github.com
22+
git remote add gh-origin https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
23+
- name: Set up Python
24+
uses: actions/setup-python@v4
25+
with:
26+
python-version: 3.12
27+
- name: Install dependencies
28+
run: |
29+
pip install poetry
30+
poetry install
31+
- name: Bump Version
32+
id: bump_version
33+
run: |
34+
NEW_VERSION=$(python scripts/update_version.py ${{ github.event.inputs.version_type }})
35+
echo "NEW_VERSION=$NEW_VERSION"
36+
echo "new_version=$(echo $NEW_VERSION)" >> $GITHUB_OUTPUT
37+
- name: Generate with new version
38+
run: ./scripts/generate.sh
39+
- name: Build
40+
run: poetry build
41+
- name: Commit and push changes
42+
run: |
43+
git add ynab/configuration.py ynab/api_client.py ynab/__init__.py pyproject.toml openapi-generator-config.yaml && git diff-index --quiet HEAD || git commit -m 'Bumping version for ${{ steps.bump_version.outputs.new_version }}'
44+
git push gh-origin HEAD:main
45+
- name: Publish to PyPI
46+
env:
47+
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.POETRY_PYPI_TOKEN_PYPI }}
48+
run: poetry publish
49+
- name: Create a Release
50+
id: create-release
51+
uses: softprops/action-gh-release@v1
52+
with:
53+
tag_name: ${{ steps.bump_version.outputs.new_version }}
54+
generate_release_notes: true
55+
- name: Comment on PRs with link to release they are included in
56+
uses: actions/github-script@v6
57+
env:
58+
RELEASE_ID: ${{ steps.create-release.outputs.id }}
59+
with:
60+
script: |
61+
const releaseId = process.env.RELEASE_ID;
62+
console.log(`Fetching release_id: ${releaseId} ...`);
63+
const getReleaseResponse = await github.rest.repos.getRelease({
64+
release_id: process.env.RELEASE_ID,
65+
owner: context.repo.owner,
66+
repo: context.repo.repo
67+
});
68+
const release = getReleaseResponse.data;
69+
70+
const prNumbersInRelease = new Set(Array.from(release.body.matchAll(/\/pull\/(\d+)/g)).map(p=>p[1]));
71+
72+
for(let prNumber of prNumbersInRelease) {
73+
console.log(`Adding comment on PR #${prNumber} ...`);
74+
await github.rest.issues.createComment({
75+
issue_number: prNumber,
76+
owner: context.repo.owner,
77+
repo: context.repo.repo,
78+
body: `The changes in this PR were just released in [${release.name}](https://github.com/${context.repo.owner}/${context.repo.repo}/releases/tag/${release.tag_name}) 🎉.`
79+
})
80+
}

DEVELOPMENT.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@
1414

1515
## Publishing
1616

17-
1. Generate package: `poetry build`.
18-
1. Run `poetry config pypi-token.pypi YOUR_PYPI_API_TOKEN`
19-
1. Upload package: `poetry publish --build`.
17+
Run the "Publish" GitHub Actions workflow.

scripts/update_version.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import subprocess
4+
import re
5+
6+
def get_current_version():
7+
"""Get the current version from poetry without bumping it."""
8+
result = subprocess.run(["poetry", "version", "-s"], capture_output=True, text=True)
9+
return result.stdout.strip()
10+
11+
def bump_poetry_version(bump_type):
12+
"""Bump the poetry version with the specified bump type."""
13+
subprocess.run(["poetry", "version", "--quiet", bump_type], check=True)
14+
return get_current_version()
15+
16+
def update_openapi_config(version):
17+
"""Update the packageVersion in the openapi-generator-config.yaml file."""
18+
config_file = "openapi-generator-config.yaml"
19+
20+
# Read the current config file
21+
with open(config_file, 'r') as f:
22+
content = f.read()
23+
24+
# Update the packageVersion line
25+
updated_content = re.sub(
26+
r'packageVersion:\s*.*',
27+
f'packageVersion: {version}',
28+
content
29+
)
30+
31+
# Write the updated content back to the file
32+
with open(config_file, 'w') as f:
33+
f.write(updated_content)
34+
35+
def main():
36+
if len(sys.argv) != 2:
37+
print(f"Usage: {sys.argv[0]} <bump_type>")
38+
print("Example: patch|minor|major|1.4.1")
39+
sys.exit(1)
40+
41+
bump_type = sys.argv[1]
42+
43+
# Bump the version in pyproject.toml
44+
version = bump_poetry_version(bump_type)
45+
46+
# Update the version in openapi-generator-config.yaml
47+
update_openapi_config(version)
48+
print(f"{version}")
49+
50+
if __name__ == "__main__":
51+
main()

0 commit comments

Comments
 (0)