Two projects in one repository - azure-devops

I use Azure DevOps Pipelines with GitVersion.
I have one git repository with two projects in folders:
ProjectA/ProjectA.csproj
ProjectA/azure-pipelines.yml
ProjectA/gitversion.yml
ProjectB/ProjectB.csproj
ProjectB/azure-pipelines.yml
ProjectB/gitversion.yml
The beginning of my pipeline.yml looks like this:
trigger:
branches:
include:
- '*'
paths:
include:
- 'ProjectA' <-- ProjectB in the other file
# Setup everything
steps:
- task: NuGetCommand#2
displayName: 'Install GitVersion CLI Tool'
inputs:
command: custom
arguments: install GitVersion.CommandLine -Version 5.0.0 -OutputDirectory $(Build.BinariesDirectory)/tools -ExcludeVersion
- task: UseDotNet#2
displayName: 'Install .NET Core SDK 3.0'
inputs:
packageType: 'sdk'
version: '3.x'
includePreviewVersions: true
# Execute the GitVersion Tool
- script: mono $(Build.BinariesDirectory)/tools/GitVersion.CommandLine/tools/GitVersion.exe /output buildserver /nofetch
displayName: 'Execute GitVersion CLI Tool'
followed by dotnet restore, build and so on.
My issue is let's say ProjectA get's the version "-alpha12" through commits.
And then I commit to ProjectB it get's the version "-alpha13".
But these two projects are independent from each other and so should the version.
What I mentioned is, there is nowhere a path to the gitversion.yml and I think the build process is just working with defaults.
But I don't know how to specify the path to the gitversion.yml and don't know if that would solve my issue.
Can someone help me?

Simply put, this isn't going to work.
GitVersion is intended to assert a single version number for the entire repository it is being run against. It doesn't know anything about the different projects that exist within that repository, and therefore it can't assert a different version number for them.
You will only ever get a single version number from GitVersion.
The recommendation would be to split Project A and B into separate repositories.

Related

Azure pipeline for Azure Function does not find project

I've got a build pipe for an Azure Function using .Net Core 3.1.x. All the steps until the publishing are doing fine. I can get the publish step working by using script, but not through the yaml task. What am I missing?
Script (works)
- script: dotnet publish --configuration Release .\af-process-mds-vehicle-output-to-deviation\af-process-mds-vehicle-output-to-deviation.csproj
Task (does not work)
- task: DotNetCoreCLI#2
displayName: 'Publish Project'
inputs:
command: 'publish'
configuration: 'Release'
projects: '.\af-process-mds-vehicle-output-to-deviation\af-process-mds-vehicle-output-to-deviation.csproj'
zipAfterPublish: true
It doesn't find the project.
Here's the error message.
2021-10-29T05:21:44.3024816Z ##[section]Starting: dotnet publish
2021-10-29T05:21:44.3150367Z ==============================================================================
2021-10-29T05:21:44.3150726Z Task : .NET Core
2021-10-29T05:21:44.3151190Z Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command
2021-10-29T05:21:44.3151475Z Version : 2.187.0
2021-10-29T05:21:44.3151733Z Author : Microsoft Corporation
2021-10-29T05:21:44.3152035Z Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
2021-10-29T05:21:44.3152373Z ==============================================================================
2021-10-29T05:21:44.7797987Z [command]C:\Windows\system32\chcp.com 65001
2021-10-29T05:21:44.7903026Z Active code page: 65001
2021-10-29T05:21:44.7927221Z Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
2021-10-29T05:21:44.8938257Z ##[error]No web project was found in the repository. Web projects are identified by presence of either a web.config file, wwwroot folder in the directory, or by the usage of Microsoft.Net.Web.Sdk in your project file. You can set Publish web projects property to false (publishWebProjects: false in yml) if your project doesn't follow this convention or if you want to publish projects other than web projects.
2021-10-29T05:21:44.9001249Z Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://learn.microsoft.com/en-us/dotnet/core/tools/ and https://learn.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
2021-10-29T05:21:44.9003648Z ##[error]Project file(s) matching the specified pattern were not found.
2021-10-29T05:21:44.9182124Z ##[section]Finishing: dotnet publish
After the tips from the answer I got the pipe working. Here's the full working pipe. (Still don't know why it didn't work earlier.)
Working pipe:
name : af-vehicle-sync-to-deviation
## if there is a change is the deviation folder for the main branch. Then trigger.
trigger:
branches:
include:
- main
paths:
include:
- af-process-mds-vehicle-output-to-deviation/*
pool:
vmImage: 'windows-latest'
variables:
buildConfiguration: 'Release'
SolutionPath: '**\*.sln'
stages:
- stage: Build
displayName: Build solution
jobs:
- job: Build
displayName: Build and publish solution
steps:
- checkout: self
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: $(SolutionPath)
- task: UseDotNet#2
inputs:
packageType: 'sdk'
version: '3.1.x'
displayName: 'Use .NET Core SDK 3.1.x'
- task: DotNetCoreCLI#2
inputs:
command: 'build'
configuration: $(buildConfiguration)
projects: '$(SolutionPath)'
displayName: 'Build solution'
- task: DotNetCoreCLI#2
displayName: 'Publish Project'
inputs:
command: 'publish'
configuration: 'Release'
projects: '**\*.csproj'
publishWebProjects: false
zipAfterPublish: true
arguments: '--output $(Build.ArtifactStagingDirectory)/$(buildConfiguration)'
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
You could use '**/*.csproj' but honestly, I would do something like this answer and add a script to list out all the files and folders recursively before this step that fails.
Assuming that you have a restore or build step before this publish you could add it after those, or just as the first step after your checkout one.
You can also inspect the logs of earlier steps to see the file path/s., instructions on doing this are available here.
Using $(System.DefaultWorkingDirectory) as your root is also recommended, rather than .\, so you would have '$(System.DefaultWorkingDirectory)\af-process-mds-vehicle-output-to-deviation...'.
Edit
If you look at the logs for your build step you will see entries like /home/vsts/work/1/s/XXX.YYY.ZZZ/XXX.YYY.ZZZ.csproj that refer to the different projects inside your solution. By default most commands will be run in $(System.DefaultWorkingDirectory) which would equate to /home/vsts/work/1/s/ in this instance, you can think of it as the root of your repository - there is more information on this structure here.
The error you were encountering is actually about the lack of a web project, rather than a path issue though, for the build step it is best practice to use the --output <output-directory-here> flag to output the compile files into a specific folder, that way you can easily publish that folder.

Not Loading Indexed Sources for nuget packages from Azure Devpos during debugging

Trying to use azure yml for build pipleline to publish symbols to allow nuget pkg to be debuggable usin azure devops. I see PDB files are donwloaded to the symbols cache folder but stepping thru is asking for source file location in visual studio, even when i have indexed the source code during publish symbol.
I have tried to enable different options in visual studio debugging but nothing seems to help
Here is my yml
# 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
name: $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r) # need this for byBuildNumber verisonScheme nuget pack
# the build will trigger on any changes to the master branch
trigger:
- master
# the build will run on a Microsoft hosted agent, using the lastest Windows VM Image
pool:
vmImage: 'windows-latest'
# these variables are available throughout the build file
# just the build configuration is defined, in this case we are building Release packages
variables:
buildConfiguration: 'Release'
#The build has 3 seperate tasks run under 1 step
steps:
# The first task is the dotnet command build, pointing to our csproj file
- task: DotNetCoreCLI#2
displayName: 'dotnet build'
inputs:
command: 'build'
arguments: '--configuration $(buildConfiguration)'
projects: 'src/Common.Core/Common.Core.csproj'
- task: PublishSymbols#2
inputs:
symbolsFolder: '$(Build.SourcesDirectory)'
searchPattern: '**/bin/**/*.pdb'
indexSources: true
publishSymbols: true
symbolServerType: 'teamServices'
treatNotIndexedAsWarning: true
symbolsProduct: '$(Build.DefinitionName)'
symbolsVersion: '$(Build.BuildNumber)'
symbolsArtifactName: '$(name).Symbols_$(BuildConfiguration)'
# The second task is dotnet pack command again pointing to the csproj file
# The nobuild means the project will not be compiled before running pack, because its already built in above step
- task: DotNetCoreCLI#2
displayName: "dotnet pack"
inputs:
command: 'pack'
arguments: '--configuration $(buildConfiguration)'
packagesToPack: 'src/Common.Core/Common.Core.csproj'
nobuild: true
includeSymbols: true
versioningScheme: 'byBuildNumber'
# The last task is a nuget command, nuget push
# This will push any .nupkg files to the 'Nuget' artifact feed
# allowPackageConflicts allows us to build the same version and not throw an error when trying to push
# instead it just ingores the latest package unless the version changes
- task: NuGetCommand#2
displayName: 'nuget push'
inputs:
command: 'push'
feedsToUse: 'select'
packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
nuGetFeedType: 'internal'
publishVstsFeed: 'myNuget'
allowPackageConflicts: true
I would expect that when i am debugging nuget packages with symbols enabled with idnexed soruce code, it automatically downloads the pdf file and the source code.
Visual Studio Settings for debugger
I see PDB files are donwloaded to the symbols cache folder but
stepping thru is asking for source file location in visual studio,
even when i have indexed the source code during publish symbol.
You should let the debugger know where to find your source files. First, please rename your xx.nupkg to xx.zip and check if it contains necessary source files.
After that you can right-click Solution in Solution Explorer=>Properties=>Debug Source Files,click the New Line option to add the path of your nuget source files to the debug source files setting.
I would expect that when i am debugging nuget packages with symbols
enabled with idnexed soruce code, it automatically downloads the pdf
file and the source code.
Maybe you can get some help from this issue. You can try setting the build action of the source files as C# compiler when you create the nuget package for .net core.

##[error]Could not find version number data in the following environment variable: BUILD_BUILDNUMBER

I am using below yaml to generate my NuGet pkg. I want to have a unique id with a date for my package. but I keep getting an error saying ould not find version number data in the following environment variable: BUILD_BUILDNUMBER
##[error]Could not find version number data in the following environment variable: BUILD_BUILDNUMBER. The value of the variable
should contain a substring with or are positive integers.
I have tried to use name: $(Build.DefinitionName)-$(date:yyyyMMdd)$(rev:.r) # need this for byBuildNumber verisonScheme nuget pack.
Also tried to set BUILD_BUILDNUMBER as a variable in the yaml file.
# 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
name: $(Build.DefinitionName)-$(date:yyyyMMdd)$(rev:.r) # need this for byBuildNumber verisonScheme nuget pack
# the build will trigger on any changes to the master branch
trigger:
- master
# the build will run on a Microsoft hosted agent, using the lastest Windows VM Image
pool:
vmImage: 'windows-latest'
# these variables are available throughout the build file
# just the build configuration is defined, in this case we are building Release packages
variables:
buildConfiguration: 'Release'
#The build has 3 seperate tasks run under 1 step
steps:
# The first task is the dotnet command build, pointing to our csproj file
- task: DotNetCoreCLI#2
displayName: 'dotnet build'
inputs:
command: 'build'
arguments: '--configuration $(buildConfiguration)'
projects: 'src/Repository.sln'
# The second task is dotnet pack command again pointing to the csproj file
# The nobuild means the project will not be compiled before running pack, because its already built in above step
- task: DotNetCoreCLI#2
displayName: "dotnet pack"
inputs:
command: 'pack'
arguments: '--configuration $(buildConfiguration)'
packagesToPack: 'src/Common.Core/Common.Core.csproj'
nobuild: true
includeSymbols: true
versioningScheme: 'byBuildNumber'
# The last task is a nuget command, nuget push
# This will push any .nupkg files to the 'Nuget' artifact feed
# allowPackageConflicts allows us to build the same version and not throw an error when trying to push
# instead it just ingores the latest package unless the version changes
- task: NuGetCommand#2
displayName: 'nuget push'
inputs:
command: 'push'
feedsToUse: 'select'
packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
nuGetFeedType: 'internal'
publishVstsFeed: 'MyNuget'
allowPackageConflicts: true
I would expect it generates nuget with right version number without failing on nuget pack step.
When you choose the NuGet package version will be like the build number you can't give this value $(Build.DefinitionName)-$(date:yyyyMMdd)$(rev:.r) to the build number.
It should be in this format:
$(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r).
You can see this info in the classic editor:
More info you can find in the docs:
For byPrereleaseNumber, the version will be set to whatever you choose for major, minor, and patch, plus the date and time in the format yyyymmdd-hhmmss.
For byEnvVar, the version will be set as whatever environment variable, e.g. MyVersion (no $, just the environment variable name), you provide. Make sure the environment variable is set to a proper SemVer e.g. 1.2.3 or 1.2.3-beta1.
For byBuildNumber, the version will be set to the build number, ensure that your build number is a proper SemVer e.g. 1.0.$(Rev:r). If you select byBuildNumber, the task will extract a dotted version, 1.2.3.4 and use only that, dropping any label. To use the build number as is, you should use byEnvVar as described above, and set the environment variable to BUILD_BUILDNUMBER.

Trying to publish a build to Nuget.org

I'm new to Azure DevOps; however, what I'm doing seems like it should be straightforward: I simply want to compile a project and publish to NuGet.org. I'm hitting that many barriers to doing it that I feel that I'm probably mis-using the tool.
I have a build which looks like this:
trigger:
- master
pool:
vmImage: 'windows-2019'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration) /property:Version=$(Build.BuildNumber)
displayName: 'dotnet build $(buildConfiguration)'
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: 'drop'
This successfully builds. However, I do get the following warning:
##[warning]Directory 'd:\a\1\a' is empty. Nothing will be added to build artifact 'drop'.
When I try to deploy, I'm getting errors - the latest of which is:
No files matched the search pattern.
In dotnet pack, here's the release step for pack:
steps:
- task: DotNetCoreCLI#2
displayName: 'dotnet pack'
inputs:
command: pack
packagesToPack: '$(System.DefaultWorkingDirectory)/name.project'
This is what's currently failing, but the next step is intended to publish to NuGet (for completeness - and incase there's an easier way to do all this):
steps:
- task: DotNetCoreCLI#2
displayName: 'dotnet pack'
inputs:
command: pack
packagesToPack: '$(System.DefaultWorkingDirectory)/name.project'
The release has an artifact, which I've set up as the output from the build (I think); however, I get the error from this:
No version is available for name.project or the latest version has
no artifacts to publish. Please check the source pipeline.
My understanding of how this should work in general is that the Build should hold the steps involved in producing the binaries, etc, whereas the Release should be any steps involved in deploying the built. So, I should be able to take a 'Build' and 'Release' it several times to multiple locations. I feel like this understanding is not in-keeping with the errors that I'm seeing.
Is my understanding correct?
What could I be doing wrong here and, more importantly, what are the methods of diagnosing issues with this?
Trying to publish a build to Nuget.org
For the first error, that because you are mising the Copy Files task before using the Publish Build Artifacts task.
Check the Publish Build Artifacts task, you can view this task is used to publish build artifacts to Azure Pipelines, TFS, or a file share.
But after build the project/solution, the output are stored on the build agent, rather than the artifacts. So we need to add a copy task before PublishBuildArtifacts to copy the files from output to the artifacts:
steps:
- task: CopyFiles#2
displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
inputs:
SourceFolder: '$(system.defaultworkingdirectory)'
TargetFolder: '$(build.artifactstagingdirectory)'
enabled: false
For the second error, you should specify the project in the repos instead of System.DefaultWorkingDirectory, where is use to build the project, change the Path to csproj in the repos:
steps:
- task: DotNetCoreCLI#2
displayName: 'dotnet pack'
inputs:
command: pack
packagesToPack: NetCoreDemo/NetCoreDemo/NetCoreDemo.csproj
Hope this helps.
You man need to provide the --output argument for dotnet build. Add the following
--output $(Build.ArtifactStagingDirectory)
so the command should look something like this.
dotnet build --configuration $(buildConfiguration) /property:Version=$(Build.BuildNumber) --output $(Build.ArtifactStagingDirectory)
this will put the output into the Artifact Staging Directory (d:\a\1\a)
which will include the *.nupkg file.
I am assuming you are using the new CSProj format and you are supplying the properties for the NuGet package and have the <GeneratePackageOnBuild>true</GeneratePackageOnBuild> property already set. If so the *.nupkg file should be in the output directory that you define in the dotnet build command. Then you don't need to use dotnet pack. In the release definition you just need to use the NuGet task and use push command to push the package to your NuGet Feed.
Use this blog post as reference. It does not have the full details since its focusing only on few aspects of the entire process. but it should be helpful to get the build aspect correct.

Publish package in Azure Devops Artifacts using Azure Pipelines

I am using Azure Pipelines from a Github Repository.
I was able to build a Dotnet Core class library using azure-pipelines.yml:
trigger:
- master
pool:
vmImage: 'Ubuntu-16.04'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'build'
What to add to this script to publish a package to Azure Devops Artifacts after build?
And how to set each version release? From GitHub tag?
Here is the documentation that gives a good set of yaml examples.
The bit you're going to zone in on is about pushing to an artifact feed.
steps:
- task: NuGetCommand#2
displayName: 'NuGet push'
inputs:
command: push
publishVstsFeed: '<feedName>'
allowPackageConflicts: true
This section is about versioning your packages.
variables:
Major: '1'
Minor: '0'
Patch: '0'
steps:
- task: NuGetCommand#2
inputs:
command: pack
versioningScheme: byPrereleaseNumber
majorVersion: '$(Major)'
minorVersion: '$(Minor)'
patchVersion: '$(Patch)'
We have done something similar to what is described in this versioning section to allow developers to increment the Major and Minor numbers and the Patch and Build numbers are derived from the build number ex. 1.2.1902.127. This tells us that this package is a member of 1.x family on feature 2 as the seventh revision of the build on Feb 12 of 2019.
We also want our assemblies versioned thus, so we have a couple powershell scripts that write the .csproj files with the appropriate property values. For .Net Framework projects we use MSBuild -t:Pack to leverage these properties for package versioning and for .Net Standard & Core we use the dotnet pack task.
note: NuGet.exe has a bug with package reference syntax for .Net Framework where the dependency tree isn't populated, so we use MSBuild to package those projects