name: Preflight Tests

on:
  workflow_dispatch:
    inputs:
      reason:
        description: Brief reason for running this workflow manually
        required: false
        default: User initiated run
        type: string
  workflow_call:

jobs:
  preflight-tests:
    if: ${{ github.repository == 'superfly/flyctl' }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        vm_size: [""]
        parallelism: [20]
        index: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version-file: "go.mod"
          check-latest: true
      - name: Get go version
        id: go-version
        run: echo "name=version::$(go env GOVERSION)" >> $GITHUB_OUTPUT
      - name: Install gotesplit, set FLY_PREFLIGHT_TEST_APP_PREFIX
        run: |
          curl -sfL https://raw.githubusercontent.com/Songmu/gotesplit/v0.2.1/install.sh | sh -s
          echo "FLY_PREFLIGHT_TEST_APP_PREFIX=pf-gha-$(openssl rand -hex 4)" >> "$GITHUB_ENV"
      - uses: actions/download-artifact@v4
        with:
          name: flyctl
          path: master-build
      - name: Run preflight tests
        id: preflight
        env:
          FLY_PREFLIGHT_TEST_ACCESS_TOKEN: ${{ secrets.FLYCTL_PREFLIGHT_CI_FLY_API_TOKEN }}
          FLY_PREFLIGHT_TEST_FLY_ORG: flyctl-ci-preflight
          # This VM size is only available in ORD.
          FLY_PREFLIGHT_TEST_FLY_REGIONS: ord
          FLY_PREFLIGHT_TEST_NO_PRINT_HISTORY_ON_FAIL: "true"
          FLY_FORCE_TRACE: "true"
          FLY_PREFLIGHT_TEST_VM_SIZE: ${{ matrix.vm_size }}
        run: |
          mv master-build/flyctl bin/flyctl
          chmod +x bin/flyctl
          export PATH=$PWD/bin:$PATH
          echo -n failed= >> $GITHUB_OUTPUT
          ./scripts/preflight.sh -r "${{ github.ref }}" -t "${{ matrix.parallelism }}" -i "${{ matrix.index }}" -o $GITHUB_OUTPUT
      - name: Post failure to slack
        if: ${{ github.ref == 'refs/heads/master' && failure() }}
        uses: slackapi/slack-github-action@37ebaef184d7626c5f204ab8d3baff4262dd30f0
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.PREFLIGHT_SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
        with:
          payload: |
            {
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": ":sob: preflight tests failed: ${{ steps.preflight.outputs.failed }} ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
                  }
                }
              ]
            }
      - uses: superfly/flyctl-actions/setup-flyctl@master
        if: always()
      - name: Clean up any un-deleted preflight apps
        if: always()
        env:
          FLY_API_TOKEN: ${{ secrets.FLYCTL_PREFLIGHT_CI_FLY_API_TOKEN }}
          FLY_PREFLIGHT_TEST_FLY_ORG: flyctl-ci-preflight
        run: |
          ./scripts/delete_preflight_apps.sh "$FLY_PREFLIGHT_TEST_APP_PREFIX"
