ci: refine version bump workflow to handle no new version scenario #42
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Bump version and Create Release | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
test: | |
uses: ./.github/workflows/ci.yml | |
bump-version-and-release: | |
needs: test | |
if: ${{ !startsWith(github.event.head_commit.message, 'bump:') }} | |
runs-on: ubuntu-latest | |
name: "Bump version, create changelog, and release" | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.PAT }} | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: "3.x" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install commitizen | |
- name: Bump version and create changelog | |
id: cz | |
run: | | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "github-actions[bot]" | |
old_version=$(cz version --project) | |
output=$(cz bump --yes || echo "NO_BUMP") | |
new_version=$(cz version --project) | |
if [[ "$old_version" == "$new_version" ]]; then | |
echo "No new version to bump. Skipping release creation." | |
echo "create_release=false" >> $GITHUB_OUTPUT | |
else | |
echo "New version created: $new_version" | |
echo "create_release=true" >> $GITHUB_OUTPUT | |
fi | |
echo "version=$new_version" >> $GITHUB_OUTPUT | |
changelog=$(cz changelog || echo "No changes") | |
echo "changelog<<EOF" >> $GITHUB_OUTPUT | |
echo "$changelog" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
- name: Generate release notes | |
id: release_notes | |
if: steps.cz.outputs.create_release == 'true' | |
run: | | |
echo "notes<<EOF" >> $GITHUB_OUTPUT | |
echo "# Release v${{ steps.cz.outputs.version }}" >> $GITHUB_OUTPUT | |
echo "" >> $GITHUB_OUTPUT | |
echo "## What's Changed" >> $GITHUB_OUTPUT | |
echo "${{ steps.cz.outputs.changelog }}" >> $GITHUB_OUTPUT | |
echo "" >> $GITHUB_OUTPUT | |
echo "## Full Changelog" >> $GITHUB_OUTPUT | |
echo "[Compare with previous release](https://github.com/${{ github.repository }}/compare/v$(git describe --tags --abbrev=0 HEAD^)...v${{ steps.cz.outputs.version }})" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
- name: Push changes | |
if: steps.cz.outputs.create_release == 'true' | |
uses: ad-m/github-push-action@master | |
with: | |
github_token: ${{ secrets.PAT }} | |
branch: ${{ github.ref }} | |
tags: true | |
- name: Create Release | |
if: steps.cz.outputs.create_release == 'true' | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.PAT }} | |
with: | |
tag_name: v${{ steps.cz.outputs.version }} | |
release_name: Release ${{ steps.cz.outputs.version }} | |
body: ${{ steps.release_notes.outputs.notes }} | |
draft: false | |
prerelease: false |