name: "hack/make"

on:
  workflow_call:
    inputs:
      mage-targets:
        description: "The mage target(s) to execute"
        type: string
        required: true
      dev-engine:
        description: "Whether to run against a dev Engine"
        type: boolean
        default: false
        required: false
      size:
        type: string
        default: "dagger-runner-2c-8g"
        required: false

jobs:
  # Use a free GitHub Actions runner when NOT running in the dagger/dagger repo
  github-free-runner:
    if: ${{ github.repository != 'dagger/dagger' }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v4
        with:
          go-version: "1.21"
          cache-dependency-path: "internal/mage/go.sum"
      - name: Waiting for Dagger Engine to be ready...
        run: |
          if [ "${{ inputs.dev-engine }}" == "true" ]
          then
            ./hack/dev

            export _EXPERIMENTAL_DAGGER_CLI_BIN="$PWD/bin/dagger"
            echo "_EXPERIMENTAL_DAGGER_CLI_BIN=${_EXPERIMENTAL_DAGGER_CLI_BIN}" >> "$GITHUB_ENV"
            export _EXPERIMENTAL_DAGGER_RUNNER_HOST=docker-container://dagger-engine.dev
            echo "_EXPERIMENTAL_DAGGER_RUNNER_HOST=${_EXPERIMENTAL_DAGGER_RUNNER_HOST}" >> "$GITHUB_ENV"

            chmod +x $_EXPERIMENTAL_DAGGER_CLI_BIN
          fi
          ./hack/make engine:connect
      - name: ${{ inputs.mage-targets }}
        run: |
          ./hack/make ${{ inputs.mage-targets }}
      - name: "ALWAYS print kernel logs - especialy useful on failure"
        if: always()
        run: sudo dmesg

  # Use our own Dagger runner when running in the dagger/dagger repo (including PRs)
  dagger-runner:
    if: ${{ github.repository == 'dagger/dagger' }}
    runs-on: ${{ inputs.size }}
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v4
        with:
          go-version: "1.21"
          cache-dependency-path: "internal/mage/go.sum"
      - name: Waiting for Dagger Engine to be ready...
        run: |
          if [ "${{ inputs.dev-engine }}" == "true" ]
          then
            ./hack/dev

            export _EXPERIMENTAL_DAGGER_CLI_BIN="$PWD/bin/dagger"
            chmod +x $_EXPERIMENTAL_DAGGER_CLI_BIN
            echo "_EXPERIMENTAL_DAGGER_CLI_BIN=${_EXPERIMENTAL_DAGGER_CLI_BIN}" >> "$GITHUB_ENV"

            export _EXPERIMENTAL_DAGGER_RUNNER_HOST=docker-container://dagger-engine.dev
          fi

          echo "_EXPERIMENTAL_DAGGER_RUNNER_HOST=${_EXPERIMENTAL_DAGGER_RUNNER_HOST}" >> "$GITHUB_ENV"

          ./hack/make engine:connect
        env:
          _EXPERIMENTAL_DAGGER_RUNNER_HOST: "unix:///var/run/buildkit/buildkitd.sock"
          _EXPERIMENTAL_DAGGER_CLOUD_TOKEN: "p.eyJ1IjogIjFiZjEwMmRjLWYyZmQtNDVhNi1iNzM1LTgxNzI1NGFkZDU2ZiIsICJpZCI6ICIwYzhmMGY2Yy00YjY1LTRhODktYTI0YS0yN2NjNWNhNzNmNTcifQ.Em92UDP-KmcNd80Y-euTS2IrRkIE8qTisP3SXYkQi3c"
      - name: ${{ inputs.mage-targets }}
        run: |
          ./hack/make ${{ inputs.mage-targets }}
        env:
          _EXPERIMENTAL_DAGGER_CLOUD_TOKEN: "p.eyJ1IjogIjFiZjEwMmRjLWYyZmQtNDVhNi1iNzM1LTgxNzI1NGFkZDU2ZiIsICJpZCI6ICIwYzhmMGY2Yy00YjY1LTRhODktYTI0YS0yN2NjNWNhNzNmNTcifQ.Em92UDP-KmcNd80Y-euTS2IrRkIE8qTisP3SXYkQi3c"
      - name: "ALWAYS print kernel logs - especialy useful on failure"
        if: always()
        run: sudo dmesg
