Last active
August 3, 2025 16:31
-
-
Save justADeni/7adf495d570dd00da0ea6c67e0cabb7e to your computer and use it in GitHub Desktop.
Actions workflow to tag, build and release a maven project
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: Auto Tag, Build, and Release | |
| on: | |
| workflow_dispatch: | |
| push: | |
| paths: | |
| - 'pom.xml' | |
| - 'src/**' | |
| pull_request: | |
| paths: | |
| - 'pom.xml' | |
| - 'src/**' | |
| jobs: | |
| all: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set Environment Variables | |
| run: | | |
| VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) | |
| NAME=$(mvn help:evaluate -Dexpression=project.name -q -DforceStdout) | |
| echo "VERSION=$VERSION" >> $GITHUB_ENV | |
| echo "NAME=$NAME" >> $GITHUB_ENV | |
| - name: Create Tag | |
| run: | | |
| git config --local user.email "[email protected]" | |
| git config --local user.name "GitHub Action" | |
| git tag -a "${{ env.VERSION }}" -m "Release ${{ env.VERSION }}" | |
| git push origin "${{ env.VERSION }}" | |
| - name: Set up Java | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: 'temurin' | |
| java-version: '21' | |
| - name: Extract dependencies block and hash it | |
| id: hash | |
| run: | | |
| awk '/<dependencies>/,/<\/dependencies>/' pom.xml > pom.dependencies.xml | |
| echo "hash=$(sha256sum pom.dependencies.xml | cut -d " " -f1)" >> $GITHUB_OUTPUT | |
| - name: Cache Maven repo | |
| uses: actions/cache@v4 | |
| with: | |
| path: ~/.m2/repository | |
| key: maven-${{ runner.os }}-${{ steps.hash.outputs.hash }} | |
| restore-keys: | | |
| maven-${{ runner.os }}- | |
| - name: Build with Maven | |
| run: mvn -B package | |
| - name: Release | |
| uses: ncipollo/release-action@v1 | |
| with: | |
| artifacts: "target/${{ env.NAME }}-${{ env.VERSION }}.jar" | |
| artifactErrorsFailBuild: true | |
| generateReleaseNotes: true | |
| makeLatest: true | |
| tag: "${{ env.VERSION }}" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: In order for this to work, you must enable this setting
Repository->Settings->Actions->General->Workflow permissions:[x] Read and write permissions