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.3k views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/65888848/trying-to-use-set-variable-from-predefined-variables-and-use-it-in-condition-for

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

1 Answer

0 votes
by (71.8m points)

trying to use set variable from predefined variables and use it in condition for stage in azure devops pipeline

You could use Output variables from previous stages:

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

Please check the document Jobs can access output variables from previous stages for some more details.

And we need also add a property ;isOutput=true and name to the previous stages:

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

Then we could use the Dependencies.stageName.outputs['jobName.stepName.variableName'] as condition in next stage:

- 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*.')))

Note: Since you need add the condition on top of all stage, we need update the stages refer from stageDependencies.stageName.jobName.outputs['stepName.variableName'] To Dependencies.stageName.outputs['jobName.stepName.variableName']

Update2:

here i am setting variable(variable.yaml) under deployment not job. so how would i frame below one Dependencies.stageNam.outputs['jobName.stepName.variableName'] should i meniton Deployment as jobName ??

The answer is yes.

You could use the Dependencies.stageName.outputs['jobName.jobName.stepName.variableName'] as condition.

Note: There are two jobName in the conditions.


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

...