简体   繁体   English

Devops YAML - 使用表达式设置构建名称

[英]Devops YAML - set build name using expression

In a yaml build, is it possible to set the build name using an expression;在 yaml 构建中,是否可以使用表达式设置构建名称; it would be advantageous if I could match the release pipeline with the actual build id.如果我可以将发布管道与实际构建 ID 匹配,那将是有利的。

Example:例子:

trigger:  
- master

variables:  
    major: 2  
    minor: 3  
    offset: 2000  
    bid: $[$build.BuildID -as [int] + $offset]

name: "$(major).$(minor).$(bid)"

You can use the UpdateBuildNumber command to dynamically set the name as part of a bash or PowerShell script.您可以使用UpdateBuildNumber 命令将名称动态设置为 bash 或 PowerShell 脚本的一部分。

For more details you can see this blog post , but the gist of it is something like this:有关更多详细信息,您可以查看此博客文章,但其要点如下:

name: 'Set dynamically below in a task'

variables:  
    major: 2  
    minor: 3  
    offset: 2000

steps:
- task: PowerShell@2
  displayName: Set the name of the build (i.e. the Build.BuildNumber)
  inputs:
    targetType: 'inline'
    script: |
      [int] $buildIdWithOffset = ([int] $(Build.BuildId)) + ([int] $(offset))
      [string] $buildName = "$(major).$(minor).$buildIdWithOffset"
      Write-Host "Setting the name of the build to '$buildName'."
      Write-Host "##vso[build.updatebuildnumber]$buildName"

There's yet another way to set the build number without using the scripts.还有另一种方法可以在不使用脚本的情况下设置内部版本号。 The advantage of this method is that the build name is set from the beginning, unlike the script that changes the original name during the build.这种方法的优点是构建名称是从头开始设置的,不像脚本在构建过程中更改了原始名称。 And the sequence number generation is cleaner (see the $(Rev:r) piece that works only in the "name" property of the pipeline.并且序列号生成更清晰(请参阅 $(Rev:r) 仅在管道的“名称”属性中起作用的部分。

You must use the "format" expression instead of ($Variable) syntax, because the build name is created during the Compile stage when the $(Variable) syntax does not work yet.您必须使用“格式”表达式而不是 ($Variable) 语法,因为构建名称是在编译阶段创建的,此时 $(Variable) 语法还不起作用。 Otherwise the variables will not be evaluated and will go into the build name as $(Variable).否则变量将不会被评估,并将作为 $(Variable) 进入构建名称。

name: '$(buildNumber).$(Rev:r)'
variables:
  majorVersion: '1'
  minorVersion: '0'

  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/master') }}:
    buildNumber: ${{ format('{0}.{1}',  variables['majorVersion'], variables['minorVersion']) }}
  ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
    buildNumber: ${{ format('{0}.{1}.PR-{0}', variables['majorVersion'], variables['minorVersion'], variables['System.PullRequest.PullRequestId']) }}

More about expressions here: Azure Pipelines Reference有关此处的表达式的更多信息: Azure Pipelines 参考

The example won't work for syntax error.该示例不适用于语法错误。

You can check the MSDN doc Expressions and there's no mathematical operations like summation provided.您可以查看 MSDN doc Expressions并且没有提供求和之类的数学运算。

A better work around would be quiting the offset:更好的解决方法是退出偏移量:

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  major: 2  
  minor: 3  
  #offset: 2000  

name: $(major).$(minor).$(build.BuildID)

This will return you the name as 2.3.101 for example.例如,这将返回名称为2.3.101

If you want the use the generated expression to automatically increment the counter function (which can only be used as part of a variable declaration) for each unique value, you can use the following:如果您希望使用生成的表达式为每个唯一值自动递增counter function(它只能用作变量声明的一部分),您可以使用以下内容:

jobs:
- job: VERSION_BUILD
  displayName: Version Build
  variables:
    - name: pipeline_start_date
      value: $[format('{0:yyyy}-{0:MM}-{0:dd}', pipeline.startTime)]
  steps:
  - checkout: none
  - task: PowerShell@2
    name: generate_minor_version
    displayName: Generate Minor Version
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "Pipeline start date is '$(pipeline_start_date)'."
        [int] $minorVersion = [math]::floor(((New-TimeSpan -Start '2022-02-01' -End '$(pipeline_start_date)').Days) / 14) + 1
        Write-Host "Minor version is '$minorVersion'."
        Write-Host "##vso[task.setvariable variable=minor_version;isOutput=true;]$minorVersion"
- job: VERSION_BUILD_2
  displayName: Version Build 2
  dependsOn:
  - VERSION_BUILD
  variables:
    - name: minor_version
      value: $[dependencies.VERSION_BUILD.outputs['generate_minor_version.minor_version']]
    - name: major_minor_version
      value: $[format('{0}.{1}', variables['major_version'], variables['minor_version'])]
    - name: build_version
      value: $[counter(variables['major_minor_version'], 0)]
  steps:
  - checkout: none
  - task: PowerShell@2
    displayName: Update Build Number
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "Major-minor version is '$(major_minor_version)'."
        Write-Host "Build version is '$(build_version)'."
        [string] $buildNumber = "$(major_version).$(minor_version).$(build_version)"
        Write-Host "Setting the name of the build to '$buildNumber'."
        Write-Host "##vso[build.updatebuildnumber]$buildNumber"

This particular script calculates the $minor_version as the number of 2 week intervals (iterations) from a given point in time.这个特定的脚本将$minor_version计算为从给定时间点开始的 2 周间隔(迭代)的数量。 So in theory, on every other Tuesday, the pipeline will increment the minor version of the build number by 1 and reset the build version of the build number to 0, which will then automatically increment for each subsequent build in that two-week period.因此理论上,在每隔一个星期二,管道会将内部版本号的次要版本增加 1,并将内部版本号的内部版本重置为 0,然后在这两周内为每个后续内部版本自动递增。

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

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