name: Publish crates snapshots

on:
  workflow_call:
    inputs:
      ref:
        description: "git ref to checkout to"
        type: string
        default: "master"
    outputs:
      version:
        description: "crate snapshots version"
        value: ${{ jobs.publish.outputs.version }}

jobs:
  publish:
    name: "Publish crates"
    runs-on: builder
    timeout-minutes: 60

    env:
      RUSTFLAGS: "-D warnings"
      RUSTC_WRAPPER: "sccache"

    outputs:
      version: "${{ steps.snapshot.outputs.version }}"

    permissions:
      contents: read
      id-token: write

    steps:
      - name: Checkout Marine
        uses: actions/checkout@v4
        with:
          repository: fluencelabs/marine
          ref: ${{ inputs.ref }}

      - name: Import secrets
        uses: hashicorp/vault-action@cb841f2c86fb6d07cff94fda240828c1abc5ba43 # v2.7.3
        with:
          url: https://vault.fluence.dev
          path: jwt/github
          role: ci
          method: jwt
          jwtGithubAudience: "https://github.com/fluencelabs"
          jwtTtl: 300
          exportToken: false
          secrets: |
            kv/cargo-registry/users/ci token | CARGO_REGISTRIES_FLUENCE_TOKEN

      - name: Setup Rust toolchain
        uses: dsherret/rust-toolchain-file@v1

      - name: Setup cache
        uses: Swatinem/rust-cache@v2
        with:
          cache-directories: ~/.cache/sccache
          shared-key: marine
          save-if: false

      - name: Setup sccache
        uses: mozilla-actions/sccache-action@v0.0.3

      - name: Generate snapshot version
        id: version
        uses: fluencelabs/github-actions/generate-snapshot-id@main

      - name: Publish crate snapshots
        id: snapshot
        uses: fluencelabs/github-actions/cargo-publish-snapshot@main
        with:
          id: ${{ steps.version.outputs.id }}

      - name: Build marine
        run: cargo build -p marine --release

      - name: Upload binary to checks
        uses: actions/upload-artifact@v3
        with:
          name: marine
          path: target/release/marine
