name: Peer Discovery AWS Integration Test
on:
  push:
    paths:
      - ".github/workflows/peer-discovery-aws.yaml"
      - "deps/rabbitmq_peer_discovery_aws/**"
      - "deps/rabbitmq_peer_discovery_common/**"
      - "deps/rabbit/src/rabbit_peer_discovery.erl"
  schedule:
    - cron: "4 0 * * MON"
  workflow_dispatch:
concurrency:
  group: ${{ github.workflow }}-${{ github.ref_name }}
  cancel-in-progress: true
env:
  OTP_VERSION: "27"
jobs:
  peer-discovery-aws-integration-test:
    name: Integration Test
    runs-on: ubuntu-22.04
    timeout-minutes: 45
    steps:
    - name: CHECK IF IMAGE WILL PUSH
      id: authorized
      run: |
        if [ -n "${{ secrets.DOCKERHUB_PASSWORD }}" ]; then
          echo "authorized=true" | tee -a $GITHUB_OUTPUT
        else
          echo "authorized=false" | tee -a $GITHUB_OUTPUT
        fi
    - name: CHECKOUT REPOSITORY
      if: steps.authorized.outputs.authorized == 'true'
      uses: actions/checkout@v4
    - uses: docker/metadata-action@v5
      if: steps.authorized.outputs.authorized == 'true'
      id: metadata
      with:
        images: pivotalrabbitmq/rabbitmq
        flavor: |
          suffix=-otp${{ env.OTP_VERSION }}
        tags: |
          type=sha,format=long
    - uses: int128/wait-for-docker-image-action@v1
      if: steps.authorized.outputs.authorized == 'true'
      with:
        tags: ${{ steps.metadata.outputs.tags }}
        timeout-seconds: 3600
        polling-seconds: 60
    - name: CONFIGURE OTP & ELIXIR
      if: steps.authorized.outputs.authorized == 'true'
      uses: erlef/setup-beam@v1.17
      with:
        otp-version: ${{ env.OTP_VERSION }}
        elixir-version: "1.17"
    - name: SETUP ecs-cli
      if: steps.authorized.outputs.authorized == 'true'
      env:
        ECS_CLI_VERSION: 1.21.0
      run: |
        curl -Lo /usr/local/bin/ecs-cli https://amazon-ecs-cli.s3.amazonaws.com/ecs-cli-linux-amd64-v${ECS_CLI_VERSION} && \
        chmod +x /usr/local/bin/ecs-cli && \
        ecs-cli --version
    - name: RUN INTEGRATION TESTS
      if: steps.authorized.outputs.authorized == 'true'
      env:
        AWS_ACCESS_KEY_ID: "${{ secrets.CONCOURSE_AWS_ACCESS_KEY_ID }}"
        AWS_SECRET_ACCESS_KEY: "${{ secrets.CONCOURSE_AWS_SECRET_ACCESS_KEY }}"
        RABBITMQ_IMAGE: "${{ steps.metadata.outputs.tags }}"
      run: |
        branch_or_tag="${GITHUB_REF##*/}"
        export AWS_ECS_CLUSTER_NAME="rabbitmq-peer-discovery-aws-actions-${branch_or_tag//[._]/-}"
        make -C deps/rabbitmq_peer_discovery_aws ct-integration
    - name: UPLOAD TEST LOGS
      if: always()
      uses: actions/upload-artifact@v4
      with:
        name: CT logs
        path: "logs/"
        if-no-files-found: ignore
