Reputation: 2472
I'm automating the deployment of a nuget package.
It work fine except that I'm unable to set prelease package the way I want to.
There are two branchs triggering the build.
When the build is triggered by master branch, I want to publish a regular nuget package.
When the build is triggered by test branch, I want to publish a prelease package.
In order to do this, I need to add -beta
to the nuget package version number.
So I changed the Automatic package versioning
from use build number
to use an environment variable
.
See following img :
At first it was complaining about the variable saying No value was found for the provided environment variable
, but I got around this issue by dropping the $()
. But now I'm still stuck because I can't find a way to set the variable with Build.BuilderNumber
and -beta
. I could duplicate the build and have each build trigger for their own branch, but I would rather only have one build that can handle builds from both branchs.
So now I'm thinking about changing the setting back to use build number
and change the expression that set the build number in tab options
of the build. However, even there I'm unable to have a conditional expression on Build.SourceBranch
to add or not the -beta
.
Chris McKenzie seem to have had a similar problem here and resolved it with powershell. Unfortunately, I don't know powershell and couldn't understand because the script seem way too big for what I need. Still I believe their my by hope with powershell. So I will be looking in that direction to implement my conditional build number.
This question is getting quite long, so I've set in bold the key element to it.
Where and how could I put a condition on the source branch to have -beta
appended to the BuildNumber and be used as the nuget package version?
Upvotes: 2
Views: 884
Reputation: 38096
You can achieve it by adding a PowerShell task to change the variable $(PackageVersion)
conditional based on different branches.
Before NuGet pack task, add a PowerShell task with the script as below:
$branch1="test"
$branch2="master"
if ("$(Build.SourceBranchName)" -eq $branch1)
{
echo "the build is on $branch1"
Write-Host "##vso[task.setvariable variable=PackageVersion]$(Build.BuildNumber)-beta"
}
elseif ("$(Build.SourceBranchName)" -eq $branch2)
{
echo "the build is on $branch2"
Write-Host "##vso[task.setvariable variable=PackageVersion]$(Build.BuildNumber)"
}
else
{
echo "it's either on $branch1 branch nor $branch2 branch"
}
Now in NuGet pack setp, the package version will be $(Build.BuildNumber)
if the branch is master
; and the package version will be $(Build.BuildNumber)-beta
if the branch is test
.
Upvotes: 6