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

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

Related

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

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.

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

How to nest yaml variable expressions?

I want to nest yaml variable expressions in a Azure Devops Pipeline as one step uses the project name separated by spaces and another uses periods ie 'My Project' vs 'My.Project'.
This is what I have currently but the echo is outputting 'My Project' instead of 'My.Project'
- script: echo "projectKey2 is ${{replace(${{ variables.projectName }},' ','.')}}"
You have extra brackets around variables.projectName that should not be there:
- script: echo "projectKey2 is ${{ replace(variables.projectName,' ','.') }}"

In Azure, can I create a mapping at runtime?

I'm trying to loop over an array of group names and I want to dynamically get the IDs of those groups so that I can assign roles to them.
I just learned I can set a variable at runtime in a pipeline like this:
steps:
- script: echo "##vso[task.setvariable variable=myVar;]foo"
displayName: Set variable
- script: echo "You can use macro syntax for variables: $(myVar)"
displayName: Echo variable
But to loop over the group names I now want to set a mapping so that I can use that in a subsequent step where I assign the roles. I tried the following:
steps:
- script: echo "##vso[task.setvariable variable=mymapping;]{a: 1}"
displayName: Set mapping
- script: echo $(mymapping)
displayName: Echo mapping
- script: echo $(mymapping.a)
displayName: Echo mapping value
But I get an error saying Mapping values are not allowed in this context.
Is there any other way of creating some sort of mapping/object/dict from within a pipeline?

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