name: publish to sonatype

on:
  push:
    tags:
      - 'v*'
jobs:
  generate_publish_id:
    runs-on: ubuntu-latest
    name: Create staging repository
    outputs:
      repository_id: ${{ steps.create.outputs.repository_id }}
    steps:
      - id: create
        uses: nexus-actions/create-nexus-staging-repo@3e5e7209801629febdcf75541a4898710d28df9a
        with:
          username: ${{ secrets.PUBLISH_REPOSITORY_USERNAME }}
          password: ${{ secrets.PUBLISH_REPOSITORY_PASSWORD }}
          staging_profile_id: ${{ secrets.SONATYPE_PROFILE_ID }}
          base_url: ${{ secrets.SONATYPE_BASE_URL }}
  publish:
    needs: generate_publish_id
    strategy:
      matrix:
        include:
          - os: macos-latest
            gradlew: gradlew
          - os: self-hosted
            gradlew: gradlew
            env: all
    runs-on: ${{ matrix.os }}
    steps:
      - name: Print publishing type
        run: echo "This runner with env type = ${{ matrix.env == 'all' && 'all' || 'native' }}"
      - name: Checkout
        uses: actions/checkout@v3
      - name: Configure JDK
        uses: actions/setup-java@v3
        with:
          distribution: '$JAVA_DISTRIBUTION$'
          java-version: '$JAVA_VERSION$'
      - name: Fetch origin/$GIT_DEFAULT_BRANCH$
        run: git fetch origin $GIT_DEFAULT_BRANCH$
      - name: Publish
        run: ./${{ matrix.gradlew }} clean publish --no-daemon --no-parallel --stacktrace
        env:
          ORG_GRADLE_PROJECT_simple.kmm.publish.repository.url: ${{ secrets.PUBLISH_REPOSITORY_URL }}
          ORG_GRADLE_PROJECT_simple.kmm.publish.repository.id: ${{ needs.generate_publish_id.outputs.repository_id }}
          ORG_GRADLE_PROJECT_simple.kmm.publish.username: ${{ secrets.PUBLISH_REPOSITORY_USERNAME }}
          ORG_GRADLE_PROJECT_simple.kmm.publish.password: ${{ secrets.PUBLISH_REPOSITORY_PASSWORD }}
          ORG_GRADLE_PROJECT_simple.kmm.sign.key.id: ${{ secrets.SIGNING_KEY_ID }}
          ORG_GRADLE_PROJECT_simple.kmm.sign.password: ${{ secrets.SIGNING_PASSWORD }}
          ORG_GRADLE_PROJECT_simple.kmm.sign.private.ke: ${{ secrets.SIGNING_PRIVATE_KEY }}
          ORG_GRADLE_PROJECT_simple.kmm.kotlin.compile.only.platform: ${{ matrix.env == 'all' && 'false' || 'true' }}
          ORG_GRADLE_PROJECT_simple.kmm.kotlin.compile.browser.enabled: false