首页 > 解决方案 > trying to use set variable from predefined variables and use it in condition for stage in azure devops pipeline

问题描述

I am trying to setup variable from predefined variable of pipeline resources and use it in condition for stage, but i could not succeeded. below is the my pipeline

     resources:
     pipelines:
     - pipeline: pipeline1
       project: appcom
       source: pipeline-api
       trigger: 
         branches:
         - develop
         - feat/*
     - pipeline: pipeline2
       project: appcom
       source: pipeline2-api
       trigger:
         branches:
         - develop
         - feat/*
    
variables:
- name: alias
  value: $(resources.triggeringAlias)

stages:
- stage: DEV
displayName: Deploying to DEV
jobs:
- deployment: Deployment
displayName: Deploying to Dev
environment: dev
pool:
name: 'CDaaSLinux'
strategy:
runOnce:
deploy:
steps:
- template: /variables/variable.yaml
    - script: echo $(alias)

    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          if [ "$(alias)" == "pipeline1" ]; then
            echo "##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline1.pipelineName)"
            echo "##vso[task.setvariable variable=branchName]$(resources.pipeline.pipeline1.sourceBranch)"
            echo "##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline1.sourceCommit) | cut -c -7"
            echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline1-api"
          elif [ "$(alias)" = "pipeline2" ]; then
            echo "##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline2.pipelineName)"
             echo "##vso[task.setvariable variable=branchName]$(resources.pipeline.pipeline2.sourceBranch)"
            echo "##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline2.sourceCommit) | cut -c -7"
            echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline2-api"
          fi

    - script: echo $(dockertag)
    - script: echo $(helmpath)
    - script: echo $(apiname)
    - script: echo $(branchName)

but I would like to execute above task on top of all stage and set them as global variables and use those variable in condition on following stages.

needed condition would be as below

stages:
- stage: DockerBuildtask
condition: and(succeeded(), or(eq(variables['$(branchName'], 'refs/heads/develop'), eq(variables['$(branchName'], 'refs/heads/release'), eq(variables['$(branchName'], 'refs/heads/feat*.'), eq(variables['$(branchName'], 'refs/heads/bugfix*.')))

below is the variable.yaml content

steps:
- task: Bash@3
inputs:
targetType: 'inline'
script: |
if [ "$(alias)" == "pipeline1" ]; then
echo "##vso[task.setvariable variable=apibuild]$(resources.pipeline.pipeline1.pipelineName)"
echo "##vso[task.setvariable variable=branchName;isOutput=true]$(resources.pipeline.pipeline1.sourceBranch)"
echo "##vso[task.setvariable variable=dockertag]$(echo '$(resources.pipeline.pipeline1.sourceCommit)' | cut -c -7)"
echo "##vso[task.setvariable variable=apiname]pipeline1-api"

could someone help me on this issue. thanks in advance

标签: azure-devopsazure-pipelines

解决方案


尝试使用预定义变量中的设置变量,并在 azure devops 管道中的阶段条件下使用它

您可以使用先前阶段的输出变量:

Dependencies.stageName.outputs['jobName.stepName.variableName']

请查看文档作业可以访问先前阶段的输出变量以获取更多详细信息。

而且我们还需要在前面的阶段 添加一个属性;isOutput=true和:</p>name

echo "##vso[task.setvariable variable=branchName;isOutput=true]$(resources.pipeline.pipeline1.sourceBranch)"


- task: PowerShell@2
  displayName: 'Inline Powershell'
  inputs:
    TargetType: inline
    Script: |
      if ("$(alias)" -eq "PipelineA")
      {
       ...
      }
    pwsh: true
  name: powershelltest

然后我们可以Dependencies.stageName.outputs['jobName.stepName.variableName']在下一阶段使用 as 条件:

- stage: DockerBuildtask
  and(succeeded(), or(eq(dependencies.ScanImage.outputs['ScanImage.powershelltest.branchName'], 'refs/heads/develop'), eq(dependencies.ScanImage.outputs['ScanImage.powershelltest.branchName'], 'refs/heads/release'), eq(dependencies.ScanImage.outputs['ScanImage.powershelltest.branchName'], 'refs/heads/feat*.'), eq(dependencies.ScanImage.outputs['ScanImage.powershelltest.branchName'], 'refs/heads/bugfix*.')))

注意:由于您需要在所有阶段的顶部添加条件,我们需要更新从stageDependencies.stageName.jobName.outputs['stepName.variableName']To引用的阶段Dependencies.stageName.outputs['jobName.stepName.variableName']

更新2:

在这里,我在部署下设置变量(variable.yaml)而不是工作。那么我将如何在一个 Dependencies.stageNam.outputs['jobName.stepName.variableName'] 下构建框架,我应该将部署称为 jobName 吗?

答案是肯定的。

您可以使用Dependencies.stageName.outputs['jobName.jobName.stepName.variableName']as 条件。

注意:条件中有两个jobName


推荐阅读