Creating a variable from a PS1 file and it is blank when called from another stage (DevOps YAML)

32 Views Asked by At

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

1

There are 1 best solutions below

0
Rui Jarimba On

This works for me:

name: $(Build.DefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)

trigger: none

pool: Default

stages:
  - stage: set_stage
    displayName: Set variable
    jobs:
      - job: set_job
        displayName: Set Pipeline Variable
        steps:
          - pwsh: |
              Write-Host "##vso[task.setvariable variable=FullName;isOutput=true;issecret=false;isreadonly=true]John Doe"
            displayName: 'Set Pipeline Variable'
            name: Person
          - pwsh: |
              Write-Host "Hello $(Person.FullName)"
            displayName: 'Display Pipeline Variable'

  - stage: read_stage
    displayName: Read variable
    dependsOn: set_stage
    jobs:
      - job: read_job
        displayName: Read Pipeline Variable
        variables:
          personName: $[stageDependencies.set_stage.set_job.outputs['Person.FullName']]
        steps:
          - pwsh: |
              Write-Host "Hello $(personName)"
            displayName: 'Read Pipeline Variable'

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 Processed text, which indicates the variable was set correctly:

##[debug]Processed: ##vso[task.setvariable variable=FullName;isOutput=true;issecret=false;isreadonly=true]John Doe