Selectively publish projects as Nuget packages to Azure Devops Artifacts - azure-devops

We have a solution with several projects. We only want 2 projects of them to be published to artifacts. Ideally we can manage this by changing settings in the project, hence the pipeline can be generic. We have been searching (trial/error) for a possible solution either on the pipeline side, either on the projects side. None of them worked or no projects ended up in the artifacts or all. Any suggestions ?
#Build and distribute nnn.Core NuGets to nnnNugets artifacts feed
name: $(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
variables:
feedName : 'nnnNugets'
buildConfiguration: 'debug'
trigger:
- nugetify
pool:
vmImage: windows-latest
steps:
- task: DotNetCoreCLI#2
displayName: 'Build'
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'
- task: DotNetCoreCLI#2
displayName: 'Pack'
inputs:
command: pack
versioningScheme: byBuildNumber
- task: NuGetAuthenticate#1
displayName: 'NuGet Authenticate'
- task: NuGetCommand#2
displayName: 'NuGet push'
inputs:
command: push
publishVstsFeed: '$(feedName)'
allowPackageConflicts: true
- task: PublishSymbols#2
inputs:
searchPattern: '**/bin/**/*.pdb'
publishSymbols: true
symbolServerType: 'teamServices'
SymbolExpirationInDays: 1000
IndexableFileFormats: Pdb

The solution was to update the PackagesToPack propery so that the selections matches the projects you want.include
- task: DotNetCoreCLI#2
displayName: 'Pack All'
inputs:
command: pack
packagesToPack: '**/*.csproj; !**/Debug/**.csproj'
versioningScheme: byBuildNumber

Related

How can I trigger my build on Azure DevOps using YAML file?

How can I trigger my build on Azure DevOps using YAML file?
I did a merge on my master branch. Build was tiggered.
Then I did a pull request from master to
release/development
release/staging
release/production
in this order. Only the merge on release/production triggered the build.
Why? To make sure it is not just a question of the correct version of the YAML file on the correct branch I repeated these actions a second time. Same result.
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4
trigger:
branches:
include:
- master
- release/production
- release/development
- release/staging
pool:
name: Hosted VS2017
demands:
- msbuild
- visualstudio
- vstest
name: $(Date:yy).$(Date:MM).$(Rev:r)
variables:
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
displayName: 'Use NuGet 4.4.1'
inputs:
versionSpec: 4.4.1
- task: NuGetCommand#2
displayName: 'NuGet restore'
inputs:
restoreSolution: $(Solution)
- task: VSBuild#1
displayName: 'Build solution'
inputs:
solution: $(Solution)
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"'
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'
- task: VSTest#2
displayName: 'Test Assemblies'
inputs:
testAssemblyVer2: |
**\$(BuildConfiguration)\*test*.dll
!**\obj\**
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'
diagnosticsEnabled: True
- 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()
Why the master and production build are starting and not the other one?
Check if you have set the trigger on the web UI for the build pipeline. The triggers set on the web UI will override that set in the YAML files.
If the triggers set on the web UI only includes the master and release/production branches, the pipeline will only be triggered for the two branches.
To avoid this, you can disable the triggers set on the web UI and only keep the triggers set in the YAML files.
Did you merge yaml to other branches? The build may not start if it is not in a branch. An example of the development branch:

Azure devOps pipeLine failed with sonarCloudPrepare

I have this situation ,i trying to build and connect my pipeLine with sonarCloud i have a .net 5.0.x project and .net core 3.1.x , when build the project normally works ok , but when start to add the sonar cloud task starst to fail.
with the next yalm my build is working fine :
trigger:
- qa
- develop
- master
resources:
- repo: self
variables:
tag: '$(Build.BuildId)'
stages:
- stage: Build
displayName: Build image
jobs:
- job: Build
displayName: Build
pool:
vmImage: 'windows-latest'
steps:
- task: UseDotNet#2
displayName: 'Install .NET 5.x.x SDK'
inputs:
version: '5.0.x'
performMultiLevelLookup: true
includePreviewVersions: true # Required for preview versions
- task: UseDotNet#2
displayName: Use Dot Net Core 3.1.x
inputs:
packageType: 'sdk'
version: '3.1.x'
- task: DotNetCoreCLI#2
inputs:
command: 'build'
projects: '**/*.sln'
displayName: 'dotnet build'
But when i add SonarCloudPrepare#1 before my build :
trigger:
- qa
- develop
- master
resources:
- repo: self
variables:
tag: '$(Build.BuildId)'
stages:
- stage: Build
displayName: Build image
jobs:
- job: Build
displayName: Build
pool:
vmImage: 'windows-latest'
steps:
- task: UseDotNet#2
displayName: 'Install .NET 5.x.x SDK'
inputs:
version: '5.0.x'
performMultiLevelLookup: true
includePreviewVersions: true # Required for preview versions
- task: UseDotNet#2
displayName: Use Dot Net Core 3.1.x
inputs:
packageType: 'sdk'
version: '3.1.x'
- task: SonarCloudPrepare#1
inputs:
SonarCloud: 'tp-survey'
organization: 'teleperformance-sonarcloud'
scannerMode: 'MSBuild'
projectKey: tp-cm-back-survey
projectName: 'tp-cm-back-survey'
extraProperties: |
sonar.exclusions="**/bin/**/*,**/obj/**/*,**/Migrations/**,**/*.dll"
sonar.coverage.exclusions="test/**"
sonar.cs.roslyn.ignoreIssues=true
sonar.verbose=true
- task: DotNetCoreCLI#2
inputs:
command: 'build'
projects: '**/*.sln'
displayName: 'dotnet build'
I getting this error :
Command-line syntax error : 'D:\a\1\s\TPSurveyUnitTest\bin\Debug\.netcoreapp,version=v5.0\TPSurveyUnitTest.dll.RoslynCA.json' is not a valid value for the '/errorlog:' option. The value must be of the form '<file>[,version={1|1.0|2|2.1}]'. [D:\a\1\s\TPSurveyUnitTest\TPSurveyUnitTest.csproj]
181 Warning(s)
1 Error(s)
Why are doing wrong ?
i tried adding the restore task but the same error.
Thanks in advance.
The SonarCloud tasks require .NET Core 2.x to be available in the machine. So your fix will be by adding this task before the SonarCloudPrepare task
- task: UseDotNet#2
displayName: 'Install .Net Core sdk 2.1.x (needed for sonar scanner)'
inputs:
version: 2.1.x
- task: DotNetCoreCLI#2
displayName: 'dotnet restore'
inputs:
command: 'restore'
projects: '**/*.csproj'
This is mentioned here in the sonarsource community issue.
https://community.sonarsource.com/t/dotnet-core-3-0-dotnet-sonarscanner/14753
Update:
You should also do dotnet restore before running the scanner.
We tried many of the methods that are indicated in the article. The following helped, the following settings were added to the project (.csproj ):
TEST .csproj
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<OutputPath>..\YourProductPath\bin\Tests\</OutputPath>
MAIN .csproj
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<OutputPath>..\YourProductPath\bin\Release\</OutputPath>
In the pipeline, change the build from .sln to .csproj
- task: DotNetCoreCLI#2
displayName: 'dotnet build'
inputs:
projects: '$(Build.SourcesDirectory)/YourProductPath/YourProduct.csproj'
arguments: '--configuration $(BuildConfiguration)'

Azure Dev Ops test step not finding tests (.net 5)

I have a simple .net 5 project that contains a main project and a unit test project with nUnit3 tests. On my machine (a mac with visual studio for mac fwiw) tests are discovered on build and work as expected.
When I try and set up a build pipeline in Azure dev ops, none of my tests are discovered and I get the follow line in the logs:
Test run detected DLL(s) which were built for different framework and platform versions. Following DLL(s) do not match current settings, which are .NETFramework,Version=v4.0 framework and X86 platform.
GenericRepositoryTests.dll is built for Framework .NETCoreApp,Version=v5.0 and Platform AnyCPU.
Microsoft.TestPlatform.CommunicationUtilities.dll is built for Framework .NETStandard,Version=v2.0 and Platform AnyCPU.
There are more Microsoft dlls it reports but you get the idea. Here is my yaml file for the build process:
# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- main
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
# Added this step manually
- task: UseDotNet#2
displayName: 'Use .NET Core sdk 5.0.100'
inputs:
packageType: 'sdk'
version: '5.0.100'
includePreviewVersions: true
# Added this step manually
- task: DotNetCoreCLI#2
displayName: 'dotnet restore'
inputs:
command: restore
projects: '**/*.csproj'
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
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
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
**\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.DefaultWorkingDirectory)'
codeCoverageEnabled: true
How can I make sure the Azure Dev Ops test runner settings are set up to run .net 5 dlls?
Thanks
Thanks to jessehouwing I found that the answer I needed was to use dotnet test rather than VSTest:
Here is my final pipeline:
trigger:
- main
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: UseDotNet#2
displayName: 'Use .NET Core sdk 5.0.100'
inputs:
packageType: 'sdk'
version: '5.0.100'
includePreviewVersions: true
- task: DotNetCoreCLI#2
displayName: 'dotnet restore'
inputs:
command: restore
projects: '**/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'dotnet build'
inputs:
command: 'build'
projects: '**/*.sln'
- task: DotNetCoreCLI#2
displayName: 'dotnet test'
inputs:
command: 'test'
projects: '**/*tests.csproj'
arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'
testRunTitle: 'Generic Repository Tests'
Please try to install the NUnit3TestAdapter <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" /> and then check the result.
See: TFS Tests do not match framework settings for more workarounds.
.csproj
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Pipeline
trigger: none
pr: none
pool:
vmImage: 'windows-latest'
stages:
- stage: CreateDeploymentArtifacts
displayName: Create Deployment Artifacts
jobs:
- job: Test
displayName: Run Xunit test
steps:
- task: DotNetCoreCLI#2
displayName: 'Restore Packages'
inputs:
command: 'restore'
projects: 'LegalRegTech.Web.sln'
- task: DotNetCoreCLI#2
displayName: 'Build application'
inputs:
command: 'build'
projects: 'LegalRegTech.Web.sln'
arguments: '--no-restore'
- task: DotNetCoreCLI#2
displayName: 'Run tests'
inputs:
command: 'test'
projects: '**/*tests.csproj'
arguments: '--collect:"XPlat Code Coverage"'
- script: 'dotnet tool install -g dotnet-reportgenerator-globaltool'
displayName: 'Install ReportGenerator tool'
- script: 'reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"'
displayName: 'Create reports'
- task: PublishCodeCoverageResults#1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml'

Azure DevOps Pipeline: can't get tests to compile

I have a solution containing 4 projects (names simplified for clarity)
MyApplication (.Net Standard 2.0)
MyApplication.Test.1 (.Net Core 3.1)
MyApplication.Test.2 (.Net Core 3.1)
MyApplication.Test.3 (.Net Core 3.1)
The solution folder contains the *.sln file, and one folder per project (plus stuff for Git source control).
The three test projects each consume a private NuGet package, "MyNugetPackage"; this package exists on our Azure environment as an artifact. They also use NUnit3 - the test project's NuGet references are as follows:
<PackageReference Include="MyNugetPackage" Version="2020.1.1" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
I created the following YAML file for our pipeline:
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
buildConfiguration: release
platform: x64
steps:
- task: NuGetToolInstaller#1
inputs:
versionSpec:
checkLatest: true
- task: NuGetCommand#2
inputs:
command: 'restore'
restoreSolution: '**/*.sln'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: MSBuild#1
inputs:
solution: '**/*.sln'
msbuildArchitecture: 'x64'
platform: 'Any CPU'
configuration: 'Release'
msbuildArguments: '-m'
clean: true
- task: DotNetCoreCLI#2
inputs:
command: test
projects: '**/*Tests/*.csproj'
arguments: '--configuration Release'
Initially, this failed at the MSBuild step, until I worked out how to set up the permissions for it to pull from our Azure artifact (the vstsFeed property). Having got the MSBuild step to execute successfully, it then fails when the DonNetCoreCLI (Test) step executes: I get multiple messages similar to:
MyTestFile.cs(13,22): error CS0234: The type or namespace name 'ABC'
does not exist in the namespace 'MyNugetPackage' (are you missing an
assembly reference?)
[d:\a\1\s\MyApplication.Test.1\MyApplication.Test.1.csproj]
I'm assuming that this means the Test projects can't find our private 'MyNugetPackage'. But how could this be when MSBuild succeeded?
I tried swapping the MSBuild step out for a Visual Studio build, but to no avail (again, build step worked, test step failed):
- task: VSBuild#1
inputs:
solution: '**\*.sln'
platform: 'any cpu'
configuration: 'release'
clean: true
maximumCpuCount: true
msbuildArchitecture: 'x64'
I then tried swapping the MSBuild step for the .NET Core build so that it matched the job for the tests
- task: DotNetCoreCLI#2
displayName: Build
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration Release'
And now the BUILD step failed.
So it seems to me that I have two routes forward:
Either stick with either MSBuild or Visual Studio Build, and work out how to then execute my tests, or
Work out how to compile successfully with DotNetcoreCLI#2 rather than use MSBuild/Visual Studio Build, and hope that the tests will then work as expected
The documentation from Microsoft seemed so simple (Run your tests)....hoping someone can help guide me on the right path.
You can run dotnet test command on your local machine to check if it works fine locally. Make sure you can successfully build and test your projects locally first.
In you pipeline, You can try use vstest task after msbuild task to run the tests.
- task: VSTest#2
inputs:
testAssemblyVer2: |
**\*[Tt]est*.dll
!**\*[Tt]estAdapter*.dll
!**\obj\**
searchFolder: $(system.defaultworkingdirectory)
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
You can also run Dotnet restore task to restore the packages before dotnet test task
- task: DotNetCoreCLI#2
displayName: 'dotnet restore'
inputs:
command: restore
projects: '**/*.csproj'
feedsToUse: select
vstsFeed: 'nugettest'
So the solution that (eventually) worked for me was to use the .Net Core (DotNetCoreCLI#2) build, and to break every step apart, e.g.
# 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:
- master
pool:
vmImage: 'windows-latest'
variables:
buildConfiguration: 'Release'
platform: x64
steps:
- task: DotNetCoreCLI#2
displayName: 'Restore for MyApplication'
inputs:
command: 'restore'
projects: 'MyApplication/*.csproj'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: DotNetCoreCLI#2
displayName: 'Restore for MyApplication.Test.1'
inputs:
command: 'restore'
projects: 'MyApplication.Test.1/*.csproj'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: DotNetCoreCLI#2
displayName: 'Restore for MyApplication.Test.2'
inputs:
command: 'restore'
projects: 'MyApplication.Test.2/*.csproj'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: DotNetCoreCLI#2
displayName: 'Restore for MyApplication.Test.3'
inputs:
command: 'restore'
projects: 'MyApplication.Test.3/*.csproj'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: DotNetCoreCLI#2
displayName: 'Build MyApplication'
inputs:
command: 'build'
projects: 'MyApplication/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Build Test.1'
inputs:
command: 'build'
projects: 'MyApplication.Test.1/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Build Test.2'
inputs:
command: 'build'
projects: 'MyApplication.Test.2/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Build Test.3'
inputs:
command: 'build'
projects: 'MyApplication.Test.3/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Execute Test.1s'
inputs:
command: 'test'
projects: 'MyApplication.Test.1/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Execute Test.2s'
inputs:
command: 'test'
projects: 'MyApplication.Test.2/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Execute Test.3s'
inputs:
command: 'test'
projects: 'MyApplication.Test.3/*.csproj'

Where can I see the created package in Azure DevOps to upload to the store?

I want to create packages to load to the store.
Here is the YAML so far
trigger:
- master
pool:
vmImage: 'VS2017-Win2016'
variables:
solution: '**/*.sln'
buildPlatform: 'x64'
buildConfiguration: 'Release'
appxPackageDir: '$(build.artifactStagingDirectory)\AppxPackages\\'
steps:
- task: NuGetToolInstaller#0
- task: NuGetCommand#2
inputs:
restoreSolution: '**\*.sln'
feedsToUse: config
nugetConfigPath: 'MyApp.Win10/mynuget.config'
- task: VSBuild#1
inputs:
platform: 'x64'
solution: '**\*.sln'
configuration: '$(buildConfiguration)'
msbuildArgs: '/p:AppxBundlePlatforms="$(buildPlatform)" /p:AppxPackageDir="$(appxPackageDir)" /p:AppxBundle=Always /p:UapAppxPackageBuildMode=StoreUpload'
The docs mention the Artifacts explorer on the Artifacts tab of the builds results page. But I cant find it.
I have also looked at the Artifacts in the project but they only contain some nuget packages I put there.
You don't see the artifacts becuase you don't have the "Publish Build Artifacts" task.
For example, add this task:
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: '$(appxPackageDir)'
artifactName: 'drop'