I have two stages in a YAML file. the first stage is for specifically setting up variables based on the runtime parameters passed into it. (used in the arguments for the script)
stages:
- stage: A
displayName: Setting Variables
jobs:
- deployment: A1
environment: "<ENVIROMENT>"
displayName: Set Variables
strategy:
runOnce:
deploy:
steps:
- task: PowerShell@2
inputs:
targetType: filePath
filePath: 'F:\Files\Powershell\Users.ps1'
arguments: '-StaffName "${{ parameters.StaffName }}" -SiteName "${{ parameters.SiteName }}" -server "$(DBServerName)" -database "DatabaseBackups" -username "$(DBUser)" -password "$(DBPassword)"'
name: person
The script does its wizardry and gets the data from a SQL server DB, then writes it to an output variable (at least, that's what I am trying to do), in the log file it is showing variables are set correctly for that task
# Output the retrieved data as YAML
Write-Host "ForeName variable = $FirstName"
Write-Host "##vso[task.setvariable variable=ForeName;isOutput=true]$FirstName"
Write-Host "AzureEmail = $AzureEmail"
Write-Host "##vso[task.setvariable variable=AzureEmail;isOutput=true]$AzureEmail"
Write-Host "DBName = $DBName"
Write-Host "##vso[task.setvariable variable=DBName;isOutput=true]$DBName"
However, in my second stage, I try to use the output variable that I created in the previous stage. I have set a dependency on the previous stage (though I know I shouldn't need to when using stageDepencies, I have tried without dependency and it doesn't make a difference), created the variable on the stage level. Yet when I run the pipeline, the task is run and the variable is empty.
- stage: UseVariablesStage
dependsOn: A
variables:
ForeName: $[ stageDependencies.A.A1.outputs['person.ForeName'] ]
jobs:
- deployment: UseVariables
environment: "<ENVIROMENT>"
strategy:
runOnce:
deploy:
steps:
- script: echo $(ForeName)
I have tried setting the variable output in my PS1 file to read only (thinking it was being overwritten) - didn't work
I have tried marking the stage as dependant on the previous stage and rather than using stage dependency I used dependency (if that makes sense?) - didn't work
I have tried different syntax for how I use my variable, for example ($env:ForeName) - didn't work
This works for me:
The biggest difference is that I'm running an inline script as opposed to a script file, but I don't believe that makes much difference. But anyway if everything else fails try the same.
One thing I noticed in the pipeline logs (when running in Diagnostics mode) was the
Processedtext, which indicates the variable was set correctly: