user6264
user6264

Reputation: 185

How to read environment variables in azure devops pipeline using powershell?

I created a PowerShell job and used the below code to set the environment variable in the azure pipeline using Powershell.

[Environment]::SetEnvironmentVariable("key", "value")

I can print the value using the $env:key in the same job itself.

But when I tried to display the value using $env:key in the next job nothing is printed. How to use the above environment variable through out the azure pipeline. Is there any other way to set and read custom environment variables.

Upvotes: 0

Views: 10512

Answers (3)

Prateek
Prateek

Reputation: 317

This is pretty straight forward, there are lots of confusion going around so I thought of answering it.

Step 1) Define your variable and its value under Variables tab inside Pipeline Edit Variables in Release Pipeline

Step 2) Under Environment Variables, map your variable from step 1 with ENV Variable name that will be used inside the Powershell scripts. Environment Variable Mapping in Azure DevOPS

Step 3) Now, access your variable with the name used in Step 2. For example, in my case it will be $env:ENVPWD It will give me the value of the variable configured in Step 1 inside the script during runtime.

Hope that helps!

Upvotes: 0

LoLance
LoLance

Reputation: 28196

According to this, using outputs in a different job is not supported in Classic UI Format.

As workarounds in this scenario, you can share variables via Pipeline Variables(share variables across jobs in same pipeline) or Variable Groups(share variables across pipelines that use same Variable Group, it also works across jobs).

Since you only want to share variables across jobs in same pipeline, pipeline variable is enough for you.

1.You can set a key variable in pipeline variables:

enter image description here

2.Add one Powershell Inline task with content below in your first job:

$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/$($env:SYSTEM_DEFINITIONID)?api-version=5.0"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

# Update an existing variable to its new value
$pipeline.variables.key.value = "value"

####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99

$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

write-host "==========================================================" 
Write-host "The value of Varialbe key is updated to" $updatedef.variables.key.value
write-host "=========================================================="

3.Run the pipeline we can find the value of key variable is successfully updated:

enter image description here

So you can run the ps script in first job to update the value of key variable, then all next jobs can access the updated variable easily.

Note:

  1. For the script itself, you only need to change lines $pipeline.variables.key.value = "value"(necessary) and Write-host "The value of Varialbe key is updated to" $updatedef.variables.key.value(optional).

If I want to set the variable named MyTest to value MyValue, the lines should be $pipeline.variables.MyTest.value = "MyValue" and Write-host "The value of Varialbe MyTest is updated to" $updatedef.variables.MyTest.value.

  1. To make sure the ps task in one job can access OAuth Token, we should Allow Scripts to Access OAuth Token. Click the agent job name and check the box:

enter image description here

  1. To enable the pipeline has the permission to update pipeline variable (edit build pipeline), go pipeline security to set the Edit build pipeline allow for user xxx(ProjectName) build service.

enter image description here

Hope all above helps :)

Upvotes: 2

4c74356b41
4c74356b41

Reputation: 72191

you pretty much have to either use library variable groups (or sets, dont remember the name) or you have to use a specific way to share variables across jobs:

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#use-output-variables-from-tasks

Upvotes: 2

Related Questions