name: Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Release version"
        required: true
jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up Java
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-cache-${{ hashFiles('**/*.gradle') }}-${{ hashFiles('**/gradle.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - uses: actions/cache@v2
        with:
          path: ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradlew') }}
          restore-keys: |
            ${{ runner.os }}-gradlew-

      - name: Set release version
        run: |
          VERSION=${{ github.event.inputs.version }}
          echo $VERSION > VERSION
          git add VERSION
          sed -i -e "s/^\:project-version\:\ .*/:project-version: ${VERSION}/g" README.adoc
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "GitHub Action"
          git commit -a -m "Releasing version $VERSION"
          git push origin main

      - name: Build
        run: |
          ./gradlew -Prelease=true build -S

      - name: Deploy
        env:
          GPG_KEY_ID: ${{ secrets.GPG_KEY_ID }}
          GPG_SECRET_KEY: ${{ secrets.GPG_SECRET_KEY }}
          GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
          GRADLE_PUBLISH_KEY: ${{ secrets.GRADLE_PUBLISH_KEY }}
          GRADLE_PUBLISH_SECRET: ${{ secrets.GRADLE_PUBLISH_SECRET }}
        run: |
          ./gradlew -Prelease=true -Pfull-release=true \
          -PsonatypeUsername=${{ secrets.SONATYPE_USERNAME }} \
          -PsonatypePassword=${{ secrets.SONATYPE_PASSWORD }} \
          build publishToSonatype publishPlugins -S

      - name: Assemble
        uses: jreleaser/release-action@v1
        with:
          version: early-access
          arguments: assemble
        env:
          JRELEASER_PROJECT_VERSION: ${{ github.event.inputs.version }}
          JRELEASER_GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }}

      - name: Release
        uses: jreleaser/release-action@v1
        with:
          version: early-access
          arguments: full-release
        env:
          JRELEASER_PROJECT_VERSION: ${{ github.event.inputs.version }}
          JRELEASER_GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }}
          JRELEASER_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
          JRELEASER_GPG_PUBLIC_KEY: ${{ secrets.GPG_PUBLIC_KEY }}
          JRELEASER_GPG_SECRET_KEY: ${{ secrets.GPG_SECRET_KEY }}
          JRELEASER_DOCKER_DEFAULT_PASSWORD: ${{ secrets.JRELEASER_DOCKER_PASSWORD }}
          JRELEASER_SDKMAN_CONSUMER_KEY: ${{ secrets.SDKMAN_CONSUMER_KEY }}
          JRELEASER_SDKMAN_CONSUMER_TOKEN: ${{ secrets.SDKMAN_CONSUMER_TOKEN }}
          JRELEASER_TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }}
          JRELEASER_TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }}
          JRELEASER_TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }}
          JRELEASER_TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}

      - name: JReleaser output
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: artifact
          path: |
            out/jreleaser/trace.log
            out/jreleaser/output.properties

  update:
    name: Update version
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          repository: jreleaser/jreleaser.github.io
          ref: 'main'
          token: ${{ secrets.GIT_ACCESS_TOKEN }}

      - name: Commit
        run: |
          VERSION=${{ github.event.inputs.version }}
          echo $VERSION > VERSION
          git add VERSION
          git config --global user.email "jreleaser@kordamp.org"
          git config --global user.name "jreleaserbot"
          git commit -a -m "Releasing version $VERSION"
          git push origin main