Search code examples
azure-devopsazure-identity

Access Azure App Configuration from unit test project ran from Azure DevOps pipeline


I am deploying a new project in Azure Pipeline, I have unit tests into this projects. unit tests must have access an azure app configuration, what is the best way for my project to authenticate to access azure app configuration ?

I don't know how to use the managed identity from there ...


Solution

  • I read a little bit more and it appear managed identity is only used for VM which I don't want.

    To be able to authenticate to Azure AppConfiguration from my unit test ran by my pipeline, I have used Azurecli task which has the ServiceConnection context and I ran dotnet test command, then I export my test results and my code coverage so that Azure DevOps has everything it needs

      - task: AzureCLI@1
        inputs:
          azureSubscription: 'devops-serviceconnection-dev'
          scriptLocation: 'inlineScript'
          inlineScript: |
            cd .\SharepointFileTransfer.Test\
            dotnet add package coverlet.collector
            dotnet test --logger "trx;LogFileName=TestResults.trx" --results-directory $(Build.ArtifactStagingDirectory)/TestResults --collect:"XPlat Code Coverage"
    
      - task: PublishCodeCoverageResults@2
        inputs:
          summaryFileLocation: '$(Build.ArtifactStagingDirectory)/**/*coverage.cobertura.xml'
          failIfCoverageEmpty: false
    
      - task: PublishTestResults@2
        inputs:
          testResultsFormat: 'VSTest'
          testResultsFiles: '$(Build.ArtifactStagingDirectory)/TestResults/*.trx'
          mergeTestResults: true
          failTaskOnFailedTests: true
    

    and then I used AzureCliCredential for my unit test to access to Azure AppConfiguration

    var builder = new ConfigurationBuilder();
    builder.AddAzureAppConfiguration(options =>
    {
        var visualStudioCredential = new VisualStudioCredential(new VisualStudioCredentialOptions() { TenantId = TenantId });
        var tokenCredential = new ChainedTokenCredential(visualStudioCredential, new AzureCliCredential(), new ManagedIdentityCredential());
        options.Connect(new Uri(AppConfigurationEndpoint), tokenCredential)
            .Select("SharepointFileTransfer:*", "dev");
    });
    Configuration = builder.Build();