resources:
  repositories:
    - repository: 1ESPipelineTemplates
      type: git
      name: 1ESPipelineTemplates/1ESPipelineTemplates
      ref: refs/tags/release

parameters:
  - name: ServiceDirectory
    type: string
    default: not-specified
  - name: CtestRegex
    type: string
    default: .*
  - name: CtestExcludeRegex
    type: string
    default: ''
  - name: CoverageEnabled
    type: boolean
    default: true
  - name: LiveTestCtestRegex
    type: string
    default: ''
  - name: Artifacts
    type: object
    default: []
  - name: ArtifactsSource
    type: object
    default: []
  - name: CoverageReportPath
    type: string
    default: 'sdk/*/*/*cov_xml.xml'
  - name: LiveTestTimeoutInMinutes
    type: number
    default: 120
  - name: LineCoverageTarget
    type: number
    default: 95
  - name: BranchCoverageTarget
    type: number
    default: 70
  - name: TestEnv
    type: object
    default: []
  - name: CMakeTestOptions
    type: object
    default: []
  - name: CMakeSourceTestOptions
    type: object
    default: []
  - name: Location
    type: string
    default: ''
  - name: CloudConfig
    type: object
    default:
      Public:
        SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources)
      Preview:
        SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources-preview)
      Canary:
        SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources)
        Location: 'eastus2euap'
      UsGov:
        SubscriptionConfiguration: $(sub-config-gov-test-resources)
      China:
        SubscriptionConfiguration: $(sub-config-cn-test-resources)
  - name: Clouds
    type: string
    default: Public
  - name: SupportedClouds
    type: string
    default: Public
  - name: UnsupportedClouds
    type: string
    default: ''
  - name: PreTestSteps
    type: stepList
    default: []
  - name: PostTestSteps
    type: stepList
    default: []
  - name: CMakeGenerationTimeoutInMinutes
    type: number
    default: 120

extends:
  ${{ if eq(variables['System.TeamProject'], 'internal') }}:
    template: v1/1ES.Official.PipelineTemplate.yml@1ESPipelineTemplates
  ${{ else }}:
    template: v1/1ES.Unofficial.PipelineTemplate.yml@1ESPipelineTemplates
  parameters:
    settings:
      skipBuildTagsForGitHubPullRequests: true
    sdl:
      sourceAnalysisPool:
        name: azsdk-pool-mms-win-2022-general
        image: azsdk-pool-mms-win-2022-1espt
        os: windows
      eslint:
        enabled: false
        justificationForDisabling: 'ESLint injected task has failures because it uses an old version of mkdirp. We should not fail for tools not controlled by the repo. See: https://dev.azure.com/azure-sdk/internal/_build/results?buildId=3499746'
      psscriptanalyzer:
        compiled: true
        break: true
      policy: M365
      credscan:
        suppressionsFile: $(Build.SourcesDirectory)/eng/CredScanSuppression.json
        toolVersion: 2.3.12.23
      binskim:
        enabled: true
        scanOutputDirectoryOnly: true

    stages:
      - stage: CMakeGeneration
        variables:
          - template: /eng/pipelines/templates/variables/image.yml@self
        jobs:
          - template: /eng/pipelines/templates/jobs/cmake-generate-jobs.yml@self
            parameters:
              TimeoutInMinutes: ${{ parameters.CMakeGenerationTimeoutInMinutes }}
              ServiceDirectory: ${{ parameters.ServiceDirectory }}
              Artifacts: ${{ parameters.Artifacts }}
              ArtifactsSource: ${{ parameters.ArtifactsSource }}
              CMakeTestOptions: ${{ parameters.CMakeTestOptions }}
              CMakeSourceTestOptions: ${{ parameters.CMakeSourceTestOptions }}

      # PrePublishBuild runs a narrower set of fast builds. If these builds pass,
      # Integration can launch immediately without awaiting more build and test jobs
      - stage: PrePublishBuild
        variables:
          - template: /eng/pipelines/templates/variables/image.yml@self
        dependsOn: []
        jobs:
          - template: /eng/pipelines/templates/jobs/archetype-sdk-client.yml@self
            parameters:
              MatrixFile: eng/pipelines/templates/stages/platform-matrix-quick.json
              JobName: QuickValidate
              ServiceDirectory: ${{ parameters.ServiceDirectory }}
              Artifacts: ${{ parameters.Artifacts }}
              CtestRegex: ${{ parameters.CtestRegex }}
              CtestExcludeRegex: ${{ parameters.CtestExcludeRegex }}
              CoverageEnabled: ${{ parameters.CoverageEnabled }}
              CoverageReportPath: ${{ parameters.CoverageReportPath }}
              LineCoverageTarget: ${{ parameters.LineCoverageTarget }}
              BranchCoverageTarget: ${{ parameters.BranchCoverageTarget }}
              ${{ if eq(parameters.ServiceDirectory, 'template') }}:
                TestPipeline: true
              TestEnv: ${{ parameters.TestEnv }}
              PreTestSteps: ${{ parameters.PreTestSteps }}
              PostTestSteps: ${{ parameters.PostTestSteps }}
              RunMetaJobs: true

      - stage: Build
        variables:
          - template: /eng/pipelines/templates/variables/image.yml@self
        dependsOn: []
        jobs:
          - template: /eng/pipelines/templates/jobs/archetype-sdk-client.yml@self
            # Code coverage is enabled by default for live tests
            parameters:
              MatrixFile: eng/pipelines/templates/stages/platform-matrix.json
              JobName: Validate
              ServiceDirectory: ${{ parameters.ServiceDirectory }}
              Artifacts: ${{ parameters.Artifacts }}
              CtestRegex: ${{ parameters.CtestRegex }}
              CtestExcludeRegex: ${{ parameters.CtestExcludeRegex }}
              CoverageEnabled: ${{ parameters.CoverageEnabled }}
              CoverageReportPath: ${{ parameters.CoverageReportPath }}
              LineCoverageTarget: ${{ parameters.LineCoverageTarget }}
              BranchCoverageTarget: ${{ parameters.BranchCoverageTarget }}
              ${{ if eq(parameters.ServiceDirectory, 'template') }}:
                TestPipeline: true
              TestEnv: ${{ parameters.TestEnv }}
              PreTestSteps: ${{ parameters.PreTestSteps }}
              PostTestSteps: ${{ parameters.PostTestSteps }}
              RunMetaJobs: false

      - ${{ if and(eq(variables['System.TeamProject'], 'internal'), ne(parameters.LiveTestCtestRegex, '')) }}:
          - template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml@self
            parameters:
              TimeoutInMinutes: ${{ parameters.LiveTestTimeoutInMinutes }}
              ServiceDirectory: ${{ parameters.ServiceDirectory }}
              CtestRegex: ${{ parameters.LiveTestCtestRegex }}
              CoverageReportPath: ${{ parameters.CoverageReportPath }}
              Location: ${{ parameters.Location }}
              CloudConfig: ${{ parameters.CloudConfig }}
              Clouds: ${{ parameters.Clouds }}
              SupportedClouds: ${{ parameters.SupportedClouds }}
              UnsupportedClouds: ${{ parameters.UnsupportedClouds }}
              PreTestSteps: ${{ parameters.PreTestSteps }}
              PostTestSteps: ${{ parameters.PostTestSteps }}

      - ${{ if and(eq(variables['System.TeamProject'], 'internal'), not(endsWith(variables['Build.DefinitionName'], ' - tests'))) }}:
          - template: archetype-cpp-release.yml@self
            parameters:
              ServiceDirectory: ${{ parameters.ServiceDirectory }}
              IntegrationDependsOn:
                - PrePublishBuild
              DependsOn:
                - PrePublishBuild
                - Build
                # Only depend on `LiveTest` if there are live tests to execute
                - ${{ if and(eq(variables['System.TeamProject'], 'internal'), ne(parameters.LiveTestCtestRegex, '')) }}:
                    # Pulled from stages/archetype-sdk-tests.yml to generate 'LiveTest_<cloud>'
                    - ${{ each cloud in parameters.CloudConfig }}:
                        - ${{ if or(contains(parameters.Clouds, cloud.key), and(contains(variables['Build.DefinitionName'], 'tests-weekly'), contains(parameters.SupportedClouds, cloud.key))) }}:
                            - ${{ if not(contains(parameters.UnsupportedClouds, cloud.key)) }}:
                                - LiveTest_${{ cloud.key }}
              Artifacts: ${{ parameters.Artifacts }}
              ArtifactName: packages
              ${{ if eq(parameters.ServiceDirectory, 'template') }}:
                TestPipeline: true

