Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

need replace alias value of predefined variables in azure devops pipelines

I have enabled pipeline resource triggers between two pipelines. would like to replace alias value dynamically with triggering pipeline resource. below is the pipeline code

  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)

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.

question from:https://stackoverflow.com/questions/65598404/need-replace-alias-value-of-predefined-variables-in-azure-devops-pipelines

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Since the value of nested variables (like $(resources.pipeline.$(alias).pipelineName)) are not yet supported in the build/release pipelines. So we could not use it in the variable directly:

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

To resolve this issue, we need add a inline powershell to set the variable resources.pipeline.<Alias>.pipelineName based on the value of the $(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")
          }

Update:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...