0
votes

In an Azure Pipeline, I want to set a variable in a Powershell script and then use that variable in a later build step. The variable doesn't print out a value.

In my Powrshell script I hardcode a value for testing purposes:

$packageFolder = 'dotnet/TestPackage/'
##vso[task.setvariable variable=changedPackage;isOutput=true]$packageFolder

Here is my YAML file:

steps:
  - task: PowerShell@2
    displayName: 'Detect Subfolder Changes'
    name: setvarStep
    inputs:
      targetType: 'filePath'
      filePath: $(System.DefaultWorkingDirectory)\detectchanges.ps1
      failOnStderr: true
 - script: echo "$(setvarStep.changedPackage)"
 - task: DotNetCoreCLI@2 
   displayName: 'dotnet build'
   condition: ne('$(setvarStep.changedPackage)', '')
   inputs: 
      projects: '$(setvarStep.changedPackage)' 

When the pipeline gets to the script step it just prints out this:

Script contents:
echo "$(setvarStep.changedPackage)"

I don't think the variable is actually set. When the pipeline gets to the dotnet build step it throws an error: ##[error]Project file(s) matching the specified pattern were not found.

While having debugging turned on I noticed this in the dotnet build step logs: ##[debug]findPath: 'C:\agents\librarywin-1\_work\2\s\$(setvarStep.changedPackage)'

I think my issue is the pipeline variable syntax is wrong even though I'm following the example on Microsoft's website. I can't figure it out.

2

2 Answers

1
votes

You should have Write-Host

$packageFolder = 'dotnet/TestPackage/'
Write-Host ##vso[task.setvariable variable=changedPackage;isOutput=true]$packageFolder

like here

enter image description here

1
votes

You need double quote the logging command:

detectchanges.ps1:

$packageFolder = 'dotnet/TestPackage/'
"##vso[task.setvariable variable=changedPackage;isOutput=true]$packageFolder"

YAML:

steps:
- task: PowerShell@2
  displayName: 'Detect Subfolder Changes'
  name: setvarStep
  inputs:
    targetType: 'filePath'
    filePath: $(System.DefaultWorkingDirectory)\detectchanges.ps1
    failOnStderr: true
- script: echo "$(setvarStep.changedPackage)"