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

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)

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

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:

Setting OS variable in Azure DevOps pipeline doesn't work

In YAML pipeline I'm attempting to set OS variable on Linux Agent so Cypress can look it up:
- script: export CYPRESS_key=ala
displayName: "Set key"
- script: echo $(CYPRESS_key)
displayName: "Print key"
unfortunately the OS variable is never set.
The output is:
/home/vsts/work/_temp/321aacd-cadd-4a16-a4d1-db7927deacde.sh: line 1: CYPRESS_key: command not found
$(command) and ${variable} you are using wrong brackets
- script: export CYPRESS_key=ala
displayName: "Set key"
- script: echo ${CYPRESS_key}
displayName: "Print key"
- script: echo $(cat /etc/os-release)
displayName: "Print file content"
Environment variables in Linux are accessed as $ENVIRONMENT_VARIABLE_NAME, not $(ENVIRONMENT_VARIABLE_NAME).

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