name: Publish bdk-jvm to Maven Central
on: [workflow_dispatch]

jobs:
  build-macOS-native-libs:
    name: "Create M1 and x86_64 native binaries"
    runs-on: macos-12
    steps:
      - name: "Checkout publishing branch"
        uses: actions/checkout@v3

      - name: "Cache"
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            ./target
          key: ${{ runner.os }}-${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}

      - name: "Set up JDK"
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17

      - name: "Set default Rust version to 1.73.0"
        run: rustup default 1.73.0

      - name: "Install aarch64 Rust target"
        run: rustup target add aarch64-apple-darwin

      - name: "Build bdk-jvm library"
        run: |
          cd bdk-jvm
          ./gradlew buildJvmLib

      - name: "Upload macOS native libraries for reuse in publishing job"
        uses: actions/upload-artifact@v3
        with:
          name: artifact-macos
          path: /Users/runner/work/bdk-ffi/bdk-ffi/bdk-jvm/lib/src/main/resources/

  build-windows-native-lib:
    name: "Create Windows native binaries"
    runs-on: windows-2022
    steps:
      - name: "Checkout publishing branch"
        uses: actions/checkout@v3

      - name: "Set up JDK"
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17

      - name: "Set default Rust version to 1.73.0"
        run: rustup default 1.73.0

      - name: "Install x86_64-pc-windows-msvc Rust target"
        run: rustup target add x86_64-pc-windows-msvc

      - name: "Build bdk-jvm library"
        run: |
          cd bdk-jvm
          ./gradlew buildJvmLib

      - name: "Upload Windows native libraries for reuse in publishing job"
        uses: actions/upload-artifact@v3
        with:
          name: artifact-windows
          path: D:\a\bdk-ffi\bdk-ffi\bdk-jvm\lib\src\main\resources\

  build-full-library:
    name: Create full bdk-jvm library
    needs: [build-macOS-native-libs, build-windows-native-lib]
    runs-on: ubuntu-20.04
    steps:
      - name: "Checkout publishing branch"
        uses: actions/checkout@v3

      - name: "Cache"
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            ./target
          key: ${{ runner.os }}-${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}

      - name: "Set up JDK"
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17

      - name: "Set default Rust version to 1.73.0"
        run: rustup default 1.73.0

      - name: "Build bdk-jvm library"
        run: |
          cd bdk-jvm
          ./gradlew buildJvmLib

      - name: "Download macOS native binaries from previous job"
        uses: actions/download-artifact@v3
        with:
          name: artifact-macos
          path: ./bdk-jvm/lib/src/main/resources/

      - name: "Download Windows native libraries from previous job"
        uses: actions/download-artifact@v3
        with:
          name: artifact-windows
          path: ./bdk-jvm/lib/src/main/resources/

      - name: "Upload library code and binaries"
        uses: actions/upload-artifact@v3
        with:
          name: artifact-full
          path: ./bdk-jvm/lib/

      - name: "Publish to Maven Central"
        env:
          ORG_GRADLE_PROJECT_signingKeyId: ${{ secrets.PGP_KEY_ID }}
          ORG_GRADLE_PROJECT_signingKey: ${{ secrets.PGP_SECRET_KEY }}
          ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.PGP_PASSPHRASE }}
          ORG_GRADLE_PROJECT_ossrhUsername: ${{ secrets.NEXUS_USERNAME }}
          ORG_GRADLE_PROJECT_ossrhPassword: ${{ secrets.NEXUS_PASSWORD }}
        run: |
          cd bdk-jvm
          ./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository
