Skip to content

Commit 7f03ff6

Browse files
committed
docs(gh): update gh action tutorial
1 parent 2f86834 commit 7f03ff6

1 file changed

Lines changed: 67 additions & 122 deletions

File tree

‎docs/tutorials/github_actions.md‎

Lines changed: 67 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Before setting up the workflow, you'll need:
1212
1. A personal access token with repository write permissions
1313
2. Commitizen configured in your project (see [configuration documentation](../config/configuration_file.md))
1414

15-
### Automatic version bumping
15+
### Automatic Version Bumping
1616

1717
To automatically execute `cz bump` in your CI and push the new commit and tag back to your repository, follow these steps:
1818

19-
#### Step 1: Create a personal access token
19+
#### Step 1: Create a Personal Access Token
2020

2121
1. Go to [GitHub Settings > Developer settings > Personal access tokens](https://github.com/settings/tokens)
2222
2. Click "Generate new token (classic)"
@@ -27,7 +27,7 @@ To automatically execute `cz bump` in your CI and push the new commit and tag ba
2727
!!! warning "Important: Use Personal Access Token, not GITHUB_TOKEN"
2828
If you use `GITHUB_TOKEN` instead of `PERSONAL_ACCESS_TOKEN`, the workflow won't trigger another workflow run. This is a GitHub security feature to prevent infinite loops. The `GITHUB_TOKEN` is treated like using `[skip ci]` in other CI systems.
2929

30-
#### Step 2: Add the token as a repository secret
30+
#### Step 2: Add the Token as a Repository Secret
3131

3232
1. Go to your repository on GitHub
3333
2. Navigate to `Settings > Secrets and variables > Actions`
@@ -36,97 +36,82 @@ To automatically execute `cz bump` in your CI and push the new commit and tag ba
3636
5. Paste the token you copied in Step 1
3737
6. Click "Add secret"
3838

39-
#### Step 3: Create the workflow file
39+
#### Step 3: Create the Workflow File
4040

41-
Create a new file `.github/workflows/bumpversion.yml` in your repository with the following content:
41+
Create a new file `.github/workflows/bump-version.yml` in your repository with the following content:
4242

43-
```yaml title=".github/workflows/bumpversion.yml"
43+
```yaml title=".github/workflows/bump-version.yml"
4444
name: Bump version
4545

4646
on:
4747
push:
4848
branches:
49-
- master # or 'main' if that's your default branch
49+
- main
5050

5151
jobs:
52-
bump-version:
53-
if: "!startsWith(github.event.head_commit.message, 'bump:')"
52+
bump:
5453
runs-on: ubuntu-latest
55-
name: "Bump version and create changelog with commitizen"
54+
permissions:
55+
contents: write
56+
actions: write
5657
steps:
57-
- name: Check out
58-
uses: actions/checkout@v6
58+
- uses: actions/checkout@v7
5959
with:
60-
token: "${{ secrets.PERSONAL_ACCESS_TOKEN }}"
6160
fetch-depth: 0
62-
- name: Create bump and changelog
63-
uses: commitizen-tools/commitizen-action@master
61+
fetch-tags: true
62+
- uses: commitizen-tools/setup-cz@main
6463
with:
65-
github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
64+
python-version: "3.x"
65+
- id: bump-version
66+
run: |
67+
old_sha="$(git rev-parse HEAD)"
68+
69+
cz --no-raise 21 bump --yes --annotated-tag
70+
71+
if [ "$(git rev-parse HEAD)" = "$old_sha" ]; then
72+
echo "No bump-eligible commits found, skipping release."
73+
echo "bumped=false" >> $GITHUB_OUTPUT
74+
exit 0
75+
fi
76+
77+
echo "bumped=true" >> $GITHUB_OUTPUT
78+
git push --follow-tags
79+
new_version="$(cz version -p)"
80+
echo "new_version=$new_version" >> $GITHUB_OUTPUT
81+
new_version_tag="$(cz version -p --tag)"
82+
echo "new_version_tag=$new_version_tag" >> $GITHUB_OUTPUT
83+
- name: Github Release
84+
if: steps.bump-version.outputs.bumped == 'true'
85+
env:
86+
GH_TOKEN: ${{ github.token }}
87+
NEW_VERSION: ${{ steps.bump-version.outputs.new_version }}
88+
NEW_VERSION_TAG: ${{ steps.bump-version.outputs.new_version_tag }}
89+
run: |
90+
gh release create "${NEW_VERSION_TAG}" --notes-file .changelog.md
91+
cz changelog --dry-run "${NEW_VERSION}" > .changelog.md
6692
```
6793
6894
#### How it works
6995
70-
- **Trigger**: The workflow runs on every push to the `master` branch (or `main` if you change it)
71-
- **Conditional check**: The `if` condition prevents infinite loops by skipping the job if the commit message starts with `bump:`
72-
- **Checkout**: Uses your personal access token to check out the repository with full history (`fetch-depth: 0`)
73-
- **Bump**: The `commitizen-action` automatically:
96+
- The action will trigger the workflow on every push to the `main` branch.
97+
- The job requests `contents: write` and `actions: write` so it can push the bump commit and tag, and trigger dependent workflows.
98+
- **Setup**: The `setup-cz` action installs the Commitizen CLI with the requested Python version
99+
- **Bump**: The `cz bump --yes --annotated-tag` command automatically:
74100
- Determines the version increment based on your commit messages
75101
- Updates version files (as configured in your `pyproject.toml` or other config)
76-
- Creates a new git tag
102+
- Creates a new annotated git tag
77103
- Generates/updates the changelog
78-
- Pushes the commit and tag back to the repository
104+
- **Push**: `git push --follow-tags` pushes the bump commit along with the new tag back to the repository
105+
- **Github Release**: creates a Github Release
79106

80107
Once you push this workflow file to your repository, it will automatically run on the next push to your default branch.
81108

82-
Check out [commitizen-action](https://github.com/commitizen-tools/commitizen-action) for more details.
83-
84-
### Creating a GitHub release
85-
86-
To automatically create a GitHub release when a new version is bumped, you can extend the workflow above.
109+
Check out [commitizen-tools/setup-cz](https://github.com/commitizen-tools/setup-cz) for more details.
87110

88-
The `commitizen-action` creates an environment variable called `REVISION` containing the newly created version. You can use this to create a release with the changelog content.
89-
90-
```yaml title=".github/workflows/bumpversion.yml"
91-
name: Bump version
92-
93-
on:
94-
push:
95-
branches:
96-
- master # or 'main' if that's your default branch
97-
98-
jobs:
99-
bump-version:
100-
if: "!startsWith(github.event.head_commit.message, 'bump:')"
101-
runs-on: ubuntu-latest
102-
name: "Bump version and create changelog with commitizen"
103-
steps:
104-
- name: Check out
105-
uses: actions/checkout@v6
106-
with:
107-
token: "${{ secrets.PERSONAL_ACCESS_TOKEN }}"
108-
fetch-depth: 0
109-
- name: Create bump and changelog
110-
uses: commitizen-tools/commitizen-action@master
111-
with:
112-
github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
113-
changelog_increment_filename: body.md
114-
- name: Release
115-
uses: ncipollo/release-action@v1
116-
with:
117-
tag: v${{ env.REVISION }}
118-
bodyFile: "body.md"
119-
skipIfReleaseExists: true
120-
env:
121-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
122-
```
123-
124-
You can find the complete workflow in our repository at [bumpversion.yml](https://github.com/commitizen-tools/commitizen/blob/master/.github/workflows/bumpversion.yml).
125-
126-
### Previewing the version bump on pull requests
111+
### Previewing the Version Bump on Pull Requests
127112

128113
To help reviewers spot unexpected version bumps before merging, you can run
129-
`cz bump --dry-run` on every pull request and post (or update) a sticky
114+
`cz version -p` on every pull request and post (or update) a sticky
130115
comment summarizing the would-be version bump.
131116

132117
Create `.github/workflows/pr-bump-preview.yml`:
@@ -153,21 +138,20 @@ jobs:
153138
}}
154139
runs-on: ubuntu-latest
155140
steps:
156-
- name: Check out PR head
157-
uses: actions/checkout@v6
141+
- uses: actions/checkout@v7
158142
with:
159-
ref: ${{ github.event.pull_request.head.sha }}
160143
fetch-depth: 0
161144
fetch-tags: true
162145
persist-credentials: false
163146
- uses: commitizen-tools/setup-cz@main
164147
with:
148+
python-version: "3.x"
165149
set-git-config: false
166-
- name: Run cz bump --dry-run
150+
- name: Run cz version
167151
id: dry-run
168152
run: |
169153
set +e
170-
output="$(cz bump --dry-run --yes 2>&1)"
154+
output="$(cz version -p --next 2>&1)"
171155
status=$?
172156
set -e
173157
{
@@ -222,40 +206,9 @@ jobs:
222206
edit-mode: replace
223207
```
224208
225-
#### How it works
226-
227-
- **Trigger**: `pull_request_target` runs in the context of the base
228-
repository, which gives the workflow `pull-requests: write` permission
229-
even for PRs from forks. We deliberately gate the job to **same-repo PRs
230-
only** (`head.repo == base.repo`); fork PRs are skipped. This is because
231-
`cz bump` renders [Jinja templates from the working directory][jinja]
232-
whenever [`update_changelog_on_bump`](../config/configuration_file.md) is
233-
enabled, and the renderer is not sandboxed — running it against
234-
fork-controlled files under a write token would risk arbitrary code
235-
execution and token exfiltration. Same-repo PRs are written by
236-
collaborators who already have push access, so the same risk doesn't
237-
apply.
238-
- **Setup**: [`commitizen-tools/setup-cz`](https://github.com/commitizen-tools/setup-cz)
239-
installs the Commitizen CLI; no language-specific build tooling is required.
240-
- **Defense in depth**: `persist-credentials: false` on `actions/checkout`
241-
keeps the workflow token out of the local git config.
242-
- **Dry-run**: `cz bump --dry-run --yes` computes the next version (and, if
243-
`update_changelog_on_bump` is set in your config, also the changelog
244-
entries that would be produced). Exit code `21` (`NoneIncrementExit`)
245-
is treated as "no eligible bump" rather than a failure.
246-
- **Sticky comment**: [`peter-evans/find-comment`](https://github.com/peter-evans/find-comment)
247-
looks up an existing comment by the hidden HTML marker
248-
`<!-- commitizen-bump-preview -->` and bot author, then
249-
[`peter-evans/create-or-update-comment`](https://github.com/peter-evans/create-or-update-comment)
250-
edits it in place (or creates a new one on the first run when the
251-
marker is not yet present), instead of leaving a growing trail of
252-
comments.
253-
254-
[jinja]: https://github.com/commitizen-tools/commitizen/blob/master/commitizen/changelog.py
255-
256209
You can find the complete workflow in our repository at [pr-bump-preview.yml](https://github.com/commitizen-tools/commitizen/blob/master/.github/workflows/pr-bump-preview.yml).
257210
258-
### Publishing a Python package
211+
### Publishing a Python Package
259212
260213
After a new version tag is created by the bump workflow, you can automatically publish your package to PyPI.
261214
@@ -280,7 +233,7 @@ After a new version tag is created by the bump workflow, you can automatically p
280233
5. Paste the PyPI token
281234
6. Click "Add secret"
282235

283-
#### Step 3: Create the publish workflow
236+
#### Step 3: Create the Publish Workflow
284237

285238
Create a new file `.github/workflows/pythonpublish.yml` that triggers on tag pushes:
286239

@@ -296,31 +249,23 @@ jobs:
296249
deploy:
297250
runs-on: ubuntu-latest
298251
steps:
299-
- uses: actions/checkout@v6
252+
- uses: actions/checkout@v7
300253
with:
301254
fetch-depth: 0
302255
- name: Set up Python
303-
uses: actions/setup-python@v4
256+
uses: actions/setup-python@v7
304257
with:
305258
python-version: "3.x"
306-
- name: Install Poetry
307-
uses: snok/install-poetry@v1
308-
with:
309-
version: latest
310-
virtualenvs-in-project: true
311-
virtualenvs-create: true
312-
- name: Install dependencies
259+
- name: Install the latest version of uv
260+
uses: astral-sh/setup-uv@v10
261+
- name: publish
313262
run: |
314-
poetry --version
315-
poetry install
316-
- name: Build and publish
317-
env:
318-
POETRY_HTTP_BASIC_PYPI_USERNAME: __token__
319-
POETRY_HTTP_BASIC_PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
320-
run: poetry publish --build
263+
uv sync
264+
uv publish --username "${PYPI_USERNAME}" --password "${PYPI_PASSWORD}"
321265
```
322266

323-
This workflow uses Poetry to build and publish the package. You can find the complete workflow in our repository at [pythonpublish.yml](https://github.com/commitizen-tools/commitizen/blob/master/.github/workflows/pythonpublish.yml).
267+
This workflow uses uv to build and publish the package.
268+
You can find the complete workflow in our repository at [pythonpublish.yml](https://github.com/commitizen-tools/commitizen/blob/master/.github/workflows/pythonpublish.yml).
324269

325270
!!! note "Alternative publishing methods"
326271
You can also use [pypa/gh-action-pypi-publish](https://github.com/pypa/gh-action-pypi-publish) or other build tools like `setuptools`, `flit`, or `hatchling` to publish your package.

0 commit comments

Comments
 (0)