---
name: release
on:
  push:
    branches:
      - main
jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    if: ${{ !startsWith(github.event.head_commit.message, 'Release') }}
    env:
      JRELEASER_OUTPUT_DIRECTORY: target

    steps:
      - name: "Initialize Environment"
        id: initialize
        uses: https://w9r.dev/w9r.dev/action-setup-environment@v1.1.4
        with:
          roleid: ${{ secrets.VAULT_ROLE_ID }}
          secretid: ${{ secrets.VAULT_SECRET_ID }}

      - name: "Determine next version"
        uses: https://github.com/obfu5c8/action-svu@e0ac511a90318bc284bf845936acd5c5c077d788 # v1
        id: generate_next_version
        with:
          type: auto
          force-increment: true
          no-metadata: false
          no-pre-release: false
          no-build: false
          strip-prefix: true
          prefix: ''
          suffix: ''

      - name: "Set new version"
        run: |
          NEW_VERSION=${{steps.generate_next_version.outputs.version}}
          echo NEW_VERSION=$NEW_VERSION >> "$GITHUB_ENV"
          echo JRELEASER_PROJECT_VERSION=$NEW_VERSION >> "$GITHUB_ENV"
          echo "New version: $NEW_VERSION"
          mvn -B --file pom.xml versions:set -DnewVersion=$NEW_VERSION

      - name: "Run JReleaser (Changelog)"
        uses: https://github.com/jreleaser/release-action@f69e545b05f149483cecb2fb81866247992694b8 # 2.4.2
        with:
          arguments: changelog --debug
          setup-java: false
        continue-on-error: true

      - name: "Commit and push changes"
        uses: https://w9r.dev/w9r.dev/action-git-commit-push@v1.4
        with:
          email: ${{ steps.initialize.outputs.gitemail }}
          name: ${{ steps.initialize.outputs.gituser }}
          commit_message: "Release ${{ env.JRELEASER_PROJECT_VERSION }} [skip ci]"
          files: pom.xml CHANGELOG.md
          access_token: ${{ env.JRELEASER_GITEA_TOKEN }}

      - name: Build package and populate staging area for deployment
        run: |
          mvn -X -B --file pom.xml package
          mvn -X --file pom.xml -Ppublication

      - name: "Run JReleaser (Assemble)"
        uses: https://github.com/jreleaser/release-action@f69e545b05f149483cecb2fb81866247992694b8 # 2.4.2
        with:
          arguments: assemble --debug
          setup-java: false
        continue-on-error: true

      - name: "Run JReleaser (Release)"
        uses: https://github.com/jreleaser/release-action@f69e545b05f149483cecb2fb81866247992694b8 # 2.4.2
        with:
          arguments: release --debug
          setup-java: false

      - name: JReleaser release trace
        if: always()
        uses: https://gitea.com/actions/upload-artifact@ff15f0306b3f739f7b6fd43fb5d26cd321bd4de5 # v3
        with:
          name: jreleaser-trace
          path: target/jreleaser/trace.log

      - name: JReleaser release properties
        if: always()
        uses: https://gitea.com/actions/upload-artifact@ff15f0306b3f739f7b6fd43fb5d26cd321bd4de5 # v3
        with:
          name: jreleaser-properties
          path: target/jreleaser/output.properties