I have a stage set up in my build pipeline to detect changes in specific folders in my repo and then I use a condition based on the output variable to trigger or skip a build stage for each path. This works perfectly fine between my Detect and Build stages, but when I then reference a template this variable no longer is accessible.
Code example:
#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"'
The above code works perfectly. I output a variable in my DetectChanges stage that I then reference in my build stage for each of my paths(only have one of the 3 placeholder builds in the example above).
The problem comes in my next stages where I have a template defined:
#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
This references my template with the below code:
#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
This stage then is always skipped due to the stageDependencies reference being null when executed from the template.
Stage execution results below show the value as 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
Why does running a stage in a template vs in line on the same file result in a variable reference not working?