3

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 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

11 Comments

Actually the same for me. I tested this and got the same. @dotnetdev Are you sure you have release triggered for latest build?
Thank you, I am going to rework some things and I will report back. As for the BuildDirectory variable, sorry, I should have explained that. It is another custom variable that just points to the directory where the folder is created. Ex.: C:/Users/Public. I will also add that for the 'copy files' task, I don't use a PowerShell script. I clicked 'Add task', and selected the 'Copy Files' task. I then just choose the source folder and target folder for the transfer (that is working OK).
@KrzysztofMadej I just made my script look identical to yours, and my build finished with the build number being master.7 (which is correct). But the MyBuild variable in the release pipeline was updated to master.6. The output of my script says the variable was set to master.6, and I checked the created folder and it is master.6 as well... I have no idea where this is going wrong.
@KrzysztofMadej Yes, I ensured that Default Version is set to Latest by going to Pipelines -> Pipelines -> {my pipeline} -> Edit -> {my pipeline artifact} -> Default Version.
@dotnetdev Pipelines -> Pipelines -> {my pipeline} -> Edit -> {my pipeline artifact} -> Default Version I am confused about this step. If you are not using yaml, you should modify the default version in Pipeline-> Release-> Artifacts
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.