gpt4 book ai didi

powershell - Azure 管道 : how to use an output variable in a condition

转载 作者:行者123 更新时间:2023-12-05 03:40:47 26 4
gpt4 key购买 nike

我正在研究在 Windows 自托管代理上运行的管道。我需要调用一个脚本,在一个阶段初始化一个变量,并在下一阶段的条件下使用该变量。

这是我的 Yaml:

stages:
- stage: Init
jobs:
- job: RunScript
steps:
- task: PowerShell@2
name: compareFiles
inputs:
targetType: filePath
filePath: '***\compareFileContent.ps1'

- stage: DisplayStage
dependsOn: Init
variables:
areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
jobs:
- job: Display
steps:
- script: |
echo $(areFilesDifferent)

- stage: DeploymentStage
dependsOn: DisplayStage
variables:
areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
condition: eq( '$(areFilesDifferent)', 'true' )
jobs:
- deployment: DeploymentJob
environment: 'ATest'
strategy:
runOnce:
deploy:
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo EnvName Finally: $(areFilesDifferent)

这是我的 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"

显示作业打印“true”,但阶段条件始终返回 false。我尝试使用返回 bool 值而不是字符串的函数,但没有用。我在条件中尝试了不同的语法,总是返回 false。我尝试将条件下移到第一份工作,但在这种情况下,在评估条件之前会触发环境批准。

我的目标是让 Powershell 决定是否应该使用该环境。使用环境时,有一个审批步骤。

我确实写了另一篇关于这个的文章(Azure pipelines, question about variables, powershell and deployments),现在我成功地从脚本中检索了 Output 变量,但我仍然无法在条件下使用它。

你能帮我吗?

最佳答案

你应该试试这个:

  - stage: DeploymentStage
dependsOn: DisplayStage
variables:
areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
condition: eq(dependencies.Init.outputs['RunScript.compareFiles.areDifferent'], 'true')
jobs:
- deployment: DeploymentJob
environment: 'ATest'
strategy:
runOnce:
deploy:
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo EnvName Finally: $(areFilesDifferent)

这里的问题是 stageDependency 在阶段级别的语法不正确。您应该使用依赖表达式,然后也通过作业名称引用变量。请检查文档 here

关于powershell - Azure 管道 : how to use an output variable in a condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68023627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com