name: release-5-verify-cargo-binstall

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-cargo-binstall:
    name: verify-cargo-binstall-${{ matrix.runner }}
    runs-on: ${{ matrix.runner }}
    if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - runner: ubuntu-22.04
          - runner: macos-14
    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: Download cargo-binstall
        run: |
          curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash

      - name: Run cargo binstall
        run: |
          cargo binstall \
            --github-token ${{ secrets.GITHUB_TOKEN }} \
            --strategies "crate-meta-data,quick-install" \
            -y \
            obelisk@$VERSION
        env:
          VERSION: ${{ steps.git-info.outputs.version }}

      - name: Verify Obelisk
        run: |
          mkdir tmp # Avoid conflict with existing obelisk.toml
          cd tmp
          obelisk --version
          obelisk server generate-config
          obelisk server verify

  cargo-binstall-alpine:
    runs-on: ubuntu-24.04
    if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
    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

      - run: |
          docker build --progress=plain --build-arg VERSION=$VERSION - <<EOF
            FROM alpine:latest
            ARG VERSION
            WORKDIR /root
            RUN wget https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-x86_64-unknown-linux-musl.tgz \
            && tar xvfz cargo-binstall-x86_64-unknown-linux-musl.tgz
            RUN ./cargo-binstall \
              --strategies "crate-meta-data,quick-install" \
              --install-path . \
              -y \
              obelisk@$VERSION
            RUN ./obelisk --version
            RUN ./obelisk server generate-config
            RUN ./obelisk server verify
          EOF
        env:
          TAG: ${{ steps.git-info.outputs.tag }}
          VERSION: ${{ steps.git-info.outputs.version }}
