name: $(BuildID)_$(BuildDefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)
resources:
  - repo: self
    clean: true
jobs:
  ### Linux build ###
  - job: LINUX
    displayName: Linux
    timeoutInMinutes: 75
    strategy:
      # Change maxParallel to 1 make builds run in serial rather than in parallel
      maxParallel: 100
      matrix:
        .Net 6.0:
          FRAMEWORK: net6.0
        .Net 8.0:
          FRAMEWORK: net8.0
        .Net Core 3.1:
          FRAMEWORK: netcoreapp3.1
        .Net Core 2.1.30:
          FRAMEWORK: netcoreapp2.1.30

    condition: succeeded()
    pool:
      vmImage: ubuntu-20.04
    steps:
      - task: UseDotNet@2
        displayName: 'Use .NET Core SDK 2.1'
        inputs:
          packageType: sdk
          version: 2.1.x
          performMultiLevelLookup: true
          installationPath: $(Agent.ToolsDirectory)/dotnet

      - task: UseDotNet@2
        displayName: 'Use .NET Core SDK 3.1'
        inputs:
          packageType: sdk
          version: 3.1.x
          performMultiLevelLookup: true
          installationPath: $(Agent.ToolsDirectory)/dotnet

      - task: UseDotNet@2
        displayName: 'Use .NET SDK 8.0'
        inputs:
          packageType: sdk
          version: 8.x
          performMultiLevelLookup: true
          installationPath: $(Agent.ToolsDirectory)/dotnet

      - task: UseDotNet@2
        displayName: 'Use .NET SDK 6.0'
        inputs:
          packageType: sdk
          version: 6.x
          performMultiLevelLookup: true
          installationPath: $(Agent.ToolsDirectory)/dotnet

      - task: DownloadBuildArtifacts@0
        inputs:
          buildType: 'specific'
          project: 'f9b79625-2860-4d92-a4ee-57b03fabfd10'
          pipeline: '278' # csharp-release-build
          buildVersionToDownload: 'latest'
          downloadType: 'single'
          downloadPath: '$(System.ArtifactsDirectory)'
          artifactName: 'nuget'
          
      - task: CopyFiles@2
        inputs:
          SourceFolder: '$(System.ArtifactsDirectory)'
          Contents: '**'
          TargetFolder: '$(Build.SourcesDirectory)/bin/pkg'
          OverWrite: true

      - task: Docker@1
        displayName: "Start TPM Simulator"
        inputs:
          containerregistrytype: "Container Registry"
          command: "Run an image"
          imageName: aziotbld/testtpm
          containerName: "testtpm-instance"
          ports: |
            127.0.0.1:2321:2321
            127.0.0.1:2322:2322
          restartPolicy: unlessStopped

      - task: Docker@1
        displayName: 'Start Test Proxy'
        inputs:
          containerregistrytype: 'Container Registry'
          command: 'Run an image'
          imageName: aziotbld/testproxy
          containerName: 'testproxy-instance'
          ports: '127.0.0.1:8888:8888'
          restartPolicy: unlessStopped

      - powershell: ./vsts/releaseTest.ps1
        displayName: Test release nugets
        env:
          # Environment variables for IoT Hub E2E tests
          IOTHUB_CONNECTION_STRING: $(IOTHUB-CONNECTION-STRING)
          IOTHUB_X509_DEVICE_PFX_CERTIFICATE: $(IOTHUB-X509-DEVICE-PFX-CERTIFICATE)
          IOTHUB_X509_CHAIN_DEVICE_NAME: $(IOTHUB-X509-CHAIN-DEVICE-NAME)
          IOTHUB_X509_CHAIN_DEVICE_PFX_CERTIFICATE: $(IOTHUB-X509-CHAIN-DEVICE-PFX-CERTIFICATE)
          IOTHUB_USER_ASSIGNED_MSI_RESOURCE_ID: $(IOTHUB-USER-ASSIGNED-MSI-RESOURCE-ID)

          # Environment variables for DPS E2E tests
          DPS_IDSCOPE: $(DPS-IDSCOPE)
          PROVISIONING_CONNECTION_STRING: $(PROVISIONING-CONNECTION-STRING)
          DPS_GLOBALDEVICEENDPOINT: $(DPS-GLOBALDEVICEENDPOINT)
          DPS_X509_PFX_CERTIFICATE_PASSWORD: $(DPS-X509-PFX-CERTIFICATE-PASSWORD)
          DPS_X509_GROUP_ENROLLMENT_NAME: $(DPS-X509-GROUP-ENROLLMENT-NAME)

          # Environment variables for Azure resources used for E2E tests (common)
          X509_CHAIN_ROOT_CA_CERTIFICATE: $(X509-CHAIN-ROOT-CA-CERTIFICATE)
          X509_CHAIN_INTERMEDIATE1_CERTIFICATE: $(X509-CHAIN-INTERMEDIATE1-CERTIFICATE)
          X509_CHAIN_INTERMEDIATE2_CERTIFICATE: $(X509-CHAIN-INTERMEDIATE2-CERTIFICATE)
          X509_CHAIN_INTERMEDIATE2_PFX_CERTIFICATE: $(X509-CHAIN-INTERMEDIATE2-PFX-CERTIFICATE)
          STORAGE_ACCOUNT_CONNECTION_STRING: $(STORAGE-ACCOUNT-CONNECTION-STRING)
          MSFT_TENANT_ID: $(MSFT-TENANT-ID)
          E2E_TEST_AAD_APP_CLIENT_ID: $(E2E-TEST-AAD-APP-CLIENT-ID)
          E2E_TEST_AAD_APP_CLIENT_SECRET: $(E2E-TEST-AAD-APP-CLIENT-SECRET)
          E2E_IKEY: $(E2E-IKEY)

          # Environment variables for the DevOps pipeline
          PROXY_SERVER_ADDRESS: 127.0.0.1:8888
          TARGET_BRANCH: $(System.PullRequest.TargetBranch)
          FRAMEWORK: $(FRAMEWORK)
          AZURE_IOT_LOCALPACKAGES: $(Build.ArtifactStagingDirectory)/nuget

          # Environment variables for invalid certificate tests
          IOTHUB_DEVICE_CONN_STRING_INVALIDCERT: $(IOTHUB-DEVICE-CONN-STRING-INVALIDCERT)
          IOTHUB_CONN_STRING_INVALIDCERT: $(IOTHUB-CONN-STRING-INVALIDCERT)
          DPS_GLOBALDEVICEENDPOINT_INVALIDCERT: $(DPS-GLOBALDEVICEENDPOINT-INVALIDCERT)
          PROVISIONING_CONNECTION_STRING_INVALIDCERT: $(PROVISIONING-CONNECTION-STRING-INVALIDCERT)

          # Environment variables used by Log Analytics Workspace for Azure Security Center
          LA_AAD_APP_ID: $(LA-AAD-APP-ID)
          LA_AAD_APP_CERT_BASE64: $(LA-AAD-APP-CERT-BASE64)
          LA_WORKSPACE_ID: $(LA-WORKSPACE-ID)

      - task: CopyFiles@2
        displayName: 'Copy files to the artifacts folder'
        inputs:
          SourceFolder: '$(Build.SourcesDirectory)'
          Contents: '**/*.trx'
          TargetFolder: '$(Build.ArtifactStagingDirectory)'
        condition: always()

      - task: PublishBuildArtifacts@1
        displayName: 'Publish Artifact: testresults_linux'
        inputs:
          ArtifactName: testresults_linux_$(FRAMEWORK)
        condition: always()

      - task: PublishTestResults@2
        displayName: 'Publish Test Results **/*.trx'
        inputs:
          testRunner: VSTest
          testRunTitle: "Linux Tests ($(FRAMEWORK)) (Attempt $(System.JobAttempt))"
          testResultsFiles: '**/*.trx'
        condition: always()

  ### Windows build ###
  - job: WINDOWS
    displayName: Windows
    timeoutInMinutes: 75
    strategy:
      # Change maxParallel to 1 make builds run in serial rather than in parallel
      maxParallel: 100
      matrix:
        .Net 6.0:
          FRAMEWORK: net6.0
        .Net 8.0:
          FRAMEWORK: net8.0
        .Net Core 3.1:
          FRAMEWORK: netcoreapp3.1
        .Net Core 2.1.30:
          FRAMEWORK: netcoreapp2.1.30
        .Net Framework 4.7.2:
          FRAMEWORK: net472

    condition: succeeded()
    pool:
      vmImage: windows-2022
    steps:
      - task: UseDotNet@2
        displayName: 'Use .NET Core SDK 2.1'
        inputs:
          packageType: sdk
          version: 2.1.x
          performMultiLevelLookup: true
          installationPath: $(Agent.ToolsDirectory)/dotnet

      - task: UseDotNet@2
        displayName: 'Use .NET Core SDK 3.1'
        inputs:
          packageType: sdk
          version: 3.1.x
          performMultiLevelLookup: true
          installationPath: $(Agent.ToolsDirectory)/dotnet

      - task: UseDotNet@2
        displayName: 'Use .NET SDK 8.0'
        inputs:
          packageType: sdk
          version: 8.x
          performMultiLevelLookup: true
          installationPath: $(Agent.ToolsDirectory)/dotnet

      - task: DownloadBuildArtifacts@0
        inputs:
          buildType: 'specific'
          project: 'f9b79625-2860-4d92-a4ee-57b03fabfd10' # azure-iot-sdk
          pipeline: '278' # csharp-release-build
          buildVersionToDownload: 'latest'
          downloadType: 'single'
          downloadPath: '$(System.ArtifactsDirectory)'
          artifactName: 'nuget'

      - task: CopyFiles@2
        inputs:
          SourceFolder: '$(System.ArtifactsDirectory)'
          Contents: '**'
          TargetFolder: '$(Build.SourcesDirectory)/bin/pkg'
          OverWrite: true

      - script: |
          call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat"
          sn -Vr *,31bf3856ad364e35
            
        displayName: 'Disable strong name validation'

      - script: |
          choco install -y squid
            
        displayName: 'Install Squid'

      - powershell: ./vsts/start_tpm_windows.ps1
        displayName: "Start TPM Simulator"

      - powershell: ./vsts/releaseTest.ps1
        displayName: Test release nugets
        env:
          # Environment variables for IoT Hub E2E tests
          IOTHUB_CONNECTION_STRING: $(IOTHUB-CONNECTION-STRING)
          IOTHUB_X509_DEVICE_PFX_CERTIFICATE: $(IOTHUB-X509-DEVICE-PFX-CERTIFICATE)
          IOTHUB_X509_CHAIN_DEVICE_NAME: $(IOTHUB-X509-CHAIN-DEVICE-NAME)
          IOTHUB_X509_CHAIN_DEVICE_PFX_CERTIFICATE: $(IOTHUB-X509-CHAIN-DEVICE-PFX-CERTIFICATE)
          IOTHUB_USER_ASSIGNED_MSI_RESOURCE_ID: $(IOTHUB-USER-ASSIGNED-MSI-RESOURCE-ID)

          # Environment variables for DPS E2E tests
          DPS_IDSCOPE: $(DPS-IDSCOPE)
          PROVISIONING_CONNECTION_STRING: $(PROVISIONING-CONNECTION-STRING)
          DPS_GLOBALDEVICEENDPOINT: $(DPS-GLOBALDEVICEENDPOINT)
          DPS_X509_PFX_CERTIFICATE_PASSWORD: $(DPS-X509-PFX-CERTIFICATE-PASSWORD)
          DPS_X509_GROUP_ENROLLMENT_NAME: $(DPS-X509-GROUP-ENROLLMENT-NAME)

          # Environment variables for Azure resources used for E2E tests (common)
          X509_CHAIN_ROOT_CA_CERTIFICATE: $(X509-CHAIN-ROOT-CA-CERTIFICATE)
          X509_CHAIN_INTERMEDIATE1_CERTIFICATE: $(X509-CHAIN-INTERMEDIATE1-CERTIFICATE)
          X509_CHAIN_INTERMEDIATE2_CERTIFICATE: $(X509-CHAIN-INTERMEDIATE2-CERTIFICATE)
          X509_CHAIN_INTERMEDIATE2_PFX_CERTIFICATE: $(X509-CHAIN-INTERMEDIATE2-PFX-CERTIFICATE)
          STORAGE_ACCOUNT_CONNECTION_STRING: $(STORAGE-ACCOUNT-CONNECTION-STRING)
          MSFT_TENANT_ID: $(MSFT-TENANT-ID)
          E2E_TEST_AAD_APP_CLIENT_ID: $(E2E-TEST-AAD-APP-CLIENT-ID)
          E2E_TEST_AAD_APP_CLIENT_SECRET: $(E2E-TEST-AAD-APP-CLIENT-SECRET)
          E2E_IKEY: $(E2E-IKEY)

          # Environment variables for the DevOps pipeline
          PROXY_SERVER_ADDRESS: 127.0.0.1:3128
          TARGET_BRANCH: $(System.PullRequest.TargetBranch)
          FRAMEWORK: $(FRAMEWORK)
          AZURE_IOT_LOCALPACKAGES: $(Build.ArtifactStagingDirectory)/nuget

          # Environment variables for invalid certificate tests
          IOTHUB_DEVICE_CONN_STRING_INVALIDCERT: $(IOTHUB-DEVICE-CONN-STRING-INVALIDCERT)
          IOTHUB_CONN_STRING_INVALIDCERT: $(IOTHUB-CONN-STRING-INVALIDCERT)
          DPS_GLOBALDEVICEENDPOINT_INVALIDCERT: $(DPS-GLOBALDEVICEENDPOINT-INVALIDCERT)
          PROVISIONING_CONNECTION_STRING_INVALIDCERT: $(PROVISIONING-CONNECTION-STRING-INVALIDCERT)

          # Environment variables used by Log Analytics Workspace for Azure Security Center
          LA_AAD_APP_ID: $(LA-AAD-APP-ID)
          LA_AAD_APP_CERT_BASE64: $(LA-AAD-APP-CERT-BASE64)
          LA_WORKSPACE_ID: $(LA-WORKSPACE-ID)

      - task: CopyFiles@2
        displayName: 'Copy TRX files to the artifacts folder'
        inputs:
          SourceFolder: '$(Build.SourcesDirectory)'
          Contents: '**/*.trx'
          TargetFolder: '$(Build.ArtifactStagingDirectory)'
        condition: always()

      - task: CopyFiles@2
        displayName: 'Copy ETL files to the artifacts folder'
        inputs:
          SourceFolder: '$(Build.SourcesDirectory)'
          Contents: '**/*.etl'
          TargetFolder: '$(Build.ArtifactStagingDirectory)'

        condition: always()

      - task: PublishBuildArtifacts@1
        displayName: 'Publish Artifact: testresults'
        inputs:
          ArtifactName: testresults_windows_$(FRAMEWORK)
        condition: always()

      - task: PublishTestResults@2
        displayName: 'Publish Test Results **/*.trx'
        inputs:
          testRunner: VSTest
          testResultsFiles: '**/*.trx'
          testRunTitle: "Windows Tests ($(FRAMEWORK)) (Attempt $(System.JobAttempt))"
          platform: Windows
          configuration: 'Debug UT + Release E2E ($(FRAMEWORK))'
        condition: always()
