有没有办法在阶段级别定义变量组?如果是这样,如何在工作级别访问它?

Is there a way to have a variable group defined at stage level? If so how to access it at Job Level?

我正在尝试找到一种方法来在阶段级别定义变量组,然后通过模板在下面的作业中访问它?我该怎么做?

# Template file getchangedfilesandvariables.yaml
parameters:
  - name: "previouscommitid"
    type: string

    
steps:
  - task: PowerShell@2
    displayName: 'Get the changed files'
    name: CommitIds
    inputs:
      targetType: 'filePath'
      filePath: '$(Build.SourcesDirectory)\AzureDevOpsPipelines\Get-COChangedfiles.ps1'
      arguments: >
        -old_commit_id ${{ previouscommitid }}

  - task: PowerShell@2
    name: PassOutput
    displayName: 'Getting Variables for Packaging'
    inputs:
        targetType: 'filepath'
        filepath: '$(System.DefaultWorkingDirectory)\AzureDevOpsPipelines\Get-COADOvariables.ps1'

下面是我的 yaml 文件。

trigger: none
name: $(BuildID)

variables:
  
  system.debug: true
  CodeSigningCertThumbprint: "somethumbprint"
  # Triggering builds on a branch itself.
  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/') }}:
    branchName: $[ replace(variables['Build.SourceBranch'], 'refs/heads/', '') ]
  # Triggering builds from a Pull Request.
  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/pull/') }}:
    branchName: $[ replace(variables['System.PullRequest.SourceBranch'], 'refs/heads/', '') ]

## it will create pipeline package and it will push it private or public feed artifacts
stages:
  - stage: Stage1
    variables:
    - group: Cloudops
    - name: oldcommitid
      value: $[variables.lastcommitid]

    jobs:
    - job: IdentifyChangedFilesAndGetADOVariables
      

      pool:
        name: OnPrem

      workspace:
        clean: all # Ensure the agent's directories are wiped clean before building.

      steps:
      - powershell: |
          [System.Version]$PlatformVersion = ((Get-Content "$(Build.SourcesDirectory)\AzureDevOpsPipelines\PlatformVersion.json") | ConvertFrom-Json).PlatformVersion
          Write-Output "The repository's PlatformVersion is: $($PlatformVersion.ToString())"
          $NewPackageVersion = New-Object -TypeName "System.Version" -ArgumentList @($PlatformVersion.Major, $PlatformVersion.Minor, $(Build.BuildId))

          Write-Output "This run's package version is $($NewPackageVersion.ToString())"
          echo "##vso[task.setvariable variable=NewPackageVersion]$($NewPackageVersion.ToString())"
          echo "##vso[task.setvariable variable=commitidold;isOutput=true]$(oldcommitid)" 
        displayName: 'Define package version.'
        name: commitidorpackageversion
        errorActionPreference: stop

      - template: getchangedfilesandvariables.yaml
        parameters: 
          previouscommitid:
            - $(commitidorpackageversion.commitidold)
           # - $(oldcommitid)

我在代码的倒数第二行收到错误

/AzureDevOpsPipelines/azure-pipelines.yml(第 49 行,第 13 列):'previouscommitid' 参数不是有效字符串。

我尝试了不同的组合,但仍然出现错误。

有什么想法吗?

在 YAML 管道中,您不能在 variables 键下定义新的变量组。

实际上,当运行 YAML 管道时,我们没有可用的语法来创建新的变量组。

variables键下,您可以:

  • 定义具有指定值的新变量。
  • 用新值覆盖现有变量。
  • 引用现有变量组和变量模板中的变量。

因此,如果您想在管道中使用带有某些变量的变量组,您应该在 Pipelines > Library 页面,然后在管道中引用它。

感谢您的回复。我的库中已经设置了变量组。我只是无法使用它。

为了实现这一点,我创建了另一个模板文件并将其提供给舞台下的变量部分。这样做之后,我实际上能够在连续的工作中使用我的变量组中的变量。

有关更多信息,您可以查看此文档:https://docs.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml

stagevariables.yaml 

variables:
- group: Cloudops

azure-pipelines.yml
    stages:
      - stage: Stage1
        variables:
          - template: stagevariables.yaml
        jobs:
        - job: CheckwhichfeedsAreAvailable