name: Tests / E2E
on:
  workflow_dispatch:
  pull_request:
    paths:
      - '**/*.go'
      - '.github/workflows/e2e.yaml'
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

env:
  DOCKER_IMAGE_NAME: ghcr.io/cosmos/ibc-go-simd

jobs:
  determine-image-tag:
    runs-on: depot-ubuntu-22.04-4
    outputs:
      simd-tag: ${{ steps.get-tag.outputs.simd-tag }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: '1.23'
      - id: get-tag
        run: |
          if [ -z "${{ github.event.pull_request.number }}" ]
          then
            echo "simd-tag=main" >> $GITHUB_OUTPUT
          else
            tag="pr-${{ github.event.pull_request.number }}"
            echo "Using tag $tag"
            echo "simd-tag=$tag" >> $GITHUB_OUTPUT
          fi

  docker-build:
    runs-on: depot-ubuntu-22.04-4
    permissions:
      packages: write
      contents: read
    steps:
      - uses: actions/checkout@v4
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.DOCKER_IMAGE_NAME }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          build-args: |
            IBC_GO_VERSION=${{ github.ref_name }}

  build-test-matrix:
    runs-on: depot-ubuntu-22.04-4
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v4
        with:
          repository: cosmos/ibc-go
      - uses: actions/setup-go@v5
        with:
          go-version: '1.23'
          cache-dependency-path: 'go.sum'
      - id: set-matrix
        run: |
          output=$(go run cmd/build_test_matrix/main.go)
          echo "matrix=$output" >> $GITHUB_OUTPUT
        env:
          TEST_EXCLUSIONS: 'TestUpgradeTestSuite'

  e2e-tests:
    runs-on: depot-ubuntu-22.04-4
    needs:
      - determine-image-tag
      - build-test-matrix
      - docker-build
    strategy:
      fail-fast: false
      matrix: ${{ fromJSON(needs.build-test-matrix.outputs.matrix) }}
    steps:
      - uses: actions/checkout@v4
        with:
          repository: cosmos/ibc-go
      - uses: actions/setup-go@v5
        with:
          go-version: '1.23'
          cache-dependency-path: 'e2e/go.sum'
      - name: Run e2e Test
        id: e2e_test
        env:
          CHAIN_IMAGE: '${{ env.DOCKER_IMAGE_NAME }}'
          CHAIN_A_TAG: '${{ needs.determine-image-tag.outputs.simd-tag }}'
          CHAIN_B_TAG: '${{ needs.determine-image-tag.outputs.simd-tag }}'
          E2E_CONFIG_PATH: 'ci-e2e-config.yaml'
        run: |
          cd e2e
          make e2e-test test=${{ matrix.test }}
      - name: Upload Diagnostics
        uses: actions/upload-artifact@v4
        if: ${{ failure() }}
        continue-on-error: true
        with:
          name: '${{ matrix.entrypoint }}-${{ matrix.test }}'
          path: e2e/diagnostics
          retention-days: 5
