name: release

on:
  workflow_dispatch:
    inputs:
      previousDevelopmentVersion:
        description: 'The previous development version'
        required: true
      currentDevelopmentVersion:
        description: 'The current development version'
        required: true
      nextDevelopmentVersion:
        description: 'The next development version'
        required: true

jobs:
  perform-release:
    name: Release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Java
        uses: actions/setup-java@v5
        with:
          java-version: 21
          distribution: 'zulu'
          server-id: central
          server-username: MAVEN_USERNAME
          server-password: MAVEN_CENTRAL_TOKEN
          gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
          gpg-passphrase: MAVEN_GPG_PASSPHRASE
          cache: maven

#         We need to do this because there are some files that are auto-generated/auto-updated during the build.
#         When creating the release, we want to make sure that any version referenced in there is actually related to
#         the version being released. So, the process involves adjusting the versioned files, then recreating the tag
      - name: Create release
        env:
          MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
          MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_CENTRAL_TOKEN }}
          MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
        run: |
          export GPG_TTY=$(tty)
          git config user.name "${{ github.actor }}"
          git config user.email "${{ github.actor }}@users.noreply.github.com"
          mvn --batch-mode --no-transfer-progress -Dtag=wanaku-${{ github.event.inputs.currentDevelopmentVersion }} -DreleaseVersion=${{ github.event.inputs.currentDevelopmentVersion }} -DdevelopmentVersion=${{ github.event.inputs.nextDevelopmentVersion }}-SNAPSHOT release:prepare
          sed -i -e "s/${{ github.event.inputs.previousDevelopmentVersion }}/${{ github.event.inputs.currentDevelopmentVersion }}/g" jbang-catalog.json
          mvn --no-transfer-progress -PcommitFiles scm:checkin
          git tag -d wanaku-${{ github.event.inputs.currentDevelopmentVersion }}
          git tag wanaku-${{ github.event.inputs.currentDevelopmentVersion }} HEAD~2
          git push origin wanaku-${{ github.event.inputs.currentDevelopmentVersion }}
          mvn --no-transfer-progress -Pdist release:perform