简体   繁体   English

从 csproj 获取 Nuget package 步骤中 Azure DevOps 中的版本号

[英]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.

相关问题 Azure DevOps NuGet Pack 忽略 csproj 包详细信息 - Azure DevOps NuGet Pack is ignoring csproj package details Azure DevOps Nuget 管道步骤中的 Nuget 包描述 - Nuget package description in the Azure DevOps Nuget pipeline step Azure DevOps Nuget 工件提要忽略包版本号的最后一位数字(如果为零) - Azure DevOps Nuget artifact feed ignoring last digit of the package version number if it is zero Azure 上 nuget 还原步骤中来自特定包源的 Nuget 包 - Nuget package from specific package source on nuget restore step on Azure VSTS/Azure DevOps:包上的自动增量 NuGet 包版本 - VSTS/Azure DevOps: Auto-Increment NuGet Package Version on Pack 将 Nuget 包推送到 Azure DevOps - Push Nuget Package to Azure DevOps Azure DevOps:无法从 Azure Pipeline 中的提要加载 nuget 包 - Azure DevOps: Can not load nuget package from feed in Azure Pipeline 确保 azure 管道构建的 dll 和 nuget 包包含与 csproj 版本相同的信息 - Ensuring azure pipeline built dll and nuget package contain same info as csproj version 从 azure devops 中的包中读取发布版本 - Read release version from package in azure devops 从构建或发布管道在 azure devops 中发布 nuget 包 - Publish nuget package in azure devops from build or release pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM