首页 > 解决方案 > 为什么这些 Azure DevOps 预定义变量在管道构建中不起作用?

问题描述

鉴于以下 yml 片段,此脚本中使用的 Azure DevOps Pipeline预定义变量不会被“拾取”。

pool:
  vmImage: 'Ubuntu 16.04'

variables:
  buildConfiguration: 'Release'

trigger:
  branches:
    include:
    - master
  paths:
    include:
  <snipped>
pr:
  autoCancel: false
  paths:
    include:
  <snipped>

steps:
# Remove this task once .NET Core 2.2 is added to hosted agent.
- task: DotNetCoreInstaller@0
  inputs:
    packageType: 'sdk'
    version: '2.2.100'

- script: dotnet build --configuration $(buildConfiguration) -p:Version=$(Year:yyyy).$(Month).$(DayOfMonth).$(Rev:r)
  displayName: 'dotnet build'
  workingDirectory: 'src/<snipped>'

请注意我如何尝试设置 a 的 version 属性dotnet build

dotnet build --configuration $(buildConfiguration) -p:Version=$(Year:yyyy).$(Month).$(DayOfMonth).$(Rev:r)

但!当我手动定义构建名称..然后引用该变量..它有效!

name: $(Year:yyyy).$(Month).$(DayOfMonth).$(Rev:r)

  <snipped>

- script: dotnet build --configuration $(buildConfiguration) -p:Version=$(Build.BuildNumber)
  displayName: 'dotnet build'
  workingDirectory: 'src/<snipped>'

这是 Azure DevOps 的错误吗?

标签: azure-devops

解决方案


这些变量$(Year:yyyy) $(Month) $(DayOfMonth) $(Rev:r)不是常规预定义构建变量的一部分,如您所见,它们在此处的列表中不存在。

只能以内部版本号格式(name在 YAML 中)使用这些变量。

因此,正如您所做的那样,您可以在那里使用它们,并在构建期间使用变量Build.BuildNumber来获取值。


推荐阅读