【问题标题】:Azure DevOps Pipeline variables define automatically change before buildAzure DevOps Pipeline 变量定义在构建之前自动更改
【发布时间】:2023-04-04 23:26:01
【问题描述】:

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

我想在排队之前使用 powershell 脚本自动更改其中一个变量。 有可能吗?

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

【问题讨论】:

    标签: c# powershell azure-devops


    【解决方案1】:

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

    恐怕没有什么变量可以做到周增量。但是你可以使用scheduled triggerscounter variable来实现类似的功能。

    步骤如下:

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

    1. 创建两个管道。管道 1 使用计数器更新变量组中的变量(每周)。管道 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)引用。

    【讨论】:

      【解决方案2】:

      是的,有可能: 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 应该每周更新一次,这是周数
      • 在您的构建定义中,您可以在选项中参数化构建号:docs.microsoft.com/en-us/azure/devops/pipelines/process/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      相关资源
      最近更新 更多