简体   繁体   English

有条件的buildnumber以发布预发布nuget包?

[英]Conditional buildnumber in order to publish prerelease nuget package?

I'm automating the deployment of a nuget package. 我正在自动部署nuget包。
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. 当构建由master分支触发时,我想发布一个常规的nuget包。
When the build is triggered by test branch, I want to publish a prelease package. 当构建由测试分支触发时,我想发布一个prelease包。
In order to do this, I need to add -beta to the nuget package version number. 为此,我需要将-beta添加到nuget包版本号。 So I changed the Automatic package versioning from use build number to use an environment variable . 所以我将Automatic package versioninguse build number更改为use an environment variable
See following img : 见以下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 $() . 起初它抱怨变量说No value was found for the provided environment variable ,但我通过删除$()解决了这个问题。 But now I'm still stuck because I can't find a way to set the variable with Build.BuilderNumber and -beta . 但现在我仍然卡住,因为我找不到使用Build.BuilderNumber-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. 所以现在我正在考虑将设置更改回use build number并更改在构建的选项卡options中设置内部版本号的表达式。 However, even there I'm unable to have a conditional expression on Build.SourceBranch to add or not the -beta . 但是,即使在那里我也无法在Build.SourceBranch上添加条件表达式来添加或不添加-beta
Chris McKenzie seem to have had a similar problem here and resolved it with powershell. Chris McKenzie似乎在这里遇到了类似的问题并用powershell解决了这个问题。 Unfortunately, I don't know powershell and couldn't understand because the script seem way too big for what I need. 不幸的是,我不知道powershell并且无法理解,因为脚本似乎对我需要的东西来说太大了。 Still I believe their my by hope with powershell. 仍然我相信他们是我的希望与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? 我在哪里以及如何在源分支上添加条件以将-beta附加到BuildNumber并用作nuget包版本?

You can achieve it by adding a PowerShell task to change the variable $(PackageVersion) conditional based on different branches. 您可以通过添加PowerShell任务来实现它,以根据不同的分支更改变量$(PackageVersion)条件。

Before NuGet pack task, add a PowerShell task with the script as below: 在NuGet打包任务之前,使用脚本添加PowerShell任务 ,如下所示:

$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 ; 现在在NuGet pack $(Build.BuildNumber)如果分支是master ,则包版本将是$(Build.BuildNumber) ; and the package version will be $(Build.BuildNumber)-beta if the branch is test . 如果分支是test ,则包版本将是$(Build.BuildNumber)-beta

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM