name: Create release

on:
  push:
    tags:
      - 'v*'
    branches:
      - main
env:
  GPG_SEC: ${{ secrets.PGP_SEC }}
  GPG_PASSWORD: ${{ secrets.PGP_PASSWORD }}
  OSSRH_USERNAME: ${{ secrets.SONATYPE_USER }}
  OSSRH_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}

jobs:
  release:
    name: Build release
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ ubuntu-latest, windows-latest, macos-latest ]
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          # release workflow should have access to all tags
          fetch-depth: 0
      - name: Set up JDK 11
        uses: actions/setup-java@v4
        with:
          java-version: 11
          distribution: temurin
      - name: Status git before
        run: git status
      - name: gradle release from tag
        # if workflow is triggered after push of a tag, deploy full release
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        uses: gradle/gradle-build-action@v3
        with:
          gradle-version: wrapper
          arguments: |
            linkReleaseExecutableMultiplatform
            publishToSonatype
            closeSonatypeStagingRepository
            -Prelease
            --build-cache
            -PgprUser=${{ github.actor }}
            -PgprKey=${{ secrets.GITHUB_TOKEN }}
      - name: gradle snapshot from branch
        # if workflow is triggered after push to a branch, deploy snapshot
        if: ${{ startsWith(github.ref, 'refs/heads/') }}
        uses: gradle/gradle-build-action@v3
        with:
          gradle-version: wrapper
          arguments: |
            linkReleaseExecutableMultiplatform
            publishToSonatype
            -Preckon.stage=snapshot
            -Prelease
            --build-cache
            -PgprUser=${{ github.actor }}
            -PgprKey=${{ secrets.GITHUB_TOKEN }}
      - name: Status git after
        if: ${{ always() }}
        run: git status
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4
        with:
          name: save-cli-${{ runner.os }}
          path: save-cli/build/bin/**/releaseExecutable/*
          retention-days: 1

  github_release:
    needs: release
    # release is created only for tags
    if: ${{ startsWith(github.ref, 'refs/tags/') }}
    name: Create Github Release
    runs-on: ubuntu-latest
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          path: tmpFolder
      - name: Create Github Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: false
      - name: Upload save-cli
        id: upload-release-asset
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: tmpFolder/**/*
          tag: ${{ github.ref }}
          overwrite: true
          file_glob: true
