Azure Devops CI/CD CodeCoverage with yml - asp.net-web-api

I have tried to create a code coverage for my webapi. I have followed this link.
I have changed the YML file and got the below build failed error in Azure Devops.
I have used the following code for this. How to fix this?
steps:
- task: DotNetCoreCLI#2
displayName: 'Build project'
inputs:
projects: '**/*.csproj'
arguments: '--output $(Build.BinariesDirectory) --configuration Release'
- task: DotNetCoreCLI#2
displayName: 'Install .NET Core tools from local manifest'
inputs:
command: custom
custom: tool
arguments: 'restore'
- task: VSTest#2
displayName: 'Unit Tests'
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
**\*Tests*.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(Build.BinariesDirectory)'
arguments: '--no-build --configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.BinariesDirectory)/TestResults/Coverage/'
publishTestResults: true
projects: '**/*.Tests.csproj'
- task: DotNetCoreCLI#2
displayName: 'Create code coverage report'
inputs:
command: custom
custom: tool
arguments: 'run reportgenerator -reports:$(Build.SourcesDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/CodeCoverage -reporttypes:HtmlInline_AzurePipelines'
- task: PublishCodeCoverageResults#1
displayName: 'Publish code coverage report'
inputs:
codeCoverageTool: 'cobertura'
summaryFileLocation: '$(Build.SourcesDirectory)/**/coverage.cobertura.xml'

Your Unit Test task places the output under $(Build.BinariesDirectory), but your reports task specifies $(Build.SourcesDirectory). Now, this may be correct if binaries are within source, but without those values, I can't be sure.
I'd rule out I would verify that the file exists where you expect it to be. Try adding this to your pipeline before the coverage task:
- pwsh: |
Write-Host "Checking Sources"
Get-ChildItem $(Build.SourcesDirectory)/coverage.cobertura.xml -Recurse
Write-Host "Checking Binaries"
Get-ChildItem $(Build.BinariesDirectory)/coverage.cobertura.xml -Recurse
Run the pipeline again and look at the log output for that task. If the file exists, then it could be related to the tool installation.

Related

Filter in DotNetCoreCLI task is not working properly

I have a strange problem with DotNetCoreCLI#2 task. I am trying to add filter in the arguments, so only specific tests can run. I am using xUnit and Trait attribute for the goal.
[Trait("Category", "UnitTest")]
Task:
task: DotNetCoreCLI#2
displayName: Run unit tests
inputs:
command: test
projects: '**/*.Tests.csproj'
publishTestResults: true
arguments: >-
-c $(buildConfiguration)
--collect:"XPlat Code Coverage"
-- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura
--filter Category=UnitTest
But when I change/rename the attribute name, its still find and run the tests.
Example:
[Trait("Category", "NotUnitTest")]
When I run dotnet test command localy, the filtration works. And when I change/rename the atribute, no test is run (as expected).
dotnet test --filter Category=UnitTest
Any ideas ?

Encountered error(s) while parsing pipeline YAML: Did not find expected <document start>

I am trying to created build pipeline, I have created classic pipeline from azure portal manually(Which is working) and generated yaml code from working pipeline then created files /.azure/pipelines/ci.yml, /.azure/pipelines/build/build-ci.yml files.
Code as below
/.azure/pipelines/ci.yml
# File: .azure/pipelines/ci.yaml
# Pipeline for the continuous integration to verify the changeset is good enuf,
# and has no platform specific content included.
# HINT: PR triggers are setup in the branch policies.
# Learn more https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml#pr-triggers
trigger: none
variables:
vmImageName: 'windows-2019'
stages:
- stage: Build
displayName: Build
jobs:
- job: Build
pool:
vmImage: $(vmImageName)
demands:
- msbuild
- visualstudio
- vstest
steps:
- template: /.azure/pipelines/build/build-ci.yml
/.azure/pipelines/build/build-ci.yml
parameters:
solution: '**/*.sln'
ArtifactName: 'Some Name'
steps:
- task: NuGetToolInstaller#0
displayName: 'Use NuGet 4.4.1'
inputs:
versionSpec: 4.4.1
- task: NuGetCommand#2
displayName: 'NuGet restore'
inputs:
restoreSolution: '$(Parameters.solution)'
- task: VSBuild#1
displayName: 'Build solution'
inputs:
solution: '$(Parameters.solution)'
vsVersion: 16.0
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactstagingdirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'
- task: VSTest#2
displayName: 'Test Assemblies'
inputs:
testAssemblyVer2: |
**\$(BuildConfiguration)\*test*.dll
!**\obj\**
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'
- task: PublishSymbols#2
displayName: 'Publish symbols path'
inputs:
SearchPattern: '**\bin\**\*.pdb'
PublishSymbols: false
continueOnError: true
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
ArtifactName: '$(Parameters.ArtifactName)'
condition: succeededOrFailed()
Getting Error while running pipeline from yaml file.
Encountered error(s) while parsing pipeline YAML:
/.azure/pipelines/build/build-ci.yml (Line: 5, Col: 1): Did not find expected .

Deploy a Azure Static web app with DevOps in a multi-stage pipeline

I'm currently developing a Blazor WebAssembly application that will be deployed on a Azure Static Web App. Now I need to create my CI/CD pipeline and there is a pretty easy way to build and publish the application using the AzureStaticWebApp task as shown here:
- task: AzureStaticWebApp#0
inputs:
app_location: 'App.Web'
app_build_command: 'dotnet build'
api_location: 'App.Api'
output_location: 'wwwroot'
azure_static_web_apps_api_token: $(deployment_token)
This task however builds and releases the application at the same time. In my pipeline, I'd like to build my Blazor application and store it as an artifact. In one stage of my pipeline, this artifact will be published to a test environment and if all tests pass, then another stage will be to publish this same artifact to the production environment. The goal is to publish the exact same artifact that was tested on the production environment.
Is there a way to accomplish this using the AzureStaticWebApp task or is there any alternative?
i think you can build the artifact in one job like this:
jobs:
- job: BuildWebsite
displayName: Build website
pool:
name: $(azdoPool)
steps:
- checkout: self
- task: Npm#1
displayName: npm install
inputs:
verbose: false
- powershell: |
npx gatsby build
displayName: build
- task: ArchiveFiles#2
displayName: package artifacts
inputs:
rootFolderOrFile: '$(System.DefaultWorkingDirectory)/public/'
includeRootFolder: false
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(webPackageFile)'
- task: PublishPipelineArtifact#1
displayName: publish website artifact
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)/$(webPackageFile)'
ArtifactName: $(artifactName)
(notice the publish to artifact in the last task)
and then test/deploy in another job:
parameters:
application: ''
jobs:
- job: DeployStaticWebsite
displayName: deploy
pool:
name: $(azdoPool)
steps:
- task: DownloadPipelineArtifact#2
displayName: Download $(webPackageFile)
inputs:
artifactName: $(artifactName)
targetPath: $(artifactExtractPath)
itemPattern: '**/$(webPackageFile)'
- task: ExtractFiles#1
inputs:
archiveFilePatterns: $(artifactExtractPath)/$(webPackageFile)
destinationFolder: $(artifactExtractPath)/extracted
displayName: extract website
- task: AzureStaticWebApp#0
displayName: Deploy App
inputs:
output_location: $(extractedZipPath)
azure_static_web_apps_api_token: $(deployTokenMercleCom)
skip_app_build: true
Notice how the first task downloads the artifact/zip from the previous job.
these two jobs should be referenced by one or more stages in a different template file. something like this:
stages:
stage: Build
displayName: Build
jobs:
template: templates/build.yml
parameters:
application: ${{variables.environment}}
stage: app1
displayName: app1
variables:
application: app1
deploymentToken: $(deployTokenApp1)
jobs:
template: templates/deploy.yml
parameters:
application: ${{variables.environment}}
stage: app2
displayName: app2
variables:
application: app2
deploymentToken: $(deployTokenApp2)
jobs:
template: templates/deploy.yml
parameters:
application: ${{variables.environment}}
you can then keep adding stages for app3, app 4 as many as you want. they can all deploy the same artifact that was originally built. You'll also probably want to use deployment jobs if you want to add Environment approvals for each stage (so that you can preapprove each stage deployment).
Note, some of these variable in this yaml need defining before these template examples will run

IPA file is not generating in Azure Devops Pipeline Integration for ReactNative iOS Application

I'm trying to set up CI/CD using the Azure pipeline for my React Native iOS application. Unfortunately, I don't see .ipa being generated in the 'XCode' task. I do see that the archive was created successfully and I do have the checkbox to generate the package. Below is my current yml file.
- task: Xcode#5
inputs:
actions: 'build'
configuration: 'Release'
sdk: 'iphonesimulator'
xcWorkspacePath: 'SampleProject.xcworkspace'
scheme: 'SampleProject'
packageApp: true
signingOption: 'manual'
signingIdentity: '$(APPLE_CERTIFICATE_SIGNING_IDENTITY)'
provisioningProfileUuid: '$(APPLE_PROV_PROFILE_UUID)'
provisioningProfileName: 'Enterprise.mobileprovision'
- task: CopyFiles#2
inputs:
Contents: '**/*.ipa'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
OverWrite: true
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'ios'
publishLocation: 'Container'

Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\s\$(buildStagingDirectory)

Building CI pipeline for .Net core APIs in VSTS. But while building getting the below error.
Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\s\$(buildStagingDirectory)
This is my build definition looks like
I have mentioned PathToPublish as $(buildStagingDirectory)
How do I get rid of this error??
The way I normally tackle this issue is, first, to use the PublishPipelineArtifact#0 task, instead of the deprecated PublishBuildArtifacts#1. So, in YAML, I would replace:
- task: PublishBuildArtifacts#1
displayName: 'PublishBuildArtifacts'
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: 'drop'
for:
- task: PublishPipelineArtifact#0
displayName: 'Publish pipeline artifact'
inputs:
artifactName: 'drop'
targetPath: '$(Build.ArtifactStagingDirectory)'
If I would continue having this error, then I would set the pipeline variable system.debug to true, trigger the pipeline again, and observe the logs from the tasks that produce the artifacts I want to publish. The path should be there somewhere in those logs
I just encounted the exact same error.
Cause
After setting the system.debug variable to true, it revealed that the publish task actually performs a zip of the output folder (which by default is $(build.artifactstagingdirectory)) and places this 1 level higher in the directory structure. It then proceeds to delete the actual folder itself! I'm not sure if this is intended at all or a bug.
Workaround
After observing the above, I simply had the output of the publish task write to $(build.artifactstagingdirectory)\artifact and the resulting Publish Artifact task was then happy to pick up the zip file as it was still pointing to $(build.artifactstagingdirectory)
Default Publish Task output that fails
2018-06-07T02:24:17.8506434Z ##[debug]Zip Source: D:\a\1\a
2018-06-07T02:24:17.8508216Z ##[debug]Zip arguments: Source: D:\a\1\a , target: D:\a\1\a.zip
2018-06-07T02:24:18.0627499Z ##[debug]Successfully created archive D:\a\1\a.zip
2018-06-07T02:24:18.0628200Z ##[debug]rm -rf D:\a\1\a
2018-06-07T02:24:18.0629858Z ##[debug]removing directory
...
...
2018-06-07T02:24:18.3052522Z ##[error]Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\a
Modified output after adding extra directory
2018-06-07T02:38:59.8138062Z ##[debug]Zip Source: D:\a\1\a\artifact
2018-06-07T02:38:59.8139294Z ##[debug]Zip arguments: Source: D:\a\1\a\artifact , target: D:\a\1\a\artifact.zip
2018-06-07T02:39:00.0331460Z ##[debug]Successfully created archive D:\a\1\a\artifact.zip
2018-06-07T02:39:00.0334435Z ##[debug]rm -rf D:\a\1\a\artifact
2018-06-07T02:39:00.0336336Z ##[debug]removing directory
...
...
2018-06-07T02:39:00.4157615Z Uploading 1 files
2018-06-07T02:39:01.9425586Z ##[debug]File: 'D:\a\1\a\artifact.zip' took 1504 milliseconds to finish upload
There is no built-in variable with that name, are you looking for:
$(Build.ArtifactStagingDirectory)
See: https://learn.microsoft.com/en-us/vsts/pipelines/build/variables?view=vsts&tabs=batch
this worked for me
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: $(System.DefaultWorkingDirectory)/bin/Any CPU/Release/netcoreapp3.1
ArtifactName: 'drop'
publishLocation: 'Container'
After you have the build folder you need to copy the file contents into Build.ArtifactStagingDirectory.
- task: CopyFiles#2
inputs:
contents: '/home/vsts/work/1/s/api-project/bin/Debug/net6.0/publish/**'
targetFolder: $(Build.ArtifactStagingDirectory)
You can then publish the artifacts
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
You can then use the artifact in your release pipeline.
I hope this helps I was struggling with this for a while I found help with this issue on this Link https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/publish-build-artifacts?view=azure-devops
Here is the source code for my whole YAML file for reference this is a pipeline for a Monorepo.
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
branches:
include:
- main
paths:
include:
- api-project/*
pool:
vmImage: ubuntu-latest
variables:
buildConfiguration: 'Release'
steps:
- script: |
dotnet build --configuration $(buildConfiguration)
dotnet publish
displayName: 'dotnet build and publish'
workingDirectory: './api-project'
- task: CopyFiles#2
inputs:
contents: '/home/vsts/work/1/s/api-project/bin/Debug/net6.0/publish/**'
targetFolder: $(Build.ArtifactStagingDirectory)
- script: |
ls
displayName: check location
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
As has already been pointed out, you probably meant $(build.StagingDirectory) so with the dot. But I'm regarding that as a simple typo, as I've encountered the same problem.
The answer is that when PUBLISHING the BUILD variables don't seem to be available (despite it being shown as the example in the tool-tip). What you probably want is $(System.ArtifactsDirectory). That worked for me.

Resources