|
28 | 28 |
|
29 | 29 | - name: Build and Test |
30 | 30 | run: mvn clean test post-integration-test |
| 31 | + |
| 32 | + # Tags and releases the version declared in pom.xml once it reaches the |
| 33 | + # default branch. A version that is already tagged is left alone. |
| 34 | + release: |
| 35 | + needs: test |
| 36 | + if: github.event_name == 'push' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch) |
| 37 | + runs-on: ubuntu-latest |
| 38 | + permissions: |
| 39 | + contents: write |
| 40 | + concurrency: |
| 41 | + group: release |
| 42 | + cancel-in-progress: false |
| 43 | + outputs: |
| 44 | + tag: ${{ steps.version.outputs.tag }} |
| 45 | + created: ${{ steps.release.outputs.created }} |
| 46 | + steps: |
| 47 | + - uses: actions/checkout@v4 |
| 48 | + |
| 49 | + - name: Read the version from pom.xml |
| 50 | + id: version |
| 51 | + run: | |
| 52 | + VERSION=$(awk '/<artifactId>ipdata-java-client<\/artifactId>/{f=1} f&&/<version>/{sub(/.*<version>/,"");sub(/<\/version>.*/,"");print;exit}' pom.xml) |
| 53 | + if [ -z "$VERSION" ]; then |
| 54 | + echo "::error file=pom.xml::Could not read the project version" |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 58 | + echo "tag=v$VERSION" >> "$GITHUB_OUTPUT" |
| 59 | +
|
| 60 | + - name: Create the tag and the GitHub release |
| 61 | + id: release |
| 62 | + env: |
| 63 | + GH_TOKEN: ${{ github.token }} |
| 64 | + TAG: ${{ steps.version.outputs.tag }} |
| 65 | + run: | |
| 66 | + status=0 |
| 67 | + git ls-remote --exit-code --tags origin "refs/tags/$TAG" > /dev/null || status=$? |
| 68 | + if [ "$status" -eq 0 ]; then |
| 69 | + echo "Tag $TAG already exists; nothing to release." |
| 70 | + echo "created=false" >> "$GITHUB_OUTPUT" |
| 71 | + exit 0 |
| 72 | + elif [ "$status" -ne 2 ]; then |
| 73 | + echo "::error::git ls-remote failed with status $status" |
| 74 | + exit "$status" |
| 75 | + fi |
| 76 | + gh release create "$TAG" --target "$GITHUB_SHA" --title "$TAG" --generate-notes |
| 77 | + echo "Created tag and release $TAG." |
| 78 | + echo "created=true" >> "$GITHUB_OUTPUT" |
| 79 | +
|
| 80 | + # A tag created with the workflow token does not trigger the Release |
| 81 | + # workflow on its own, so the Maven Central deploy runs in this run. |
| 82 | + publish: |
| 83 | + needs: release |
| 84 | + if: needs.release.outputs.created == 'true' |
| 85 | + uses: ./.github/workflows/release.yml |
| 86 | + with: |
| 87 | + tag: ${{ needs.release.outputs.tag }} |
| 88 | + secrets: inherit |
0 commit comments