azure - 需要替换 azure devops 管道中预定义变量的别名值

标签 azure azure-devops azure-pipelines

我已在两个管道之间启用管道资源触发器。希望通过触发管道资源动态替换别名值。下面是管道代码

  resources:        
  pipelines:
  - pipeline: pipeline1
    project: onecom
    source: pipeline1-api
    trigger: 
      branches:
      - develop    
      - feat/*
  - pipeline: pipeline2
    project: onecom
    source: pipeline2-api 
    trigger:
      branches:
      - develop
      - feat
  variables:
  - name: apiname
    value:  $(resources.pipeline.<Alias>.pipelineName)
  - name: dockertag
    value: $(resources.pipeline.<Alias>.sourceCommit)
  - name: runname
    value: $(resources.pipeline.<Alias>.runName)

stages:
- stage: ScanImage

  jobs:
  - job: ScanImage

    pool:
    vmImage: 'ubuntu-16.04'

    steps:
    - script: echo $(apiname)
    - script: echo $(runname)

如果构建来自 ,我想将 $(resources.pipeline..pipelineName) 中的 Alias 值替换为值 pipeline1>来源:pipeline1-api,如果构建来自动态来源:pipeline2-api,则使用pipeline2

最佳答案

I would like to replace Alias value in $(resources.pipeline..pipelineName) with value pipeline1 if build comes from source: pipeline1-api and with pipeline2 if build comes from source: pipeline2-api dynamically.

由于构建/发布管道尚不支持嵌套变量的值(如$(resources.pipeline.$(alias).pipelineName)))。因此我们无法直接在变量中使用它:

  variables:
  - name: apiname
    value:  $(resources.pipeline.$(alias).pipelineName)

要解决此问题,我们需要添加内联 powershell 来设置变量 resources.pipeline.<Alias>.pipelineName基于 $(resources.triggeringAlias) 的值:

variables:
  - name: alias
    value: $(resources.triggeringAlias)


    - task: InlinePowershell@1
      inputs:
        script: |
          if ("$(alias)" -eq "PipelineA")
          {
            Write-Host ("##vso[task.setvariable variable=dockertag]$(resources.pipeline.PipelineA.sourceCommit) | cut -c -7")
          }
          elseif ("$(alias)" -eq "PipelineB")
          {
            Write-Host ("##vso[task.setvariable variable=dockertag]$(resources.pipeline.PipelineB.sourceCommit) | cut -c -7")
          }

更新:

could you please help me same config in bash as we are using these task in linux machines

- task: PowerShell@2
  displayName: 'Inline Powershell'
  inputs:
    TargetType: inline
    Script: |
      if ("$(alias)" -eq "PipelineA")
      {
        Write-Host ("##vso[task.setvariable variable=dockertag]$(resources.pipeline.PipelineA.sourceCommit) | cut -c -7")
      }
      elseif ("$(alias)" -eq "PipelineB")
      {
        Write-Host ("##vso[task.setvariable variable=dockertag]$(resources.pipeline.PipelineB.sourceCommit) | cut -c -7")
      }
    pwsh: true

关于azure - 需要替换 azure devops 管道中预定义变量的别名值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65598404/

相关文章:

azure - 使用 New-AzSqlDatabaseCopy 时“指定的 sku 'ElasticPool' 无效”

build - 如何对 msbuild 内置目标(如构建/重建)设置条件?

azure 管道cron未运行

azure - 从 Azure DevOps 池中删除代理

tfs - 在具有多个阶段的 VSTS 构建中跳过获取源和/或清理步骤

Azure 虚拟机 VMRole SLA

azure - Azure 事件中心中的客户端与发布者

azure - 将 Windows Azure 网站与 Windows Azure 虚拟机连接

azure-devops - Azure DevOps - 网络防火墙后面的部署池本地代理

awk 无法选择具有空值的列