name: Publish marine-js snapshot

on:
  workflow_call:
    inputs:
      ref:
        description: "git ref to checkout to"
        type: string
        default: "master"
    outputs:
      version:
        description: "@fluencelabs/marine-js version"
        value: ${{ jobs.publish.outputs.version }}

env:
  FORCE_COLOR: true

jobs:
  publish:
    name: "Publish marine-js snapshot"
    runs-on: ubuntu-latest
    timeout-minutes: 60

    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/npm-registry/basicauth/ci token | NODE_AUTH_TOKEN

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

      - name: Setup wasm-pack
        uses: jetli/wasm-pack-action@v0.4.0

      - name: Setup node with self-hosted npm registry
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          registry-url: "https://npm.fluence.dev"
          cache-dependency-path: marine-js/npm-package/package-lock.json

      - name: Build with wasm-pack
        run: ./build.sh
        working-directory: marine-js

      - name: Run npm install
        working-directory: marine-js/npm-package
        run: npm i

      - name: Run npm build
        working-directory: marine-js/npm-package
        run: npm run build

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

      - name: Publish snapshot
        id: snapshot
        uses: fluencelabs/github-actions/npm-publish-snapshot@main
        with:
          working-directory: marine-js/npm-package
          id: ${{ steps.version.outputs.id }}
