name: release-5-verify-download-sh

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-using-vm:
    name: verify-using-vm-${{ 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: ubuntu-22.04-arm
          - 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

      - run: |
          mkdir temp
          cd temp
          curl -L --tlsv1.2 -sSf https://raw.githubusercontent.com/obeli-sk/obelisk/main/download.sh | bash
          ./obelisk --version | grep $VERSION
          ./obelisk server generate-config
          ./obelisk server verify
        env:
          VERSION: ${{ steps.git-info.outputs.version }}

  verify-using-docker:
    name: verify-using-docker-${{ 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: ubuntu-22.04-arm
    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 apk add curl
            RUN curl -L --tlsv1.2 -sSf https://raw.githubusercontent.com/obeli-sk/obelisk/main/download.sh | sh
            RUN ./obelisk --version | grep $VERSION
            RUN ./obelisk server generate-config
            RUN ./obelisk server verify
          EOF
        env:
          VERSION: ${{ steps.git-info.outputs.version }}
