name: Release

on:
  push:
    tags:
      - 'release*'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  #v4.2.2
      with:
        token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
        ref: "main"
        fetch-tags: true

    - name: Setup git
      run: |
        git config --global user.name "Stephen Colebourne (CI)"
        git config --global user.email "scolebourne@joda.org"

    - name: Set up JDK
      uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12  #v4.7.0
      with:
        java-version: 8
        distribution: 'temurin'
        cache: 'maven'

    - name: Maven version
      run: |
        mkdir -p ./.mvn
        echo "-e" >> ./.mvn/maven.config
        echo "-B" >> ./.mvn/maven.config
        echo "-ntp" >> ./.mvn/maven.config
        echo "-DtrimStackTrace=false" >> ./.mvn/maven.config
        echo "--settings" >> ./.mvn/maven.config
        echo "$( pwd )/.github/maven-settings.xml" >> ./.mvn/maven.config
        mvn --version
        mkdir -p target

    #------------------------------------------------------------------------
    - name: Maven release
      env:
        MAVEN_CENTRAL_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
        MAVEN_CENTRAL_PASSWORD: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
        MAVEN_GPG_PASSPHRASE: ${{ secrets.RELEASES_GPG_PASSPHRASE }}
        MAVEN_GPG_KEY: ${{ secrets.RELEASES_GPG_PRIVATE_KEY }}
        GITHUB_TOKEN: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
      run: |
        mvn clean deploy -Doss.repo

    - name: Update website
      run: |
        git tag websiterelease
        git push origin websiterelease

    # delete the initiating tag, but not if the build is manually initialized by workflow_dispatch
    - name: Delete release tag
      if: "always() && github.ref != 'refs/heads/main'"
      run: |
        git tag --delete "${GITHUB_REF_NAME}" || true
        git push --delete origin "${GITHUB_REF_NAME}" || true
