name: Build Test Image
description: A composite action that allows building and publishing the test remote runner image

inputs:
  repository:
    description: The docker repository for the image
    default: chainlink-ccip-tests
    required: false
  tag:
    description: The tag to use by default and to use for checking image existance
    default: ${{ github.sha }}
    required: false
  other_tags:
    description: Other tags to push if needed
    required: false
  suites:
    description: The test suites to build into the image
    default: chaos migration reorg smoke soak benchmark load ccip-tests/load ccip-tests/smoke ccip-tests/chaos
    required: false
  QA_AWS_ROLE_TO_ASSUME:
    description: The AWS role to assume as the CD user, if any. Used in configuring the docker/login-action
    required: true
  QA_AWS_REGION:
    description: The AWS region the ECR repository is located in, should only be needed for public ECR repositories, used in configuring docker/login-action
    required: true
  QA_AWS_ACCOUNT_NUMBER:
    description: The AWS region the ECR repository is located in, should only be needed for public ECR repositories, used in configuring docker/login-action
    required: true

runs:
  using: composite
  steps:

    # Base Test Image Logic
    - name: Get CTF Version
      id: version
      uses: smartcontractkit/chainlink-github-actions/chainlink-testing-framework/mod-version@fc3e0df622521019f50d772726d6bf8dc919dd38 # v2.3.19
      with:
        go-project-path: ./integration-tests
        module-name: github.com/smartcontractkit/chainlink-testing-framework/lib
        enforce-semantic-tag: false
    - name: Get CTF sha
      if: steps.version.outputs.is_semantic == 'false'
      id: short_sha
      env:
        VERSION: ${{ steps.version.outputs.version }}
      shell: bash
      run: |
        short_sha="${VERSION##*-}"
        echo "short sha is: ${short_sha}"
        echo "short_sha=${short_sha}" >> "$GITHUB_OUTPUT"
    - name: Checkout chainlink-testing-framework
      if: steps.version.outputs.is_semantic == 'false'
      uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
      with:
        repository: smartcontractkit/chainlink-testing-framework
        ref: main
        fetch-depth: 0
        path: ctf
    - name: Get long sha
      if: steps.version.outputs.is_semantic == 'false'
      id: long_sha
      env:
        SHORT_SHA: ${{ steps.short_sha.outputs.short_sha }}
      shell: bash
      run: |
        cd ctf
        long_sha=$(git rev-parse ${SHORT_SHA})
        echo "sha is: ${long_sha}"
        echo "long_sha=${long_sha}" >> "$GITHUB_OUTPUT"
    - name: Check if test base image exists
      if: steps.version.outputs.is_semantic == 'false'
      id: check-base-image
      uses: smartcontractkit/chainlink-github-actions/docker/image-exists@75a9005952a9e905649cfb5a6971fd9429436acd # v2.3.25
      with:
        repository: test-base-image
        tag: ${{ steps.long_sha.outputs.long_sha }}
        AWS_REGION: ${{ inputs.QA_AWS_REGION }}
        AWS_ROLE_TO_ASSUME: ${{ inputs.QA_AWS_ROLE_TO_ASSUME }}
    - name: Build Base Image
      if: steps.version.outputs.is_semantic == 'false' && steps.check-base-image.outputs.exists == 'false'
      uses: smartcontractkit/chainlink-github-actions/docker/build-push@75a9005952a9e905649cfb5a6971fd9429436acd # v2.3.25
      env:
        BASE_IMAGE_NAME: ${{ inputs.QA_AWS_ACCOUNT_NUMBER }}.dkr.ecr.${{ inputs.QA_AWS_REGION }}.amazonaws.com/test-base-image:${{ steps.long_sha.outputs.long_sha }}
      with:
        tags: ${{ env.BASE_IMAGE_NAME }}
        file: ctf/k8s/Dockerfile.base
        AWS_REGION: ${{ inputs.QA_AWS_REGION }}
        AWS_ROLE_TO_ASSUME: ${{ inputs.QA_AWS_ROLE_TO_ASSUME }}
    # End Base Image Logic

    # Test Runner Logic
    - name: Check if image exists
      id: check-image
      uses: smartcontractkit/chainlink-github-actions/docker/image-exists@75a9005952a9e905649cfb5a6971fd9429436acd # v2.3.25
      with:
        repository: ${{ inputs.repository }}
        tag: ${{ inputs.tag }}
        AWS_REGION: ${{ inputs.QA_AWS_REGION }}
        AWS_ROLE_TO_ASSUME: ${{ inputs.QA_AWS_ROLE_TO_ASSUME }}
    - name: Build and Publish Test Runner
      if: steps.check-image.outputs.exists == 'false'
      uses: smartcontractkit/chainlink-github-actions/docker/build-push@75a9005952a9e905649cfb5a6971fd9429436acd # v2.3.25
      with:
        tags: |
          ${{ inputs.QA_AWS_ACCOUNT_NUMBER }}.dkr.ecr.${{ inputs.QA_AWS_REGION }}.amazonaws.com/${{ inputs.repository }}:${{ inputs.tag }}
          ${{ inputs.other_tags }}
        file: ./integration-tests/test.Dockerfile
        build-args: |
          BASE_IMAGE=${{ inputs.QA_AWS_ACCOUNT_NUMBER }}.dkr.ecr.${{ inputs.QA_AWS_REGION }}.amazonaws.com/test-base-image
          IMAGE_VERSION=${{ steps.long_sha.outputs.long_sha || steps.version.outputs.version }}
          SUITES="${{ inputs.suites }}"
        AWS_REGION: ${{ inputs.QA_AWS_REGION }}
        AWS_ROLE_TO_ASSUME: ${{ inputs.QA_AWS_ROLE_TO_ASSUME }}
    - name: Print Image Built
      shell: sh
      env:
        INPUTS_REPOSITORY: ${{ inputs.repository }}
        INPUTS_TAG: ${{ inputs.tag }}
      run: |
        echo "### ${INPUTS_REPOSITORY} image tag for this test run :ship:" >>$GITHUB_STEP_SUMMARY
        echo "\`${INPUTS_TAG}\`" >>$GITHUB_STEP_SUMMARY
    # End Test Runner Logic
