Skip to content

Commit fd06998

Browse files
Merge pull request #343 from VWS-Python/gha-tag-release
Try to tag a release from a GitHub action
2 parents a4cce19 + 2a22cd6 commit fd06998

File tree

3 files changed

+27
-57
lines changed

3 files changed

+27
-57
lines changed

.github/workflows/release.yml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,6 @@ jobs:
2121
with:
2222
python-version: ${{ matrix.python-version }}
2323

24-
- uses: actions/cache@v2
25-
with:
26-
path: ~/.cache/pip
27-
# This is like the example but we use ``*requirements.txt`` rather
28-
# than ``requirements.txt`` because we have multiple requirements
29-
# files.
30-
key: ${{ runner.os }}-pip-${{ hashFiles('**/*requirements.txt') }}
31-
restore-keys: |
32-
${{ runner.os }}-pip-
33-
34-
- name: "Install dependencies"
35-
run: |
36-
python -m pip install --upgrade pip setuptools wheel
37-
# We use '--ignore-installed' to avoid GitHub's cache which can cause
38-
# issues - we have seen packages from this cache cause trouble with
39-
# pip-extra-reqs.
40-
python -m pip install --ignore-installed --upgrade --editable .[dev]
41-
4224
- name: "Calver calculate version"
4325
uses: StephaneBour/actions-calver@master
4426
id: calver
@@ -48,18 +30,40 @@ jobs:
4830
env:
4931
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5032

51-
- name: "Publish a release"
33+
- name: "Update changelog"
5234
env:
5335
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
5436
NEXT_VERSION: ${{ steps.calver.outputs.release }}
5537
run: |
5638
python admin/release.py
5739
40+
- uses: stefanzweifel/git-auto-commit-action@v4
41+
id: commit
42+
with:
43+
commit_message: Bump CHANGELOG
44+
45+
- name: Bump version and push tag
46+
id: tag_version
47+
uses: mathieudutour/[email protected]
48+
with:
49+
github_token: ${{ secrets.GITHUB_TOKEN }}
50+
custom_tag: ${{ steps.calver.outputs.release }}
51+
tag_prefix: ""
52+
commit_sha: ${{ steps.commit.outputs.commit_hash }}
53+
54+
- name: Create a GitHub release
55+
uses: ncipollo/release-action@v1
56+
with:
57+
tag: ${{ steps.tag_version.outputs.new_tag }}
58+
name: Release ${{ steps.tag_version.outputs.new_tag }}
59+
body: ${{ steps.tag_version.outputs.changelog }}
60+
5861
- name: Build a binary wheel and a source tarball
5962
run: |
6063
# Checkout the latest tag - the one we just created.
6164
git fetch --tags
6265
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
66+
python -m pip install build
6367
python -m build --sdist --wheel --outdir dist/ .
6468
6569
- name: Publish distribution 📦 to PyPI

admin/release.py

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,26 @@
55
import os
66
from pathlib import Path
77

8-
from github import Github
9-
from github.ContentFile import ContentFile
10-
from github.Repository import Repository
118

12-
13-
def update_changelog(version: str, github_repository: Repository) -> None:
9+
def update_changelog(version: str) -> None:
1410
"""
1511
Add a version title to the changelog.
1612
"""
1713
changelog_path = Path('CHANGELOG.rst')
18-
branch = 'master'
19-
changelog_content_file = github_repository.get_contents(
20-
path=str(changelog_path),
21-
ref=branch,
22-
)
23-
# ``get_contents`` can return a ``ContentFile`` or a list of
24-
# ``ContentFile``s.
25-
assert isinstance(changelog_content_file, ContentFile)
26-
changelog_bytes = changelog_content_file.decoded_content
27-
changelog_contents = changelog_bytes.decode('utf-8')
14+
changelog_contents = changelog_path.read_text(encoding='utf-8')
2815
new_changelog_contents = changelog_contents.replace(
2916
'Next\n----',
3017
f'Next\n----\n\n{version}\n------------',
3118
)
32-
github_repository.update_file(
33-
path=str(changelog_path),
34-
message=f'Update for release {version}',
35-
content=new_changelog_contents,
36-
sha=changelog_content_file.sha,
37-
)
19+
changelog_path.write_text(new_changelog_contents, encoding='utf-8')
3820

3921

4022
def main() -> None:
4123
"""
4224
Perform a release.
4325
"""
44-
github_token = os.environ['GITHUB_TOKEN']
45-
github_repository_name = os.environ['GITHUB_REPOSITORY']
4626
version_str = os.environ['NEXT_VERSION']
47-
github_client = Github(github_token)
48-
github_repository = github_client.get_repo(
49-
full_name_or_id=github_repository_name,
50-
)
51-
update_changelog(version=version_str, github_repository=github_repository)
52-
github_repository.create_git_tag_and_release(
53-
tag=version_str,
54-
tag_message='Release ' + version_str,
55-
release_name='Release ' + version_str,
56-
release_message='See CHANGELOG.rst',
57-
type='commit',
58-
object=github_repository.get_commits()[0].sha,
59-
)
27+
update_changelog(version=version_str)
6028

6129

6230
if __name__ == '__main__':

requirements/dev-requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
PyGithub==1.55
21
Sphinx-Substitution-Extensions==2020.9.30.0
32
autoflake==1.4
43
black==21.12b0
5-
build==0.7.0
64
check-manifest==0.47
75
doc8==0.10.1
86
flake8-commas==2.1.0

0 commit comments

Comments
 (0)