name: Publish avm snapshot

on:
  workflow_call:
    inputs:
      ref:
        description: "GitHub ref to checkout to"
        type: string
        default: "master"
    outputs:
      version:
        description: "@fluencelabs/avm version"
        value: ${{ jobs.snapshot.outputs.version }}

env:
  FORCE_COLOR: true

jobs:
  snapshot:
    name: "Publish avm"
    runs-on: ubuntu-latest
    timeout-minutes: 60

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

    permissions:
      contents: read
      id-token: write

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

      - name: Download air-interpreter-wasm binary
        uses: actions/download-artifact@v3
        with:
          name: air-interpreter-wasm
          path: avm/client/dist/

      - run: mv air_interpreter_server.wasm avm.wasm
        working-directory: avm/client/dist

      - name: Import secrets
        uses: hashicorp/vault-action@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 node with self-hosted npm registry
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          registry-url: "https://npm.fluence.dev"
          cache-dependency-path: avm/client/package-lock.json
          cache: "npm"

      - run: npm i
        working-directory: avm/client

      - run: npm run build
        working-directory: avm/client

      - 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: avm/client
          id: ${{ steps.version.outputs.id }}
