0
votes

I'm facing problems when I try to send an Environment variable between tasks using Powershell and Ubuntu hosts in Azure DevOps build pipelines.

To test and show this issue I have created a pipeline with the following:

-Two tasks are created, the first one is writing an environment variable:

$EnvVariable= "Value"

Write-Host "##vso[task.setvariable variable=EnvVariable;]$EnvVariable"

Write-Output "##vso[task.setvariable variable=EnvVariable;]$EnvVariable"

-The second Task is printing this value in the output:

Write-Host "env:EnvVariable : $($env:EnvVariable)"

Write-Host "ENV:EnvVariable : $($ENV:EnvVariable)"

Write-Output "env:EnvVariable : $($env:EnvVariable)"

Write-Output "ENV:EnvVariable : $($ENV:EnvVariable)"

-The output from this pipeline is looking good in Windows hosts, for example, using "vs2017-win2016" host I get this output:

env:EnvVariable : Value
ENV:EnvVariable : Value
env:EnvVariable : Value
ENV:EnvVariable : Value

-But when I execute this same pipeline in ubuntu hosts I'm not getting the expected output, here you have the output from second task executed in "ubuntu-18.04" build host:

env:EnvVariable : 
ENV:EnvVariable : 
env:EnvVariable : 
ENV:EnvVariable : 

As you can see there are no values from environment variable using the same tasks but in ubuntu hosts.

Just to double-check I've used Write-Output and Write-Host to check if the problem is caused by the requirement to use one of those and used env: and ENV: just to check if the case-sensitive variable name is the cause.

Thanks a lot for your time.

2
I found an alternative which is use "Environment Variables" section from the PowerShell task definition and define all required variables in there, but this is not the best approach since in the real scenario I'm using a script defined in a repository with multiple variables and It's a time consuming if you need to define all them in this sectiongarrigueta
Hi garrigueta, any update ?LoLance

2 Answers

0
votes

If you don't want to use env section you need to use upper case for Linux based agents:

- task: PowerShell@2
  displayName: Set Variables
  name: setVariables
  inputs:
    targetType: 'inline'
    pwsh: true
    script: |
      $EnvVariable= "Value"

      Write-Host "##vso[task.setvariable variable=EnvVariable;]$EnvVariable"

      Write-Output "##vso[task.setvariable variable=EnvVariable;]$EnvVariable"

- task: PowerShell@2
  displayName: Read variables 0
  inputs:
    targetType: 'inline'
    pwsh: true
    script: |
      Write-Host "env:EnvVariable : $($env:ENVVARIABLE)"

      Write-Host "ENV:EnvVariable : $($ENV:ENVVARIABLE)"

      Write-Output "env:EnvVariable : $($env:ENVVARIABLE)"

      Write-Output "ENV:EnvVariable : $($ENV:ENVVARIABLE)"

- task: PowerShell@2
  displayName: Read variables 2
  env:
    EnvVariable: $(EnvVariable)
  inputs:
    targetType: 'inline'
    pwsh: true
    script: |
      Write-Host "env:EnvVariable : $($env:EnvVariable)"

      Write-Host "ENV:EnvVariable : $($ENV:EnvVariable)"

      Write-Output "env:EnvVariable : $($env:EnvVariable)"

      Write-Output "ENV:EnvVariable : $($ENV:EnvVariable)"
0
votes

I'm facing problems when I try to send an Environment variable between tasks using Powershell and Ubuntu hosts in Azure DevOps build pipelines.

According to Environment variables:

On UNIX systems (macOS and Linux), environment variables have the format $NAME. On Windows, the format is %NAME% for batch and $env:NAME in PowerShell.

So what you met is expected behavior according to the document. And to work for both Windows and Linux, I suggest using format $(EnvVariable).

Details:

The difference when you set the variable EnvVariable:

  1. In Linux using Powershell task you should use $env:ENVVARIABLE. Also it works for Powershell task in Windows, but it doesn't work in CMD/bash task.

  2. In Linux using bash task you should use $ENVVARIABLE, but it can't work in PS task.

  3. In both Linux and Windows, no matter bash task, cmd task or powershell task, you can use $(EnvVariable).