0

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 Answers 2

1

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)"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Adding the parentheses did the trick. I knew it had to be something minor.
1

You should have Write-Host

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

like here

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.