繁体   English   中英

Azure DevOps Pipeline 变量定义在构建前自动更改

[英]Azure DevOps Pipeline variables define automatically change before build

我有一个带有一些管道变量的 Azure DevOps 构建管道。

我想在排队之前用powershell脚本自动更改它,其中一个变量。 是否可以?

或者有没有其他方法可以解决这个问题?

是的,这是可能的: https : //docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=classic%2Cpowershell

该变量必须在管道中声明,您可以设置一个带有输出的值(来自 cmd、powerhsell、python、exe、...):

##vso[task.setvariable variable=MyVariable;]NewValue

在 PowerShell 中,您可以使用Write-Host

Write-Host "##vso[task.setvariable variable=MyVariable;]NewValue"

“20.2.35”。 20.2 部分应该每年手动更新一次,但是 .35 应该每周更新一次,这是周数

恐怕没有任何变量可以实现每周增量。 但是你可以使用调度触发器计数器变量来实现类似的功能。

以下是步骤:

  1. Pipeline -> Library创建一个变量组。

在此处输入图片说明

  1. 创建两个管道。 管道 1 使用 Counter 更新变量组中的变量(每周)。 管道 2 将变量设置为 Buildnumber。

例子:

管道 1 使用 powershell 任务运行Rest API来更新变量。

trigger:
- none

schedules:
  - cron: "0 12 * * 0"
    displayName: Weekly Sunday build
    branches:
       include:
         - master

variables:
  - name: version
    value: $[counter('',36)]

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $token = "PAT"
      
      $url="https://dev.azure.com/{Org name}/_apis/distributedtask/variablegroups/{Group ID}?api-version=6.1-preview.2"
      
      $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
      
      $JSON = @'
      {
         "variables":{"{variable name}":{"value":"$(version)"}},"id":{Group ID},"type":"Vsts","name":"{Variable Group name}","variableGroupProjectReferences":[{"projectReference":{"id":"{Project ID}","name":"{Project Name}"},"name":"{Variable Group Name}"}]
      }
      '@
      
      $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method PUT -Body $JSON -ContentType application/json

管道 2. 使用变量设置 Buildnumber。

name: 20.2 $(123)

trigger:
- none

variables:
  - group: test

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'

说明:

Pipeline1 将每周运行一次以更新变量值。 并且该值将增加 1。 Pipeline2 使用此变量来设置构建号。 这个值在管道运行之前就已经设置好了,你可以使用$(build.buildnumber)来引用它。

暂无
暂无

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

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