fix: release automatic versions #2
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: Semantic Release | |
| on: | |
| push: | |
| branches: | |
| - master | |
| workflow_dispatch: | |
| inputs: | |
| force: | |
| description: 'Force release even if tag exists' | |
| required: false | |
| default: false | |
| type: boolean | |
| jobs: | |
| release: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: read | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| fetch-tags: true | |
| - name: Set up JDK | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '8' | |
| distribution: 'temurin' | |
| - name: Extract version from pom.xml | |
| id: get_version | |
| run: | | |
| VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "tag=v$VERSION" >> $GITHUB_OUTPUT | |
| echo "Extracted version: $VERSION" | |
| - name: Fetch all tags | |
| run: git fetch --tags --force | |
| - name: Check if tag exists | |
| id: check_tag | |
| run: | | |
| TAG="${{ steps.get_version.outputs.tag }}" | |
| FORCE_RELEASE="${{ github.event.inputs.force }}" | |
| if [ "$FORCE_RELEASE" = "true" ] || [ "$FORCE_RELEASE" = true ]; then | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| echo "Force release enabled, proceeding with release" | |
| elif git rev-parse "$TAG" >/dev/null 2>&1; then | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| echo "Tag $TAG already exists, skipping release" | |
| else | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| echo "Tag $TAG does not exist, proceeding with release" | |
| fi | |
| - name: Generate release notes | |
| id: release_notes | |
| if: steps.check_tag.outputs.exists == 'false' | |
| run: | | |
| TAG="${{ steps.get_version.outputs.tag }}" | |
| PREVIOUS_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") | |
| if [ -z "$PREVIOUS_TAG" ]; then | |
| # First release - get all commits | |
| RELEASE_NOTES=$(git log --pretty=format:"- %s (%h)" --no-merges) | |
| else | |
| # Get commits since last tag | |
| RELEASE_NOTES=$(git log ${PREVIOUS_TAG}..HEAD --pretty=format:"- %s (%h)" --no-merges) | |
| fi | |
| # Create release notes with header | |
| { | |
| echo "## Release ${{ steps.get_version.outputs.version }}" | |
| echo "" | |
| echo "### Changes" | |
| echo "" | |
| echo "$RELEASE_NOTES" | |
| } > release_notes.txt | |
| # Output for GitHub release | |
| { | |
| echo "notes<<EOF" | |
| cat release_notes.txt | |
| echo "EOF" | |
| } >> $GITHUB_OUTPUT | |
| - name: Create Git tag | |
| if: steps.check_tag.outputs.exists == 'false' | |
| run: | | |
| TAG="${{ steps.get_version.outputs.tag }}" | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| # Delete tag if it exists locally (for force release) | |
| if git rev-parse "$TAG" >/dev/null 2>&1; then | |
| git tag -d "$TAG" || true | |
| fi | |
| git tag -a "$TAG" -m "Release ${{ steps.get_version.outputs.version }}" | |
| git push origin "$TAG" --force | |
| - name: Create GitHub Release | |
| if: steps.check_tag.outputs.exists == 'false' | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: ${{ steps.get_version.outputs.tag }} | |
| name: Release ${{ steps.get_version.outputs.version }} | |
| body: ${{ steps.release_notes.outputs.notes }} | |
| draft: false | |
| prerelease: false | |