name: "Setup ARC E2E Test Action"
description: "Build controller image, create kind cluster, load the image, and exchange ARC configure token."

inputs:
  app-id:
    description: "GitHub App Id for exchange access token"
    required: true
  app-pk:
    description: "GitHub App private key for exchange access token"
    required: true
  image-name:
    description: "Local docker image name for building"
    required: true
  image-tag:
    description: "Tag of ARC Docker image for building"
    required: true
  target-org:
    description: "The test organization for ARC e2e test"
    required: true

outputs:
  token:
    description: "Token to use for configure ARC"
    value: ${{steps.config-token.outputs.token}}

runs:
  using: "composite"
  steps:
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2
      with:
        # Pinning v0.9.1 for Buildx and BuildKit v0.10.6
        # BuildKit v0.11 which has a bug causing intermittent
        # failures pushing images to GHCR
        version: v0.9.1
        driver-opts: image=moby/buildkit:v0.10.6

    - name: Build controller image
      # https://github.com/docker/build-push-action/releases/tag/v6.15.0
      uses: docker/build-push-action@471d1dc4e07e5cdedd4c2171150001c434f0b7a4
      with:
        file: Dockerfile
        platforms: linux/amd64
        load: true
        build-args: |
          DOCKER_IMAGE_NAME=${{inputs.image-name}}
          VERSION=${{inputs.image-tag}}
        tags: |
          ${{inputs.image-name}}:${{inputs.image-tag}}
        no-cache: true

    - name: Create minikube cluster and load image
      shell: bash
      run: |
        minikube start
        minikube image load ${{inputs.image-name}}:${{inputs.image-tag}}

    - name: Get configure token
      id: config-token
      # https://github.com/peter-murray/workflow-application-token-action/releases/tag/v3.0.0
      uses: peter-murray/workflow-application-token-action@dc0413987a085fa17d19df9e47d4677cf81ffef3
      with:
        application_id: ${{ inputs.app-id }}
        application_private_key: ${{ inputs.app-pk }}
        organization: ${{ inputs.target-org}}
