0

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

# 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”参数不是有效的字符串。

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

有任何想法吗?

4

2 回答 2

0

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

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

在该variables键下,您可以:

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

所以,如果你想在管道中使用带有一些变量的变量组,你应该在管道>页面上手动定义变量组,然后在管道中引用它。

于 2022-02-18T09:59:07.910 回答
0

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

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

有关更多信息,您可以查看此文档: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
于 2022-02-18T19:27:52.010 回答