trigger: none

pr: none

pool:
  vmImage: 'ubuntu-22.04'

variables:
  buildConfiguration: 'Release'
  folderPath: '$(Build.SourcesDirectory)'
  appRegistrationTenantId: 'cdc5aeea-15c5-4db6-b079-fcadd2505dc2'
  authenticodeSignId: '2d5c4ab9-0b7e-4f60-bb92-70322df77b94'
  nugetSignId: 'a94a770a-9a7b-4888-a3ea-24584b851e49'

stages:
- stage: Build_Test_Sign_Pack
  jobs:
  - job: BuildTestSignPack
    displayName: 'Build, Test, Sign, and Pack'
    steps:
      - checkout: self

      - task: UseDotNet@2
        displayName: 'Use .NET 9'
        inputs:
          packageType: 'sdk'
          version: '9.0.x'

      - script: dotnet restore
        displayName: 'Restore'

      - script: dotnet build --no-restore --configuration $(buildConfiguration)
        displayName: 'Build'

      - script: dotnet test --no-restore --verbosity normal --logger trx --configuration $(buildConfiguration)
        displayName: 'Test'

      - task: EsrpCodeSigning@5
        displayName: "Authenticode Sign DLLs"
        inputs:
          ConnectedServiceName: 'TeamsESRP-CP-230012'
          AppRegistrationTenantId: '$(appRegistrationTenantId)'
          AppRegistrationClientId: '$(authenticodeSignId)'
          AuthSignCertName: '$(authenticodeSignId)'
          EsrpClientId: '$(authenticodeSignId)'
          AuthAKVName: esrp-teams
          UseMSIAuthentication: true
          FolderPath: '$(folderPath)'
          Pattern: 'Libraries/**/*.dll'
          UseMinimatch: true
          signConfigType: 'inlineSignParams'
          inlineOperation: |
            [
              {
                  "KeyCode": "CP-230012",
                  "OperationCode": "SigntoolSign",
                  "Parameters": {
                      "OpusName": "Microsoft",
                      "OpusInfo": "http://www.microsoft.com",
                      "FileDigest": "/fd \"SHA256\"",
                      "PageHash": "/NPH",
                      "TimeStamp": "/tr \"http://rfc3161.gtm.corp.microsoft.com/TSS/HttpTspServer\" /td sha256"
                  },
                  "ToolName": "sign",
                  "ToolVersion": "1.0"
              },
              {
                "KeyCode": "CP-230012",
                "OperationCode": "SigntoolVerify",
                "Parameters": {},
                "ToolName": "sign",
                "ToolVersion": "1.0"
              }
            ]
          SessionTimeout: '60'
          MaxConcurrency: '50'
          MaxRetryAttempts: '5'

      - task: DotNetCoreCLI@2
        displayName: 'Pack NuGet Packages'
        inputs:
          command: pack
          packagesToPack: '$(folderPath)/Libraries/**/*.csproj'
          packDestination: '$(Build.ArtifactStagingDirectory)'
          includeSymbols: false
          nobuild: true
          configuration: '$(buildConfiguration)'
          arguments: '/p:SymbolPackageFormat=snupkg'

      - task: EsrpCodeSigning@5
        displayName: 'Sign NuGet Packages'
        inputs:
          ConnectedServiceName: 'TeamsESRP-CP-401405'
          AppRegistrationTenantId: '$(appRegistrationTenantId)'
          AppRegistrationClientId: '$(nugetSignId)'
          AuthSignCertName: '$(nugetSignId)'
          EsrpClientId: '$(nugetSignId)'
          AuthAKVName: esrp-teams
          UseMSIAuthentication: true
          FolderPath: '$(Build.ArtifactStagingDirectory)'
          Pattern: |
            *.nupkg
            *.snupkg
          UseMinimatch: true
          signConfigType: 'inlineSignParams'
          inlineOperation: |
            [
              {
                "KeyCode": "CP-401405",
                "OperationCode": "NuGetSign",
                "ToolName": "sign",
                "ToolVersion": "1.0",
                "Parameters": {}
              },
              {
                "KeyCode": "CP-401405",
                "OperationCode": "NuGetVerify",
                "Parameters": {},
                "ToolName": "sign",
                "ToolVersion": "1.0"
              }
            ]
          SessionTimeout: '20'
          MaxConcurrency: '50'
          MaxRetryAttempts: '5'
          PendingAnalysisWaitTimeoutMinutes: '5'

      - task: PublishBuildArtifacts@1
        displayName: 'Publish NuGet Artifacts'
        inputs:
          PathToPublish: '$(Build.ArtifactStagingDirectory)'
          ArtifactName: 'Packages'

- stage: PushToNuGet
  displayName: 'Push NuGet Packages to nuget.org'
  dependsOn: Build_Test_Sign_Pack
  condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
  jobs:
  - deployment: PushPackages
    displayName: 'Manual Approval Required to Push Packages'
    environment:
      name: 'teams-net-publish'
    strategy:
      runOnce:
        deploy:
          steps:
          - download: current
            artifact: Packages

          - task: NuGetCommand@2
            displayName: 'Push NuGet Packages'
            inputs:
              command: push
              packagesToPush: '$(Pipeline.Workspace)/Packages/*.nupkg'
              nuGetFeedType: external
              publishFeedCredentials: 'Microsoft.Teams.*'