簡體   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