Referencing yaml variables in AzurePowerShellV5 inline script - azure

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.

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

Set environment variable in unix machine using bash script via YAML steps

I need to set the below environment variable to a Unix machine through bash script as an inline script in the YAML file
My env variables are
cache=30
delay=10
url={"https.8shd3dad#d/wipeout#doamin.com"}
I have tried the below steps in YAML for setting the same, but couldn't see the environment variable and its value at my runtime
- task: Bash#3
inputs:
targetType: 'inline'
script: export cache=30
export url={"https.8shd3dad#d/wipeout#doamin.com"}
export Delay=10
env:
cache : $(30)
Can anyone help me in fixing this issue? since I am new to YAML and bash.
After each export command, you also need to set the variable in the variable service to be able to expose it as an environment variable.
- task: Bash#3
inputs:
targetType: 'inline'
script: |
export cache=30
echo "##vso[task.setvariable variable=cache;]${cache}"
export url={"https.8shd3dad#d/wipeout#doamin.com"}
echo "##vso[task.setvariable variable=url;]${url}"
export Delay=10
echo "##vso[task.setvariable variable=Delay;]${Delay}"
env:
cache : $(30)

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

Resources