试图从预定义的变量中使用set变量,并将其用于azure devops管道中的阶段条件



我试图从管道资源的预定义变量设置变量,并在舞台条件下使用它,但我无法成功。下面是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)

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

所需条件如下

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

下面的是变量。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"
有人能在这个问题上帮助我吗?提前感谢

试图从预定义变量中使用set变量,并在azure devops管道中使用它

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

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

有关更多细节,请查看文档Jobs可以访问先前阶段的输出变量。

我们还需要添加属性;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*.')))

注意:因为需要在所有stage之上添加条件,我们需要更新阶段参考stageDependencies.stageName.jobName.outputs['stepName.variableName']ToDependencies.stageName.outputs['jobName.stepName.variableName']

更新2:

这里我在部署而不是工作下设置变量(variable.yaml)。所以我该如何构建1以下的框架呢Dependencies.stageNam.outputs [' jobName.stepName。variableName ']应该是否提及部署为jobName ??

答案是肯定的。

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

注:条件中有两个jobName

最新更新