我在构建管道中设置了一个阶段,用于检测存储库中特定文件夹中的更改,然后使用基于输出变量的条件来触发或跳过每个路径的构建阶段。 这在我的检测和构建阶段之间工作得非常好,但是当我引用模板时,该变量不再可访问。
代码示例:
#azure-pipelines.yml file
stages:
- stage: DetectChanges
displayName: Detect Changes
jobs:
- job: DetectChanges
displayName: 'Detect changes'
steps:
- powershell: |
$pathfilters = @("ui/", "api/", "publicapi/")
foreach($path in $pathfilters) {
$changed = $(git diff HEAD HEAD~ --name-only $path)
if ($changed.count -gt 0) {
echo "$($changed.count) change$(if ($changed.count -gt 1) {echo s}) detected on $path"
echo "##vso[task.setvariable variable=$($path.Substring(0,$path.Length-1))_changed;isOutput=true]true"
}
else {
echo "No changes detected on $path"
echo "##vso[task.setvariable variable=$($path.Substring(0,$path.Length-1))_changed;isOutput=true]false"
}
}
name: detect_changes
displayName: Detect Changes
- stage: Build
displayName: Build
jobs:
- job: UIBuild
displayName: UI-Build
condition: eq(stageDependencies.DetectChanges.DetectChanges.outputs['detect_changes.ui_changed'], 'true')
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: 'Write-Host "UI-Build"'
上面的代码完美运行。 我在 DetectChanges 阶段输出一个变量,然后在构建阶段为每个路径引用该变量(只有上面示例中的 3 个占位符构建之一)。
问题出现在我定义了模板的下一个阶段:
#azure-pipelines.yml next stage snippet (after the code above)
- stage: DevDeploy
displayName: Dev Deploy
dependsOn: Build
variables:
- group: dev
jobs:
- template: templates/deploy.yml
这引用了我的模板,代码如下:
#templates/deploy.yml snippet
- deployment: ui
condition: eq(stageDependencies.DetectChanges.DetectChanges.outputs['detect_changes.ui_changed'], 'true')
environment: '$(myEnv)'
strategy:
runOnce:
deploy:
steps:
- template: uideploy.yml
由于从模板执行时 stageDependency 引用为空,因此始终会跳过此阶段。
下面的阶段执行结果显示值为 null:
Started: Today at 3:39 PM
Duration: 1h 37m 22s
Evaluating: eq(stageDependencies['DetectChanges']['DetectChanges']['outputs']
['detect_changes.ui_changed'], 'true')
Expanded: eq(Null, 'true')
Result: False
为什么在模板中运行阶段与在同一文件上运行阶段会导致变量引用不起作用?
您可以尝试将 vso 任务的输出作为变量添加到舞台吗?
- stage: DevDeploy
displayName: Dev Deploy
dependsOn: Build
variables:
- group: dev
- name: NameOfTheVAR
value: $[ stageDependencies.DetectChanges.DetectChanges.outputs['detect_changes.ui_changed'] ]
jobs:
- template: templates/deploy.yml