azure pipeline variable created in powershell script not displaying value - powershell

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.

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)"

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

Related

how to get and set array inside the ADO pipeline Powershell from variable?

I have below code in yaml file:
trigger:
branches:
include:
- maventest
pool:
vmImage: ubuntu-latest
steps:
- task: PowerShell#2
inputs:
filePath: './1.ps1'
Below is 1.ps1 file
$user1=#($(user))
Write-Host $(user)
write-host $user1[0]
As I can pass the value of variable inside while run pipeline but user is not getting from variable.
I had also used the $(env:user) but it didn't get value from variable.
From your YAML sample, the cause of the issue is that the pipeline variable is not able to directly expand in PowerShell file.
To solve this issue, you can pass the pipeline variable to ps file via argument of PowerShell task.
Here is the example:
Ps file sample:
param($user)
$user1=#($user)
Write-Host $user
write-host $user1[0]
Pipeline sample:
steps:
- task: PowerShell#2
inputs:
filePath: './1.ps1'
arguments: '-user $(user)'
Result:

Azure DevOps set env variable

I am trying to set an environment variable to use it in another task but it doesn't work.
The first task should set the variable "versionTag" so I can use it in the next task as $(versionTag).
Can anyone help me with this?
- task: Bash#3
displayName: Create version tag
inputs:
targetType: 'inline'
script: |
versionTag=$(echo "$(Build.BuildNumber)" | tr '+' '-')
echo "versionTag: ${versionTag}"
echo "##vso[task.setvariable variable=versionTag]${versionTag}"
- task: Docker#2
displayName: Create runtime docker image
inputs:
containerRegistry: '$(dockerRegistryServiceConnection)'
repository: '$(imageRepository)'
command: 'build'
Dockerfile: '$(dockerfilePath)'
buildContext: '$(Build.SourcesDirectory)'
tags: |
$(tags)
$(versionTag)
There's a magic command string you can write to the log:
echo "##vso[task.prependpath]c:\my\directory\path"
The path will be updated for the scope of the Job. If your pipeline has multiple jobs, you need to issue the same command for future jobs as well.
The updated path will be available in the next step. Not in the step in which you issue the command.

Azure DevOps yaml: use a powershell task output parameter to generate a loop in dependent job

I have the following yaml as used in an Azure DevOps pipeline (this is not the full pipeline - it's just a portion of yaml that is in a template):
jobs:
- job: CheckExcludedWorkspaces
displayName: Check Excluded Workspaces
pool:
name: DefaultWindows
steps:
- task: PowerShell#2
name: GetWorkspaces
displayName: Check Excluded Workspaces
inputs:
filePath: "$(System.DefaultWorkingDirectory)/pipelines_v2/powershell/checkExcludedWorkspaces.ps1"
targetType: FilePath
errorActionPreference: 'stop'
arguments: -environmentFolder "$(rootFolderPrefix)\${{parameters.environmentFolder}}" -excludeFolderList "${{parameters.tagOutList}}"
pwsh: false
# - ${{ each folder in dependencies.CheckExcludedWorkspaces.outputs['GetWorkspaces.WorkspaceList'] }}:
- job: NewJob
dependsOn: CheckExcludedWorkspaces
variables:
testVar: $[ dependencies.CheckExcludedWorkspaces.outputs['GetWorkspaces.WorkspaceList'] ]
pool:
name: DefaultWindows
steps:
- powershell: |
Write-Host "Test var = $(testVar)"
displayName: Test workspaces output
this works correctly in that the second job retrieves a variable from a powershell task in the previous job and outputs that variable value. The task in the second job outputs a list of apps using variable testVar. The output contains:
app1,app2,app3,app4 etc
I would like to take this the next stage which is I would like to create a loop of jobs that repeated runs for this application list. Something like:
jobs:
- job: CheckExcludedWorkspaces
displayName: Check Excluded Workspaces
pool:
name: DefaultWindows
steps:
- task: PowerShell#2
name: GetWorkspaces
displayName: Check Excluded Workspaces
inputs:
filePath: "$(System.DefaultWorkingDirectory)/pipelines_v2/powershell/checkExcludedWorkspaces.ps1"
targetType: FilePath
errorActionPreference: 'stop'
arguments: -environmentFolder "$(rootFolderPrefix)\${{parameters.environmentFolder}}" -excludeFolderList "${{parameters.tagOutList}}"
pwsh: false
- ${{ each folder in dependencies.CheckExcludedWorkspaces.outputs['GetWorkspaces.WorkspaceList'] }}:
- job: NewJob
dependsOn: CheckExcludedWorkspaces
variables:
testVar: $[ dependencies.CheckExcludedWorkspaces.outputs['GetWorkspaces.WorkspaceList'] ]
pool:
name: DefaultWindows
steps:
- powershell: |
Write-Host "Test var = ${{folder}}"
displayName: Test workspaces output
This code gives me an error:
Unrecognized value: 'dependencies'. Located at position 1 within expression: dependencies.CheckExcludedWorkspaces.outputs['GetWorkspaces.WorkspaceList']
Is there a way i can use a powershell task output variable, to create a list of jobs in a dependent job? The problem is that i don't know at design time what the list of applications will be (the pipeline should ideally find this out when it runs). The list of applications is based on the list of folders that are created within a repository - which changes over time..
In current situation, we cannot use the 'each' key word for the variables. The 'each' keyword is used for the Obj type, but the variable is String.
For more details, you can refer the doc: Each keyword

How to use output variable value from one powershell script to another powershell script

Hi All I'm working on ymal release pipeline where I have two PowerShell script.
test1.ps1
test2.ps1
step 1)
In test1.ps1 I'm setting a output variable using :
$link="google.com"
Write-Host "##vso[task.setvariable variable=packageurl]$link"
step 2)
Now I want to use this pacakgeurl variable value in test2.ps1 script.
Ymal code will look like :
- task: AzurePowerShell#3
displayName: 'Query Latest Package'
inputs:
azureSubscription: 'test1'
ScriptType: FilePath
ScriptPath: 'source\test1.ps1'
- task: PowerShell#2
displayName: 'Download package'
inputs:
targetType: filePath
filePath: 'source\test2.ps1'
So basically I have to use the output variable value from 1 task to 2 task via PowerShell script.
I also tried to follow this link : https://developercommunity.visualstudio.com/content/problem/676342/how-to-use-output-variable-from-a-powershell-scrip.html
Can anyone help me on this ..?
Thanks in advance.
This works as you expect:
trigger: none
pr: none
pool:
vmImage: 'windows-latest'
steps:
- task: AzurePowerShell#3
displayName: 'Query Latest Package'
inputs:
azureSubscription: 'rg-the-code-manual'
ScriptType: FilePath
ScriptPath: 'stackoverflow\94-variables\script-1.ps1'
azurePowerShellVersion: LatestVersion
- task: PowerShell#2
displayName: 'Download package'
inputs:
targetType: filePath
filePath: 'stackoverflow\94-variables\script-2.ps1'
Yaml file is as you have it.
script-1.ps1 sets variable:
$link="google.com"
Write-Host "##vso[task.setvariable variable=packageurl]$link"
And script-2.ps1 uses that variable:
Write-Host $env:PACKAGEURL

Pass variable set in PowerShell task to another task (YAML)

I have a yaml file with following tasks:
parameters:
steps:
- task: AzurePowerShell#4
displayName: 'script'
inputs:
azureSubscription:
ScriptPath:
ScriptArguments:
azurePowerShellVersion: LatestVersion
- task: AzureResourceGroupDeployment#2
displayName: 'deployment'
inputs:
azureSubscription:
resourceGroupName:
location:
overrideParameters: '-abc $(var1) -def $(var2)'
deploymentMode: 'Incremental'
In the Powershell Script, I'm setting 2 variables as follows:
$ABC = 1
$DEF = 2
Write-Host "##vso[task.setvariable variable=var1;isOutput=true]$ABC"
Write-Host "##vso[task.setvariable variable=var2;isOutput=true]$DEF"
On trying to use these variables in the 2nd task (in overrideParameters section), I see the following error:
[error]InvalidContentLink: Unable to download deployment content from 'xxxx$(var1)'
[error]InvalidContentLink: Unable to download deployment content from 'xxxx$(var2)'
Am I setting variables in PowerShell script wrong?
You can try to add a reference name to the first task. For example:
- task: AzurePowerShell#4
displayName: 'script'
inputs:
azureSubscription:
ScriptPath:
ScriptArguments:
azurePowerShellVersion: LatestVersion
name: test
Then in the second task, get the variable value in the form of $(test.var1).
This is because in definition editor, downstream tasks won't get variable name intellisense for output variables that were published by an ad-hoc script. You can refer to this document for details.
In addition, here is a blog with some examples on how to pass variables in Azure Pipelines YAML tasks.