2
votes

In my Azure Build Pipeline (classic, not YAML) I set my build number to be the branch name and then a revision number variable. This was my process for that: Pipelines -> Pipelines -> {my pipeline} -> Edit -> Options -> Build Number Format

$(SourceBranchName)$(Rev:.r)

In my testing, that works great.

Now, in my Release Pipeline, the first script I run is a PowerShell script that takes the build number, and applies it to a local variable (MyBuild) I created. The script is as follows:

Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
$buildNumber = $Env:BUILD_BUILDNUMBER
$pipeline.variables.MyBuild.value = $buildNumber

This variable is used later in the pipeline to create a folder that houses my release files.

$(BuildDirectory)/$(MyBuild)/Debug

For some reason, my variable is always one build behind. For example, if my build number is master.5, the folder that is created by my Release Pipeline is master.4. I have tried changing the order my scripts are in the pipeline, but that doesn't solve anything. It is weird because my Build Pipeline is correct (always named properly, ex. master.1, master.2, master.3, etc.) but my Release Pipeline variable is always one revision behind.

2

2 Answers

1
votes

Powershell script to update the custom build number

- powershell: |
[string]$version="$(Build.Repository.Name)_SomeCustomData_$(Build.BuildId)"
Write-Output "##vso[build.updatebuildnumber]$(Version)"
displayName: Set Build Number
0
votes

I tested it and it works well. Below is my reproduction, you can refer to:

In release pipeline :

Write-Host '##vso[task.setvariable variable=MyBuild]$(Build.BuildNumber)'

enter image description here

md $(Agent.ReleaseDirectory)/$env:MyBuild/Debug

enter image description here

Select build source as release artifact, default version select Latest, enable Continuous deployment trigger. This creates a release every time a new build is available.

Test reuslt:

enter image description here

enter image description here

enter image description here

In addition, the point I am confused about is how do you use the $(BuildDirectory) in the release pipeline? Agent.BuildDirectory:
The local path on the agent where all folders for a given build pipeline are created. This predefined variable should not be available in the release pipeline, we should use Agent.ReleaseDirectory.You can refer to predefined variable.