简体   繁体   中英

Azure DevOps Pipeline variables define automatically change before build

I have an Azure DevOps build pipeline with some pipeline variables.

I would like to change it, one of the variables automatically with powershell script, right before queueing. Is it possible?

Or is there any other way to work with that?

Yes, it's possible : https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=classic%2Cpowershell

The variable have to be declared in the pipeline and you can set a value with output (from cmd, powerhsell, python, exe, ...) :

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

In PowerShell, you can use Write-Host :

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

"20.2.35". 20.2 part should be updated manually once year, but .35 should be updated every week, this is week number

I am afraid that there is no variable that can achieve weekly increments. But you can use scheduled triggers and counter variable to achieve similar functions.

Here are the steps:

  1. Create a Variable Group in Pipeline -> Library .

在此处输入图片说明

  1. Create two Pipelines. Pipeline 1 use the Counter to update the variable in variable group(every week). Pipeline 2 set the variable as the Buildnumber.

Example:

Pipeline 1 Use powershell task to run the Rest API to update the variable.

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

Pipeline 2. Use the Variable to set the 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'

Explaination:

Pipeline1 will run once a week to update the variable value. And the value will be incremented by 1. Pipeline2 uses this variable to set the build number. This value has been set before the pipeline runs, you can use $(build.buildnumber) to reference it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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