3

I am trying to make my environment variables dependent on the Agent operating system.

I wrote the following test.yml, but it does not work:

jobs:
  - job: Test
    pool:
      vmImage: 'ubuntu-latest'
    steps:
      - pwsh: |
          echo $env:TestVar
          echo $env:OS
        env:
          OS: $[variables['Agent.OS']]
          ${{ if eq(variables['Agent.OS'], 'Windows_NT') }}:
            TestVar: "I am Windows"
          ${{ if eq(variables['Agent.OS'], 'Linux') }}:
            TestVar: "I am Linux"

Is there a problem with my YAML indentation?

7
  • Not sure about the conditional part, but you don't really need the OS environment variable, you should be able to access Agent.OS directly as $env:Agent_OS. Commented Jul 14, 2020 at 7:53
  • I was just checking if I my expression is correct. I think my conditional expression is correct, but maybe I cannot use it for env Commented Jul 14, 2020 at 8:04
  • All looks correct and follows what the docs say too, can you grab the logs of what is happening? Commented Jul 14, 2020 at 8:19
  • Here is the run log. Nothing useful. TestVar is empty. Also, OS is just printed as $[variables['Agent.OS']]. I can fix that by using $(Agent.OS), but that is another issue. Commented Jul 14, 2020 at 8:28
  • @Amin Your syntax is correct after changing OS: $(Agent.OS). If you change ${{ if eq(variables['Agent.OS'], 'Windows_NT') }} to ${{ if eq(variables['Build.SourceBranchName'], 'master') }}, the TestVar can be printed. It seems agent variable is not supported in such scenario. Commented Jul 14, 2020 at 10:43

3 Answers 3

2

Your YAML seems to be correct, but there is sth not clear on Azure DevOps. However, you can use this as a workaround:

trigger:
- master

jobs:
- job: Test
  pool:
    vmImage: 'ubuntu-latest'
  variables:
    varOS: $(Agent.OS)
  steps:
    - pwsh: |
        $testVar = ''
        $os = '$(Agent.OS)'
        if ($os -eq 'Windows_NT') {
          $testVar = "I am Windows";
        } elseif ($os -eq 'Linux') {
          $testVar = "I am Linux";
        }
        Write-Host $testVar
        Write-Host "##vso[task.setvariable variable=HelloOS;isOutput=true]$testVar"
      name: Initialize
    - pwsh: |
        echo $env:TestVar
        echo $env:OS
        echo $env:AGENT_OS
        echo $env:IsMaster
        printenv
      env:
        OS: $(varOS)
        TestVar: $(Initialize.HelloOS)
        ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/master')}}:
          IsMaster: "true"

for this build I got:

I am Linux
Linux
Linux
true

I tried few approached, also putting conditional setup in variables section. But only this gave expected result. Of course since you can access $env:AGENT_OS you can put this conditional logic directly in target step. I just assumed that you want to have TestVar provided somewhere else.

Sign up to request clarification or add additional context in comments.

2 Comments

If I set the env variable using $env:testVar="Linux" do I still need to use task.setvariable to make it available through other steps? I want to know if the env variables that are set, get lost between the steps.
IMHO yes, I did not check this, but all pipeline/release/agent/etc variables become env variables, but not otherwise.
2

Based on the answer of Cece Dong, Agent variables can't be used in this syntax. The cleanest way to achieve the same effect is just to use the shell for managing environment variables.

jobs:
  - job: Test
    pool:
      vmImage: 'ubuntu-latest'
    steps:
      - pwsh: |
           if ($env:AGENT_OS -eq "Windows_NT") {
             $env:TestVar="I am Windows"
           }
           if ($env:AGENT_OS -eq "Linux") {
             $env:TestVar="I am Linux"
           }

and if TestVar is supposed to be used in another step use this afterwards:

           echo "##vso[task.setvariable variable=env:TestVar]$env:TestVar"

Comments

1

Your syntax is correct after changing OS: $(Agent.OS). But Agent variables doesn't support used in a template expression. You could refer to the following link, the variables with Yes in Available in templates? are supposed to able able to used in a template expression.

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

1 Comment

Thanks that's a good explanation of why this does not work. Agent variables are not available in this syntax. learn.microsoft.com/en-us/azure/devops/pipelines/build/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.