简体   繁体   English

如何使用VSTS构建过程自动增加nuget软件包的版本?

[英]How do I auto-increment nuget package version using VSTS build process?

In this video from MSDN at the 3:34 second mark, the presenter shows how to append the Build ID to a nuget's version. 在MSDN的第二点3:34的视频中 ,演示者演示了如何将Build ID附加到nuget的版本。 In the MSBuild arguments, he specifies: 他在MSBuild参数中指定:

/t:pack /p:PackageVersion=1.0.$(Build.BuildId)

So, when the project is built by VSTS, the nuget assembly's revision number is using the build id. 因此,当项目由VSTS生成时,nuget程序集的修订版号将使用生成ID。

I would like to do something similar. 我想做类似的事情。 Instead of hard coding the 1.0 in the build definition, I'd like to retrieve that from .csproj file. 与其在构建定义中对1.0进行硬编码,我想从.csproj文件中检索它。 I am using the new .csproj file which stores nuget information. 我正在使用新的.csproj文件来存储nuget信息。

For example, I'd like to specify in the csproj: 例如,我想在csproj中指定:

<Version>0.0.1-beta</Version>

Then, VSTS would append the BuildID and generate the assembly version as 0.0.0.1-beta.49 (49 being the build id) 然后,VSTS将附加BuildID并生成程序集版本为0.0.0.1-beta.49 (49是构建ID)

I ended up doing the opposite of what Shayki Abramczyk suggested. 我最终做了与Shayki Abramczyk建议相反的事情。

I use a task called "Variables Task Pack". 我使用了一个名为“变量任务包”的任务。 It can be found here (and is free at the time of this answer): https://marketplace.visualstudio.com/items?itemName=YodLabs.VariableTasks#qna 可以在这里找到(并且在获得此答案时是免费的): https : //marketplace.visualstudio.com/items?itemName=YodLabs.VariableTasks#qna

Using this task, I set two variable: $(BuildId) and $(ReleaseType). 使用此任务,我设置了两个变量:$(BuildId)和$(ReleaseType)。 See the settings snapshots at the end of the answer. 请参阅答案末尾的设置快照。

Then, in my CSPROJ project file, I modified the nuget version to use the two environment variables. 然后,在我的CSPROJ项目文件中,我修改了nuget版本以使用两个环境变量。 Here's a clip of the project file: 这是项目文件的片段:

<PropertyGroup>
     <Version>0.0.0.0$(BuildId)$(ReleaseType)</Version>
     <FileVersion>0.0.0.0$(BuildId)$(ReleaseType)</FileVersion>
    ...
</PropertyGroup>

IMPORTANT: Notice the extra 0 in front of $(BuildId). 重要说明:请注意$(BuildId)前面的多余0。 I had to add that in order to build locally. 我必须添加它才能在本地构建。 Without it, the build failed with an incorrect version format error. 没有它,构建会因错误的版本格式错误而失败。

Now, after building, I get the buildid as my revision number and release type appended. 现在,在构建之后,我获得了buildid作为附加的修订版号和发布类型。

Here are the screen shots showing configuration of both variables. 这是显示两个变量的配置的屏幕截图。

BuildId变量的设置 ReleaseType变量的设置

You can create a Power Shell script that retrieves the version from csproj file, then add the version to a new environment variable with this command: Set-VstsTaskVariable 您可以创建一个Power Shell脚本来从csproj文件中检索版本,然后使用以下命令将版本添加到新的环境变量中: Set-VstsTaskVariable

For Example: 例如:

$csprojId = $retrivedIdfromTheFile
Set-VstsTaskVariable -Name "CSPROJ_ID" -Value $csprojId

Now you can use the CSPROJ_ID variable on the MSBuild arguments: 现在,您可以在MSBuild参数上使用CSPROJ_ID变量:

/p:PackageVersion=$(CSPROJ_ID).$(Build.BuildId)

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

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