How I can Push number build from Azure DevOps Pipelines to file *.csproj (project ASP.NET CORE)?

2.4k Views Asked by At

I am beginner to A and just starting to work in it

What task I must add in project ASP.NET CORE to Azure DevOps Pipelines to pass the build number from the azure devops pipeline to the project code Client.Shell.csproj:

<MinimumRequiredVersion> 4.60.0825.700 </MinimumRequiredVersion>
<ApplicationRevision> 716 </ApplicationRevision>
<ApplicationVersion> 4.60.0825.% 2a </ApplicationVersion>

https://i.stack.imgur.com/BXyHm.png

3

There are 3 best solutions below

0
On

I ended up using PowerShell task in my build pipeline to change the value of <Version> tag in the project file.

Here is the fragment of my pipeline:

name: $(Rev:r)

steps:
- taskk: PowerShell@2
  displayName: Inject build number
  inputs:
    targetType: 'inline'
    script: |
      (Get-Content MyProject.csproj) -Replace '\.\d+</Version>', '.$(Build.BuildNumber)</Version>' | Out-File MyProject.csproj

- task: DotNetCoreCLI@2
  displayName: Dotnet build
  inputs:
    command: build

...

Note the first line with name: parameter. It makes the build number be a sequentially incremented integer number instead of something made up from current date, which would be the default. And that is what goes into $(Build.BuildNumber) variable.

The project file has the following fragment in it:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <VersionPrefix>1.0.0</VersionPrefix>
    <Version>$(VersionPrefix).1</Version>
  </PropertyGroup>

...

The regular expression will replace any number after the dot and before </Version> by the actual build number.

I use <Version> tag, because I need to build a nuget. You can replace the <ApplicationRevision> tag the same way if needed. And of course, "MyProject" is just a placeholder for your actual project name.

If you need to do it in multiple projects, I would recommend keeping the version in a single .props file imported in every project and replacing in that file instead of replacing in multiple files.

0
On

You can do something like this, Replace "1.2.3" with your build parameter.

dotnet publish ./MyProject.csproj /p:Version="1.2.3" /p:InformationalVersion="1.2.3-qa"

Look at this https://github.com/dotnet/docs/issues/7568

0
On

When a pipeline runs, it usually performs get sources action first. So you just need to map the correct project path, then the pipeline will get entire project to the Agent's working folder.

To update the version in .csproj file, and to pass the build number from the azure devops pipeline, you can check Assembly Info extension, and use the variable $(Build.BuildNumber) in the version field.

This extension can update project AssemblyInfo files and .Net Core / .Net Standard project files .csproj. For detailed instructions on how to configure the extension please see the following link:

https://github.com/BMuuN/vsts-assemblyinfo-task/wiki/Versioning

If you want to update the file in repo, you still need to run git push command to push the changes to the repo.