尝试使用预定义变量中的设置变量并将其用于 azure devops 管道中的阶段

时间:2021-01-25 16:41:12

标签: azure-devops azure-pipelines

我正在尝试从管道资源的预定义变量中设置变量并将其用于阶段,但我无法成功。下面是我的管道

     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)

但我想在所有阶段的顶部执行上述任务并将它们设置为全局变量并在以下阶段使用这些变量。

所需条件如下

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

下面是variable.yaml的内容

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"

有人可以帮助我解决这个问题。提前致谢

1 个答案:

答案 0 :(得分:0)

<块引用>

尝试使用预定义变量中的设置变量,并将其用于 azure devops 管道中的阶段

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

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

有关更多详细信息,请查看文档 Jobs can access output variables from previous stages

而且我们还需要在前面的阶段添加一个属性;isOutput=truename

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'] 作为条件:

- 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']的阶段Dependencies.stageName.outputs['jobName.stepName.variableName']

更新 2:

<块引用>

这里我正在部署而不是工作设置变量(variable.yaml)。所以 我将如何框架低于一个 Dependencies.stageNam.outputs['jobName.stepName.variableName'] 应该 我meniton Deployment as jobName ??

答案是肯定的。

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

注意:条件中有两个 jobName