name: Publish ARC Image

# Revert to https://github.com/actions-runner-controller/releases#releases
# for details on why we use this approach
on:
  release:
    types:
      - published
  workflow_dispatch:
    inputs:
      release_tag_name:
        description: "Tag name of the release to publish"
        required: true
      push_to_registries:
        description: "Push images to registries"
        required: true
        type: boolean
        default: false

permissions:
  contents: write
  packages: write

env:
  TARGET_ORG: actions-runner-controller
  TARGET_REPO: actions-runner-controller

concurrency:
  group: ${{ github.workflow }}
  cancel-in-progress: true

jobs:
  release-controller:
    name: Release
    runs-on: ubuntu-latest
    # gha-runner-scale-set has its own release workflow.
    # We don't want to publish a new actions-runner-controller image
    # we release gha-runner-scale-set.
    if: ${{ !startsWith(github.event.inputs.release_tag_name, 'gha-runner-scale-set-') }}
    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - uses: actions/setup-go@v6
        with:
          go-version-file: "go.mod"

      - name: Install tools
        run: |
          curl -L -O https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.2.0/kubebuilder_2.2.0_linux_amd64.tar.gz
          tar zxvf kubebuilder_2.2.0_linux_amd64.tar.gz
          sudo mv kubebuilder_2.2.0_linux_amd64 /usr/local/kubebuilder
          curl -s https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh | bash
          sudo mv kustomize /usr/local/bin
          curl -L -O https://github.com/tcnksm/ghr/releases/download/v0.13.0/ghr_v0.13.0_linux_amd64.tar.gz
          tar zxvf ghr_v0.13.0_linux_amd64.tar.gz
          sudo mv ghr_v0.13.0_linux_amd64/ghr /usr/local/bin

      - name: Set version env variable
        run: |
          # Define the release tag name based on the event type
          if [[ "${{ github.event_name }}" == "release" ]]; then
            echo "VERSION=$(cat ${GITHUB_EVENT_PATH} | jq -r '.release.tag_name')" >> $GITHUB_ENV
          elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
            echo "VERSION=${{ inputs.release_tag_name }}" >> $GITHUB_ENV
          fi

      - name: Upload artifacts
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          make github-release

      - name: Get Token
        id: get_workflow_token
        uses: peter-murray/workflow-application-token-action@d17e3a9a36850ea89f35db16c1067dd2b68ee343
        with:
          application_id: ${{ secrets.ACTIONS_ACCESS_APP_ID }}
          application_private_key: ${{ secrets.ACTIONS_ACCESS_PK }}
          organization: ${{ env.TARGET_ORG }}

      - name: Resolve push to registries
        run: |
          # Define the push to registries based on the event type
          if [[ "${{ github.event_name }}" == "release" ]]; then
            echo "PUSH_TO_REGISTRIES=true" >> $GITHUB_ENV
          elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
            echo "PUSH_TO_REGISTRIES=${{ inputs.push_to_registries }}" >> $GITHUB_ENV
          fi

      - name: Trigger Build And Push Images To Registries
        run: |
          # Authenticate
          gh auth login --with-token <<< ${{ steps.get_workflow_token.outputs.token }}

          # Trigger the workflow run
          jq -n '{"event_type": "arc", "client_payload": {"release_tag_name": "${{ env.VERSION }}", "push_to_registries": "${{ env.PUSH_TO_REGISTRIES }}" }}' \
            | gh api -X POST /repos/actions-runner-controller/releases/dispatches --input -

      - name: Job summary
        run: |
          echo "The [publish-arc](https://github.com/actions-runner-controller/releases/blob/main/.github/workflows/publish-arc.yaml) workflow has been triggered!" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Parameters:**" >> $GITHUB_STEP_SUMMARY
          echo "- Release tag: ${{ env.VERSION }}" >> $GITHUB_STEP_SUMMARY
          echo "- Push to registries: ${{ env.PUSH_TO_REGISTRIES }}" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Status:**" >> $GITHUB_STEP_SUMMARY
          echo "[https://github.com/actions-runner-controller/releases/actions/workflows/publish-arc.yaml](https://github.com/actions-runner-controller/releases/actions/workflows/publish-arc.yaml)" >> $GITHUB_STEP_SUMMARY
