parameters:
  # Agent OS identifier and used as job name
  agentOs: ''

  # Agent pool
  pool: {}

  # Additional variables
  variables: {}

  # Build strategy - matrix
  strategy: ''

  # Job timeout
  timeoutInMinutes: 180

jobs:
- ${{ if and(ne(variables['System.TeamProject'], 'public'), notin(variables['Build.Reason'], 'PullRequest'), eq(variables['Build.SourceBranch'], 'refs/heads/release/8.0.3xx'), not(contains(parameters.agentOs, 'TemplateEngine'))) }}:
  - template: /eng/common/templates-official/job/onelocbuild.yml@self
    parameters:
      CreatePr: true
      LclSource: lclFilesfromPackage
      LclPackageId: 'LCL-JUNO-PROD-DOTNETSDK'
      MirrorBranch: release/8.0.4xx
      MirrorRepo: sdk

- ${{ if not(contains(parameters.agentOs, 'TemplateEngine')) }}:
  - template: /eng/common/templates-official/job/job.yml@self
    parameters:
      name: ${{ parameters.agentOs }}
      enableMicrobuild: true
      enablePublishBuildAssets: true
      enableTelemetry: true
      enablePublishUsingPipelines: true
      helixRepo: dotnet/sdk
      pool: ${{ parameters.pool }}
      timeoutInMinutes: ${{ parameters.timeoutInMinutes }}
      ${{ if ne(parameters.strategy, '') }}:
        strategy: ${{ parameters.strategy }}
      workspace:
        clean: all
      variables:
        - ${{ each variable in parameters.variables }}:
          - ${{ if ne(variable.name, '') }}:
            - name: ${{ variable.name }}
              value: ${{ variable.value }}
        - name: _AgentOSName
          value: ${{ parameters.agentOs }}
        - name: _TeamName
          value: DotNetCore
        - name: _OfficialBuildIdArgs
          value: ''
        - name: _SignArgs
          value: ''
        - name: _HelixApiToken
          value: ''
        # Helix Testing requires a token when internally run
        - ${{ if ne(variables['System.TeamProject'], 'public') }}:
          - group: DotNet-HelixApi-Access
          - name: _HelixApiToken
            value: $(HelixApiAccessToken)
        - ${{ if and(ne(variables['System.TeamProject'], 'public'), notin(variables['Build.Reason'], 'PullRequest')) }}:
          - group: dotnet-benchview
          - name: _OfficialBuildIdArgs
            value: /p:OfficialBuildId=$(BUILD.BUILDNUMBER)
          - name: _SignArgs
            value: /p:DotNetSignType=$(_SignType) /p:TeamName=$(_TeamName)
          - name: _PerfIterations
            value: 25

      steps:
      - ${{ if ne(variables['System.TeamProject'], 'public') }}:
        - task: PowerShell@2
          displayName: Setup Private Feeds Credentials
          inputs:
            filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.ps1
            arguments: -ConfigFile $(Build.SourcesDirectory)/NuGet.config -Password $Env:Token
          env:
            Token: $(dn-bot-dnceng-artifact-feeds-rw)
      - ${{ if eq(parameters.agentOs, 'Windows_NT') }}:
        - powershell: eng\common\build.ps1
                    $(_CIBuild)
                    -configuration $(_BuildConfig)
                    $(_PublishArgs)
                    $(_SignArgs)
                    $(_OfficialBuildIdArgs)
                    /p:Test=false
                    $(_InternalRuntimeDownloadArgs)
                    $(_OfficialBuildArgs)
          continueOnError: false
          displayName: Build
          env:
            BuildConfig: $(_BuildConfig)
            SYSTEM_ACCESSTOKEN: $(System.AccessToken)
            HelixAccessToken: ${{ parameters.HelixAccessToken }}
        - powershell: eng\common\build.ps1
                -configuration $(_BuildConfig)
                -prepareMachine
                -ci
                -restore
                $(_Test)
                -projects $(Build.SourcesDirectory)\test\UnitTests.proj
                /bl:$(Build.SourcesDirectory)\artifacts\log\$(_BuildConfig)\TestInHelix.binlog
                /p:_CustomHelixTargetQueue=${{ parameters.helixTargetQueue }}
                $(_InternalRuntimeDownloadArgs)
          displayName: Run Tests in Helix
          condition: succeeded()
          env:
              SYSTEM_ACCESSTOKEN: $(System.AccessToken)
              HelixAccessToken: $(_HelixApiToken)
              RunAoTTests: 'false'

      - ${{ if eq(parameters.agentOs, 'Windows_NT_FullFramework') }}:
        - powershell: eng\common\build.ps1
                    $(_CIBuild)
                    -configuration $(_BuildConfig)
                    $(_SignArgs)
                    $(_OfficialBuildIdArgs)
                    $(_InternalRuntimeDownloadArgs)
                    /p:Test=false
          continueOnError: false
          displayName: Build
          env:
            BuildConfig: $(_BuildConfig)
            TestFullMSBuild: 'true'
            SYSTEM_ACCESSTOKEN: $(System.AccessToken)
            HelixAccessToken: ${{ parameters.HelixAccessToken }}
        - powershell: eng\common\build.ps1
                -configuration $(_BuildConfig)
                -prepareMachine
                -ci
                -restore
                -test
                -projects $(Build.SourcesDirectory)\test\UnitTests.proj
                /bl:$(Build.SourcesDirectory)\artifacts\log\$(_BuildConfig)\TestInHelix.binlog
                /p:_CustomHelixTargetQueue=${{ parameters.helixTargetQueue }}
                $(_InternalRuntimeDownloadArgs)
          displayName: Run Tests in Helix
          condition: succeededOrFailed()
          env:
              TestFullMSBuild: 'true'
              SYSTEM_ACCESSTOKEN: $(System.AccessToken)
              HelixAccessToken: $(_HelixApiToken)

      - ${{ if eq(parameters.agentOs, 'Windows_NT_TestAsTools') }}:
        - powershell: eng\common\build.ps1
                    $(_CIBuild)
                    -configuration $(_BuildConfig)
                    $(_SignArgs)
                    $(_OfficialBuildIdArgs)
                    $(_InternalRuntimeDownloadArgs)
                    /p:RunTestsAsTool=true
          displayName: Build
          env:
            BuildConfig: $(_BuildConfig)

      - ${{ if notIn(parameters.agentOs, 'Windows_NT', 'Windows_NT_FullFramework', 'Windows_NT_TestAsTools') }}:
        - script: eng/common/build.sh
                    $(_CIBuild)
                    --configuration $(_BuildConfig)
                    $(_SignArgs)
                    $(_OfficialBuildIdArgs)
                    $(_InternalRuntimeDownloadArgs)
                    -p:Test=false
          continueOnError: false
          displayName: Build
          env:
            BuildConfig: $(_BuildConfig)
        - powershell: eng/common/build.sh
                -configuration $(_BuildConfig)
                -prepareMachine
                -ci
                -restore
                -test
                -projects $(Build.SourcesDirectory)/test/UnitTests.proj
                /bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/TestInHelix.binlog
                /p:_CustomHelixTargetQueue=${{ parameters.helixTargetQueue }}
                $(_InternalRuntimeDownloadArgs)
          displayName: Run Tests in Helix
          condition: succeededOrFailed()
          env:
              SYSTEM_ACCESSTOKEN: $(System.AccessToken)
              HelixAccessToken: $(_HelixApiToken)
              RunAoTTests: 'false'

      - task: CopyFiles@2
        displayName: Gather Logs
        inputs:
          SourceFolder: '$(Build.SourcesDirectory)/artifacts'
          Contents: |
            log/$(_BuildConfig)/**/*
            TestResults/$(_BuildConfig)/**/*
            SymStore/$(_BuildConfig)/**/*
            tmp/$(_BuildConfig)/**/*.binlog
          TargetFolder: '$(Build.ArtifactStagingDirectory)'
        continueOnError: true
        condition: always()

      - task: 1ES.PublishBuildArtifacts@1
        displayName: Publish Logs to VSTS
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)'
          ArtifactName: '$(_AgentOSName)_$(Agent.JobName)_$(Build.BuildNumber)'
          publishLocation: Container
        continueOnError: true
        condition: always()

# AoT Jobs
- ${{ if and(in(parameters.agentOs, 'Windows_NT', 'Darwin'), or(eq(variables['System.TeamProject'], 'public'), in(variables['Build.Reason'], 'PullRequest'))) }}:
  - template: /eng/common/templates-official/job/job.yml@self
    parameters:
      name: ${{ parameters.agentOs }}_AoT_Tests
      enableMicrobuild: true
      enablePublishBuildAssets: true
      enableTelemetry: true
      enablePublishUsingPipelines: true
      helixRepo: dotnet/sdk
      pool: ${{ parameters.pool }}
      timeoutInMinutes: ${{ parameters.timeoutInMinutes }}
      ${{ if ne(parameters.strategy, '') }}:
        strategy: ${{ parameters.strategy }}
      workspace:
        clean: all
      variables:
        - ${{ each variable in parameters.variables }}:
          - ${{ if ne(variable.name, '') }}:
            - name: ${{ variable.name }}
              value: ${{ variable.value }}
        - name: _AgentOSName
          value: ${{ parameters.agentOs }}
        - name: _TeamName
          value: DotNetCore
        - name: _OfficialBuildIdArgs
          value: ''
        - name: _PublishArgs
          value: ''
        - name: _SignArgs
          value: ''
        - name: _HelixApiToken
          value: ''
        # Helix Testing requires a token when internally run
        - ${{ if ne(variables['System.TeamProject'], 'public') }}:
          - group: DotNet-HelixApi-Access
          - name: _HelixApiToken
            value: $(HelixApiAccessToken)
        - ${{ if and(ne(variables['System.TeamProject'], 'public'), notin(variables['Build.Reason'], 'PullRequest')) }}:
          - group: dotnet-benchview
          - name: _OfficialBuildIdArgs
            value: /p:OfficialBuildId=$(BUILD.BUILDNUMBER)
          - name: _SignArgs
            value: /p:DotNetSignType=$(_SignType) /p:TeamName=$(_TeamName)
          - name: _PerfIterations
            value: 25

      steps:
      - ${{ if and(ne(variables['System.TeamProject'], 'public'), in(parameters.agentOs, 'Windows_NT', 'Darwin')) }}:
        - task: PowerShell@2
          displayName: Setup Private Feeds Credentials
          inputs:
            filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.ps1
            arguments: -ConfigFile $(Build.SourcesDirectory)/NuGet.config -Password $Env:Token
          env:
            Token: $(dn-bot-dnceng-artifact-feeds-rw)
      - ${{ if eq(parameters.agentOs, 'Windows_NT') }}:
        - powershell: eng\common\build.ps1
                    $(_CIBuild)
                    -configuration $(_BuildConfig)
                    $(_SignArgs)
                    $(_OfficialBuildIdArgs)
                    /p:Test=false
                    $(_InternalRuntimeDownloadArgs)
          continueOnError: false
          displayName: Build
          env:
            BuildConfig: $(_BuildConfig)
            SYSTEM_ACCESSTOKEN: $(System.AccessToken)
            HelixAccessToken: ${{ parameters.HelixAccessToken }}
        - script: $(Build.SourcesDirectory)/artifacts/bin/redist/$(_BuildConfig)/dotnet/dotnet workload install wasm-tools --skip-manifest-update
          displayName: Install wasm-tools Workload
          continueOnError: false
        - powershell: $(Build.SourcesDirectory)/eng/common/build.ps1
                -ci
                -restore
                -test
                -prepareMachine
                -projects $(Build.SourcesDirectory)/test/UnitTests.proj
                -configuration $(_BuildConfig)
                /bl:$(Build.SourcesDirectory)\artifacts\log\$(_BuildConfig)\TestInHelix.binlog
                /p:_CustomHelixTargetQueue=${{ parameters.helixTargetQueue }}
                $(_InternalRuntimeDownloadArgs)
          displayName: Run AoT Tests in Helix
          env:
              SYSTEM_ACCESSTOKEN: $(System.AccessToken)
              HelixAccessToken: $(_HelixApiToken)
              RunAoTTests: 'true'

      - ${{ if eq(parameters.agentOs, 'Darwin') }}:
        - script: eng/common/build.sh
                    --configuration $(_BuildConfig)
                    --restore
                    --build
                    --pack
                    --ci
                    $(_SignArgs)
                    $(_OfficialBuildIdArgs)
                    $(_InternalRuntimeDownloadArgs)
                    -p:Test=false
          displayName: Build
          env:
            BuildConfig: $(_BuildConfig)
        - script: $(Build.SourcesDirectory)/artifacts/bin/redist/$(_BuildConfig)/dotnet/dotnet workload install wasm-tools --skip-manifest-update
          displayName: Install wasm-tools Workload
          continueOnError: false
        - script: eng/common/build.sh
                --configuration $(_BuildConfig)
                --prepareMachine
                --ci
                --restore
                --test
                --projects $(Build.SourcesDirectory)/test/UnitTests.proj
                /bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/TestInHelix.binlog
                /p:_CustomHelixTargetQueue=${{ parameters.helixTargetQueue }}
                $(_InternalRuntimeDownloadArgs)
          displayName: Run AoT Tests in Helix
          env:
              SYSTEM_ACCESSTOKEN: $(System.AccessToken)
              HelixAccessToken: $(_HelixApiToken)
              RunAoTTests: 'true'

      - ${{ if in(parameters.agentOs, 'Windows_NT', 'Darwin') }}:
        - task: CopyFiles@2
          displayName: Gather Logs
          inputs:
            SourceFolder: '$(Build.SourcesDirectory)/artifacts'
            Contents: |
              log/$(_BuildConfig)/**/*
              TestResults/$(_BuildConfig)/**/*
              SymStore/$(_BuildConfig)/**/*
              tmp/$(_BuildConfig)/**/*.binlog
            TargetFolder: '$(Build.ArtifactStagingDirectory)'
          continueOnError: true
          condition: always()

        - task: 1ES.PublishBuildArtifacts@1
          displayName: Publish Logs to VSTS
          inputs:
            PathtoPublish: '$(Build.ArtifactStagingDirectory)'
            ArtifactName: '$(_AgentOSName)_$(Agent.JobName)_$(Build.BuildNumber)'
            publishLocation: Container
          continueOnError: true
          condition: always()

# TemplateEngine Jobs
- ${{ if contains(parameters.agentOs, 'TemplateEngine') }}:
  - template: /eng/common/templates-official/job/job.yml@self
    parameters:
      name: ${{ parameters.agentOs }}
      enableMicrobuild: true
      enablePublishBuildAssets: true
      enableTelemetry: true
      enablePublishUsingPipelines: true
      helixRepo: dotnet/sdk
      pool: ${{ parameters.pool }}
      timeoutInMinutes: ${{ parameters.timeoutInMinutes }}
      ${{ if ne(parameters.strategy, '') }}:
        strategy: ${{ parameters.strategy }}
      workspace:
        clean: all
      variables:
        - ${{ each variable in parameters.variables }}:
          - ${{ if ne(variable.name, '') }}:
            - name: ${{ variable.name }}
              value: ${{ variable.value }}
        - name: _AgentOSName
          value: ${{ parameters.agentOs }}
        - name: _TeamName
          value: DotNetCore
        - name: _OfficialBuildIdArgs
          value: ''
        - name: _PublishArgs
          value: ''
        - name: _SignArgs
          value: ''
        - name: _HelixApiToken
          value: ''
        # Helix Testing requires a token when internally run
        - ${{ if ne(variables['System.TeamProject'], 'public') }}:
          - group: DotNet-HelixApi-Access
          - name: _HelixApiToken
            value: $(HelixApiAccessToken)
        - ${{ if and(ne(variables['System.TeamProject'], 'public'), notin(variables['Build.Reason'], 'PullRequest')) }}:
          - group: dotnet-benchview
          - name: _OfficialBuildIdArgs
            value: /p:OfficialBuildId=$(BUILD.BUILDNUMBER)
          - name: _SignArgs
            value: /p:DotNetSignType=$(_SignType) /p:TeamName=$(_TeamName)
          - name: _PerfIterations
            value: 25

      steps:
      - ${{ if ne(variables['System.TeamProject'], 'public') }}:
        - task: PowerShell@2
          displayName: Setup Private Feeds Credentials
          inputs:
            filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.ps1
            arguments: -ConfigFile $(Build.SourcesDirectory)/NuGet.config -Password $Env:Token
          env:
            Token: $(dn-bot-dnceng-artifact-feeds-rw)
      - ${{ if contains(parameters.agentOs, 'Windows_NT') }}:
        - powershell: eng\common\build.ps1
                    $(_CIBuild)
                    -configuration $(_BuildConfig)
                    $(_SignArgs)
                    $(_OfficialBuildIdArgs)
                    /p:Test=false
                    $(_InternalRuntimeDownloadArgs)
          displayName: Build
          env:
            BuildConfig: $(_BuildConfig)
            SYSTEM_ACCESSTOKEN: $(System.AccessToken)
            HelixAccessToken: ${{ parameters.HelixAccessToken }}
        - powershell: eng\common\build.ps1
                -configuration $(_BuildConfig)
                -prepareMachine
                -ci
                -restore
                $(_Test)
                -projects $(Build.SourcesDirectory)\test\Microsoft.TemplateEngine.Cli.UnitTests\Microsoft.TemplateEngine.Cli.UnitTests.csproj
                /bl:$(Build.SourcesDirectory)\artifacts\log\$(_BuildConfig)\TemplateEngine.Cli.UnitTests.binlog
                $(_InternalRuntimeDownloadArgs)
          displayName: Run TemplateEngine Cli Unit Tests
        - powershell: eng\common\build.ps1
                -configuration $(_BuildConfig)
                -prepareMachine
                -ci
                -restore
                $(_Test)
                -projects $(Build.SourcesDirectory)\test\dotnet-new.Tests\dotnet-new.IntegrationTests.csproj
                /bl:$(Build.SourcesDirectory)\artifacts\log\$(_BuildConfig)\dotnet-new.IntegrationTests.binlog
                $(_InternalRuntimeDownloadArgs)
          displayName: Run dotnet new Integration Tests

      - ${{ if not(contains(parameters.agentOs, 'Windows_NT')) }}:
        - script: eng/common/build.sh
                    --configuration $(_BuildConfig)
                    --restore
                    --build
                    --ci
                    $(_SignArgs)
                    $(_OfficialBuildIdArgs)
                    $(_InternalRuntimeDownloadArgs)
                    -p:Test=false
          displayName: Build
          env:
            BuildConfig: $(_BuildConfig)
        - powershell: eng/common/build.sh
                -configuration $(_BuildConfig)
                -prepareMachine
                -ci
                -restore
                -test
                -projects $(Build.SourcesDirectory)/test/Microsoft.TemplateEngine.Cli.UnitTests/Microsoft.TemplateEngine.Cli.UnitTests.csproj
                /bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/TemplateEngine.Cli.UnitTests.binlog
                $(_InternalRuntimeDownloadArgs)
          displayName: Run TemplateEngine Cli Unit Tests
        - powershell: eng/common/build.sh
                -configuration $(_BuildConfig)
                -prepareMachine
                -ci
                -restore
                -test
                -projects $(Build.SourcesDirectory)/test/dotnet-new.Tests/dotnet-new.IntegrationTests.csproj
                /bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/dotnet-new.IntegrationTests.binlog
                $(_InternalRuntimeDownloadArgs)
          displayName: Run dotnet new Integration Tests

      - task: PublishTestResults@2
        displayName: Publish Test Results
        inputs:
          testResultsFormat: xUnit
          testResultsFiles: 'artifacts/TestResults/$(_BuildConfig)/*.xml'
          testRunTitle: '$(_AgentOSName)_$(Agent.JobName)'
          buildPlatform: '$(BuildPlatform)'
          buildConfiguration: '$(_BuildConfig)'
        condition: always()

      - task: CopyFiles@2
        displayName: Gather Logs
        inputs:
          SourceFolder: '$(Build.SourcesDirectory)/artifacts'
          Contents: |
            log/$(_BuildConfig)/**/*
            TestResults/$(_BuildConfig)/**/*
            SymStore/$(_BuildConfig)/**/*
            tmp/$(_BuildConfig)/**/*.binlog
          TargetFolder: '$(Build.ArtifactStagingDirectory)'
        continueOnError: true
        condition: always()

      - task: 1ES.PublishBuildArtifacts@1
        displayName: Publish Logs to VSTS
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)'
          ArtifactName: '$(_AgentOSName)_$(Agent.JobName)_$(Build.BuildNumber)'
          publishLocation: Container
        continueOnError: true
        condition: always()
