parameters:
  # This template adds arcade-powered source-build to CI. The template produces a server job with a
  # default ID 'Source_Build_Complete' to put in a dependency list if necessary.

  # Specifies the prefix for source-build jobs added to pipeline. Use this if disambiguation needed.
  jobNamePrefix: 'Source_Build'

  # Defines the platform on which to run the job. By default, a linux-x64 machine, suitable for
  # managed-only repositories. This is an object with these properties:
  #
  # name: ''
  #   The name of the job. This is included in the job ID.
  # targetRID: ''
  #   The name of the target RID to use, instead of the one auto-detected by Arcade.
  # nonPortable: false
  #   Enables non-portable mode. This means a more specific RID (e.g. fedora.32-x64 rather than
  #   linux-x64), and compiling against distro-provided packages rather than portable ones.
  # skipPublishValidation: false
  #   Disables publishing validation.  By default, a check is performed to ensure no packages are
  #   published by source-build.
  # container: ''
  #   A container to use. Runs in docker.
  # pool: {}
  #   A pool to use. Runs directly on an agent.
  # buildScript: ''
  #   Specifies the build script to invoke to perform the build in the repo. The default
  #   './build.sh' should work for typical Arcade repositories, but this is customizable for
  #   difficult situations.
  # jobProperties: {}
  #   A list of job properties to inject at the top level, for potential extensibility beyond
  #   container and pool.
  platform: {}

jobs:
- job: ${{ parameters.jobNamePrefix }}_${{ parameters.platform.name }}
  displayName: Source-Build (${{ parameters.platform.name }})

  ${{ each property in parameters.platform.jobProperties }}:
    ${{ property.key }}: ${{ property.value }}

  ${{ if ne(parameters.platform.container, '') }}:
    container: ${{ parameters.platform.container }}

  ${{ if eq(parameters.platform.pool, '') }}:
    # The default VM host AzDO pool. This should be capable of running Docker containers: almost all
    # source-build builds run in Docker, including the default managed platform.
    # /eng/common/templates/variables/pool-providers.yml can't be used here (some customers declare variables already), so duplicate its logic
    pool:
      ${{ if eq(variables['System.TeamProject'], 'public') }}:
        name: $[replace(replace(eq(contains(coalesce(variables['System.PullRequest.TargetBranch'], variables['Build.SourceBranch'], 'refs/heads/main'), 'release'), 'true'), True, 'NetCore-Svc-Public' ), False, 'NetCore-Public')]
        demands: ImageOverride -equals Build.Ubuntu.2204.Amd64.Open

      ${{ if eq(variables['System.TeamProject'], 'internal') }}:
        name: $[replace(replace(eq(contains(coalesce(variables['System.PullRequest.TargetBranch'], variables['Build.SourceBranch'], 'refs/heads/main'), 'release'), 'true'), True, 'NetCore1ESPool-Svc-Internal'), False, 'NetCore1ESPool-Internal')]
        demands: ImageOverride -equals Build.Ubuntu.2204.Amd64

  ${{ if ne(parameters.platform.pool, '') }}:
    pool: ${{ parameters.platform.pool }}

  workspace:
    clean: all

  steps:
  - template: /eng/common/templates/steps/source-build.yml
    parameters:
      platform: ${{ parameters.platform }}
