[英]Fetch version number in Nuget package step in Azure DevOps from csproj
Trying to set up a build pipeline where nugets built from pull-requests are suffixed with '-unstable'.尝试建立一个构建管道,其中从拉取请求构建的 nuget 后缀为“-unstable”。 However, I can't seem to get the version of the class-library project that is being built defined in its csproj file.但是,我似乎无法获得在其 csproj 文件中定义的正在构建的类库项目的版本。 I've tried every keyword I could think of and I've been unsuccessful in finding documentation or others with the same issue so far.我已经尝试了我能想到的每一个关键字,但到目前为止,我一直未能找到具有相同问题的文档或其他关键字。
Any help would be appreciated.任何帮助,将不胜感激。
Below are excerpts from relevant files:以下是相关文件的摘录:
class-library.csproj类库.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<Version>1.1.0.0</Version>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
</Project>
ci-file.yaml ci-file.yaml
variables:
- group: BuildParameters
- name: BuildVersion
${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
value: '$(Version)'
${{ else }}:
value: '$(Version).$(Build.BuildId)-unstable'
steps:
- template: template.yaml
template.yaml模板.yaml
- task: NuGetCommand@2
displayName: 'NuGet Pack $(...)'
inputs:
command: pack
packagesToPack: '$(...)'
versioningScheme: byEnvVar
versionEnvVar: BuildVersion
Based on your description, you need to get the version value in the csproj file.根据您的描述,您需要在 csproj 文件中获取版本值。
The version number in the csproj file cannot be read directly using variables. csproj文件中的版本号不能直接使用变量读取。
To meet your requirements, you need to add a task to read the version value in csproj file and set the version as pipeline variable.为了满足您的要求,您需要添加一个任务来读取 csproj 文件中的版本值并将版本设置为管道变量。 Then you can use the version number in the Nuget task.然后您可以使用 Nuget 任务中的版本号。
Here is an example: PowerShell script这是一个示例:PowerShell 脚本
steps:
- powershell: |
$xml = [Xml] (Get-Content .\xxx.csproj)
$version =$xml.Project.PropertyGroup.Version
echo "$version"
echo "$(Build.SourceBranchName)"
if( "$(Build.SourceBranchName)" -eq "master") {
echo "##vso[task.setvariable variable=version]$version "
}else {
$Newversion = "$version.$(Build.BuildId)-unstable"
echo "##vso[task.setvariable variable=version]$Newversion"
}
displayName: 'PowerShell Script'
Then you can use the variable $(version)
in the next tasks.然后您可以在接下来的任务中使用变量$(version)
。
For more info, you can refer to this doc about set variables in Pipeline .有关更多信息,您可以参考有关在 Pipeline 中设置变量的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.