首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有一种方法可以在阶段级别上定义变量组?如果是的话,如何在工作级别访问它?

是否有一种方法可以在阶段级别上定义变量组?如果是的话,如何在工作级别访问它?
EN

Stack Overflow用户
提问于 2022-02-17 13:07:31
回答 2查看 881关注 0票数 1

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

代码语言:javascript
复制
# 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文件。

代码语言:javascript
复制
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-管线.Col(行: 49,Col: 13):“previouscommitid”参数不是有效字符串。

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

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-18 19:27:52

谢谢你的回应。我已经在我的库中设置了变量组。我只是不能用它。

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

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

代码语言:javascript
复制
stagevariables.yaml 

variables:
- group: Cloudops

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

Stack Overflow用户

发布于 2022-02-18 09:59:07

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

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

variables密钥下,您可以:

使用指定的values.

  • Override定义新变量,使用新values.

  • Reference定义现有变量,从现有变量组和变量模板定义变量。

因此,如果要使用包含管道中某些变量的变量组,应在管道> Library页面上手动定义变量组,然后在管道中引用变量组。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71158737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档