【发布时间】:2021-06-18 10:10:42
【问题描述】:
在之前的帖子Azure pipeline: how to use an output variable in a condition 中,我在使用依赖项中的变量时遇到了问题。
现在,我有一个管道,我在其中调用返回 true 或 false 的脚本。 返回值设置下一阶段要运行的内容,但我现在有两个问题。
变量的条件有效,但变量的显示没有显示。 该条件使工作流转到两个分支,但现在我需要它只返回一个分支
这是我的 Yaml:
stages:
- stage: Init
jobs:
- job: RunScript
steps:
- task: PowerShell@2
name: compareFiles
inputs:
targetType: filePath
filePath: '***\compareFileContent.ps1'
- stage: Display1
dependsOn: Init
variables:
areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
jobs:
- job: RunScript
steps:
- task: CmdLine@2
inputs:
script: |
echo areFilesDifferent: $(areFilesDifferent)
- stage: DeploymentStageWithApproval
dependsOn: Display1
variables:
areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
condition: eq( variables.areFilesDifferent, 'false' )
jobs:
- deployment: DeploymentJob
environment: 'STWithApproval'
strategy:
runOnce:
deploy:
steps:
- task: CmdLine@2
inputs:
script: |
echo DeploymentStageWithApproval: $(areFilesDifferent)
- stage: DeploymentStageWithoutApproval
dependsOn: Display1
variables:
areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
condition: eq( variables.areFilesDifferent, 'true' )
jobs:
- deployment: DeploymentJob
environment: 'ST'
strategy:
runOnce:
deploy:
steps:
- task: CmdLine@2
inputs:
script: |
echo DeploymentStageWithoutApproval: $(areFilesDifferent)
- stage: Display2
condition: or( eq(dependencies.DeploymentStageWithApproval.result, 'Succeeded'), eq(dependencies.DeploymentStageWithoutApproval.result, 'Succeeded') )
variables:
areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
jobs:
- job: RunScript
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo areFilesDifferent: $[ variables.areFilesDifferent ]
echo DeploymentStageWithApproval: $[ dependencies.DeploymentStageWithApproval.result ]
echo DeploymentStageWithoutApproval: $[ dependencies.DeploymentStageWithoutApproval.result ]
这是我的 Powershell:'***\compareFileContent.ps1'
$equal = $filetext1 -ceq $filetext2 # case sensitive comparison
$equalString = (&{If($equal) {"true"} Else {"false"}})
echo "##vso[task.setvariable variable=areDifferent;isOutput=true]$equalString"
所有的脚本作业都只写文本,不写变量,我不明白为什么。
如果运行 DeploymentStageWithApproval,则会触发批准,但不会运行 Display2。
如果运行 DeploymentStageWithoutApproval,则运行 Display2
我的感觉是因为如果批准,但一旦我批准,任务就会运行,为什么它没有进入“阶段:Display2”?
感谢您的帮助, 克劳德
***************************更新1
谢谢,您的 cmets 确实帮助我理解了语法。不过,在我的最后阶段,变量没有打印出来,我不明白为什么。
这是我的 YAML 的一个较小版本,用于演示我的问题:
stages:
- stage: Init
jobs:
- job: RunScript
steps:
- checkout: none
- task: PowerShell@2
name: compareFiles
inputs:
targetType: filePath
filePath: '***\compareFileContent.ps1'
- stage: Display1
dependsOn: Init
variables:
areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
jobs:
- job: DisplayVariables
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo areFilesDifferent: $(areFilesDifferent)
显示:areFilesDifferent:true
- stage: WithoutApproval
dependsOn: Display1
condition: eq( dependencies.Init.outputs['RunScript.compareFiles.areDifferent'], 'true' )
variables:
areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
jobs:
- deployment: DeploymentJob
environment: 'ST'
strategy:
runOnce:
deploy:
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo DeploymentStageWithoutApproval: $(areFilesDifferent)
显示:DeploymentStageWithoutApproval:
我会假设它与部署任务相关联,但我不知道如何获得该值,我确实尝试将变量分配移动到作业下但没有成功。
我可能不需要那个地方的变量,但为了调试,理解这个问题会很有用。
感谢和问候, 克劳德
【问题讨论】:
标签: azure-pipelines azure-pipelines-yaml