How to write a foreach loop on build variable in Azure devops Powershell#2 task? - azure

I have an azure pipeline that maintain a variable that holds project names - lets assume parameters.projects that holds projectA, projectB, projectC
I wish to execute a foreach loop and perform an operation on every project.
I currently use
- ${{ each Project in parameters.projects}}:
- task: PowerShell#2
displayName: "operation on [${{ Project }}]."
inputs:
targetType: 'inline'
workingDirectory: $(System.DefaultWorkingDirectory)
script: |
New-Item -Path '$(Build.ArtifactStagingDirectory)\${{ Project }}' -ItemType Directory
....
In the above example the foreach argument (iteration value) is azure's, which means it will spwan a task for each project in the pipeline. This works but roughly slow.
I wish to run something like...
- task: PowerShell#2
displayName: "operation on [${{ Project }}]."
inputs:
targetType: 'inline'
workingDirectory: $(System.DefaultWorkingDirectory)
script: |
foreach ($Project in ${{ parameters.projects }})
{
New-Item -Path '$(Build.ArtifactStagingDirectory)\${{ Project }}' -ItemType Directory
....
}
But i'm not sure about the syntax, and couldn't find a useful explanation/examples.
what is the right syntax? also a description web page it useful also.

Runtime parameters let you have more control over what values can be passed to a pipeline. With runtime parameters you can:
Supply different values to scripts and tasks at runtime
Control parameter types, ranges allowed, and defaults
Dynamically select jobs and stages with template expressions
So, your current script is the right syntax.
I tested the object type parameters; It cannot be used in the PowerShell task.
You can try to use the string type parameters and split the string for loop.
YAML like:
parameters:
- name: projects
type: string
default: project1;project2
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
$prjs = "${{parameters.projects}}"
$projects = $prjs -Split ";"
foreach($project in $projects){
New-Item -Path '$(Build.ArtifactStagingDirectory)\$project' -ItemType Directory
}
For more information, you could refer to the runtime parameters.

Related

Azure pipeline ##vso[build.addbuildtag] does not work

I have a problem with adding buildtag to one specific pipeline.
When i use this code with normal string it adds tag successfully:
- bash: |
echo "##vso[build.addbuildtag]TEST_TAG"
displayName: 'Add TAG to Run'
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
but when i use it with variable it throws me an error.
The funniest thing is that same code with variable works fine in another pipeline:
- bash: |
echo "##vso[build.addbuildtag]$(ChangeNumber)"
displayName: 'Add TAG to Run'
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
Error:
##[error]Unable to process command '##vso[build.addbuildtag]' successfully. Please reference documentation (http://go.microsoft.com/fwlink/?LinkId=817296)
##[error]Build tag is required.
Variable is fine because i "echo" it earlier successfully.
What might be the issue?
I figured out that variable was an issue and that it was defined but not passed to another tasks, so the solution is:
- task: Bash#3
displayName: 'Add TAG to Run'
name: TAG
inputs:
targetType: "inline"
script: |
ChangeNumber=$(<$(System.ArtifactsDirectory)/variables/ChangeNumber.var)
echo "##vso[task.setvariable variable=ChangeNumber;isOutput=true]$ChangeNumber"
echo "##vso[build.addbuildtag]$ChangeNumber"
failOnStderr: true

Azure Pipelines, iterate through files

In azure build pipeline, I'm trying to iterate through all the files that have extension .hex. The aim is to use every file the same way, but not knowing how many are there.
I receive an error from Azure :
Encountered error(s) while parsing pipeline YAML:
/azure-pipelines.yml (Line: 11, Col: 3): Unexpected symbol: '*'. Located at position 26 within expression: $(Build.SourcesDirectory)*.hex.
The Pipeline has been reduced to the minimum possible :
trigger: none
pool:
name: "LabelManagementPool"
steps:
- ${{ each filename in $(Build.SourcesDirectory)\*.hex}}:
- script: echo ${{ filename }}
What am I missing here ? Is what I want to achieve possible ?
Unfortunately it is not possible to do it this way as when the pipeline kicks of it will compile into a structure with all stages, jobs, etc defined. Since this path doesn't exist yet it can't be compiled.
The workaround would be to use another programming language, such as python or powershell, to obtain the list of files that match the decription. And then use it to loop over each file and perform whatever action you want it to run.
As an example, you could use the following in PowerShell to find all hex files and then would need to write additional code for looping over it and performing your desired task.
# Obtain list of all files that are hex
$files = Get-ChildItem "C:\repos\python" -Filter *.hex -Recurse | % { $_.FullName }
# Loop over every file
Foreach ($file in $files)
{echo $file}
The yaml would be this
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
# Obtain list of all files that are hex
$files = Get-ChildItem "$(Build.SourcesDirectory)" -Filter *.hex -Recurse | % { $_.FullName }
# Loop over every file
Foreach ($file in $files)
{echo $file}

Why can I not pass a runtime variable in if condition in azure devops yaml template

I think I'm calling my variable os_name from the powershell step incorrectly; each time I test the build it doesn't pass through my if statement. I'm wondering what's wrong with my code. Has anyone tried using runtime variables in azure devops expression similar to this?
- powershell: |
$os_name = 'linux'
echo "##vso[task.setvariable variable=os_name;]$os_name"
- ${{ if eq( variables.os_name , 'linux') }}:
- powershell: |
echo "variables.os_name = linux"
This should work for you:
- task: PowerShell#2
displayName: set variable
inputs:
targetType: 'inline'
script: |
"##vso[task.setvariable variable=os_name;]linux"
- task: PowerShell#2
displayName: Linux case
inputs:
targetType: 'inline'
script: |
Write-Host "Linux"
condition: eq(variables.os_name, 'linux')
Run for Linux:
Run for non Linux:

How can I use a variable of one yaml in another yaml using powershell?

As I am new to yaml and powershell,Can someone please help me in understanding the way of using a variable of one Yaml in another one?
Example Scenario:
I have variable- $a in abc.yml under the folder-Test1,now I have to use the same $a in bcd.yaml under the folder -Test2.
How can I achieve this in powershell?
To pass params between yml files in Azure DevOps you have to specify a path to the template (the file you want to pass the param across to) and give the parameter a value. Then in the second file also declare the parameter.
To make use of the param it in the second yaml you can use the ${{parameters.name}} construction:
Let's assume $a represents a directory path...
Example contents of abc.yml:
stages:
- stage: 'Passing param example'
jobs:
- template: ${{variables['System.DefaultWorkingDirectory']}}/bcd.yml
parameters:
param1_butuseanynameyoulike: $a
The in the bcd.yml:
parameters:
- name: 'param1_butuseanynameyoulike'
displayName: 'Parameter passed from original yaml file'
type: string
default: ''
jobs:
- job: somename
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
Get-ChildItem -Path ${{ parameters.param1_butuseanynameyoulike }}"

Referencing yaml variables in AzurePowerShellV5 inline script

I have a Azure DevOps yaml pipeline, that looks (a bit like) like this:
variables:
MyVar: Test
Steps:
- task: AzurePowerShell#5
displayName: 'Test variables from yml file'
inputs:
azureSubscription: MyServiceConnection
ScriptType: InLineScript
InLine: |
Write-Host "I really want to see the values from the variables in the yml file here"
Write-Host "parameters from the yml file would be great too"
Write-Host "But what would I write to do that?"
Write-Host "$(MyVar) <- Nothing here"
Write-Host "$(variables.MyVar) <- Nothing here"
Write-Host "$DoesThisWork <- Nothing here"
Write-Host "$OrThis <- Nothing here"
env:
DoesThisWork: $(MyVar)
OrThis: $(variables.MyVar)
How do I use MyVar in the InLine script?
I stripped it down to the simplest possible and it works just fine:
pool:
vmImage: 'windows-latest'
variables:
myVar: test value
steps:
- powershell: Write-Host "$(myVar)"
generates:
I modified your example to remove the env block which did not compile, and remove the incorrect variable references:
pool:
vmImage: 'windows-latest'
variables:
myVar: test value
steps:
- task: AzurePowerShell#5
inputs:
azureSubscription: 'My Service Connection Name'
ScriptType: 'InlineScript'
Inline: |
Write-Host "I really want to see the values from the variables in the yml file here"
Write-Host "parameters from the yml file would be great too"
Write-Host "But what would I write to do that?"
Write-Host "$(MyVar) <- Nothing here"
azurePowerShellVersion: 'LatestVersion'
and got:
It didn't get past the first one because the syntax $(variables.MyVar) is invalid. The syntax works as follows:
Compile time (usable only in the file in which the variable is declared and not to nested files like templates): ${{ variables.MyVar }}
Runtime (before task execution): $(MyVar) - expands to "$(MyVar)" if empty
Runtime (designed for conditions, or where the default value causes problems): $[variables.MyVar] - expands to empty string if empty
I'm wondering if the lack of an Azure Powershell version was part of your problem?
When you specify an env block, it creates the variables as environment variables.
In PowerShell, you reference environment variables with $env:VariableName. So in your case, $env:DoesThisWork.

Resources