|
| 1 | +name: Create Tag and Release on Version Change |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + paths: |
| 6 | + - src/__init__.py |
| 7 | + branches: |
| 8 | + - python3 |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + tag_and_release: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: Read version from file |
| 23 | + id: get_version |
| 24 | + run: | |
| 25 | + VERSION=$(grep -oP '(?<=__version__ = ")[^"]+' src/__init__.py) |
| 26 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 27 | +
|
| 28 | + - name: Check if tag already exists |
| 29 | + id: check_tag |
| 30 | + run: | |
| 31 | + if git ls-remote --tags origin | grep -q "refs/tags/v${{ steps.get_version.outputs.version }}$"; then |
| 32 | + echo "Tag v${{ steps.get_version.outputs.version }} already exists." |
| 33 | + echo "skip=true" >> $GITHUB_OUTPUT |
| 34 | + exit 0 |
| 35 | + fi |
| 36 | +
|
| 37 | + - name: Create and push tag |
| 38 | + if: steps.check_tag.outputs.skip != 'true' |
| 39 | + run: | |
| 40 | + git config user.name "github-actions[bot]" |
| 41 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 42 | + git tag v${{ steps.get_version.outputs.version }} |
| 43 | + git push origin v${{ steps.get_version.outputs.version }} |
| 44 | +
|
| 45 | + - name: Generate changelog |
| 46 | + id: changelog |
| 47 | + if: steps.check_tag.outputs.skip != 'true' |
| 48 | + run: | |
| 49 | + LAST_TAG=$(git describe --tags --abbrev=0 --exclude "v${{ steps.get_version.outputs.version }}" 2>/dev/null || echo "") |
| 50 | +
|
| 51 | + if [ -z "$LAST_TAG" ]; then |
| 52 | + echo "No previous tag found, showing last 10 commits." |
| 53 | + CHANGELOG=$(git log --pretty=format:"- %s (%h)" --no-merges -n 10) |
| 54 | + else |
| 55 | + echo "Previous tag: $LAST_TAG" |
| 56 | + CHANGELOG=$(git log "$LAST_TAG"..HEAD --pretty=format:"- %s (%h)" --no-merges -n 10) |
| 57 | + fi |
| 58 | +
|
| 59 | + # Escape only % to avoid GitHub Actions output issues |
| 60 | + CHANGELOG="${CHANGELOG//'%'/'%25'}" |
| 61 | +
|
| 62 | + echo "changelog<<EOF" >> $GITHUB_OUTPUT |
| 63 | + echo "$CHANGELOG" >> $GITHUB_OUTPUT |
| 64 | + echo "EOF" >> $GITHUB_OUTPUT |
| 65 | +
|
| 66 | + - name: Create GitHub Release |
| 67 | + if: steps.check_tag.outputs.skip != 'true' |
| 68 | + uses: softprops/action-gh-release@v2 |
| 69 | + with: |
| 70 | + tag_name: v${{ steps.get_version.outputs.version }} |
| 71 | + name: Release v${{ steps.get_version.outputs.version }} |
| 72 | + body: | |
| 73 | + ## Changes in this release |
| 74 | + ${{ steps.changelog.outputs.changelog }} |
| 75 | + env: |
| 76 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments