name: release-5-verify-cargo-install

# Contrary to `check-run-cargo-install`, install latest Rust and run cargo install using sources from crates.io .

on:
  workflow_dispatch:
    inputs:
      ref:
        description: "The ref (branch or SHA) to obtain the tag"
        required: false
        type: string
  workflow_run:
    workflows:
      - release-2-cargo-publish
    types:
      - completed

defaults:
  run:
    shell: bash -xe {0}

jobs:
  verify-assets:
    runs-on: ubuntu-24.04
    if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
    strategy:
      fail-fast: false
    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
        with:
          # Use the ref if provided, otherwise obtain the parent workflow branch, defaults to main.
          ref: ${{ github.event.inputs.ref || github.event.workflow_run.head_branch }}

      - id: git-info
        run: |
          VERSION="$(grep -m1 '^version = "' Cargo.toml | cut -d'"' -f2)"
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "tag=v$VERSION" >> $GITHUB_OUTPUT
          echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT

      - name: Install latest Rust
        run: |
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install protobuf-compiler -y

      - name: Run cargo install
        run: |
          source "$HOME/.cargo/env"
          cargo install obelisk@$VERSION --locked
        env:
          VERSION: ${{ steps.git-info.outputs.version }}

      - name: Verify Obelisk
        run: |
          source "$HOME/.cargo/env"
          mkdir tmp # Avoid conflict with existing obelisk.toml
          cd tmp
          obelisk --version
          obelisk server generate-config
          obelisk server verify
