Rui Jarimba
Rui Jarimba

Reputation: 18094

Setting and using an environment variable in multiple release tasks

Problem

How do I pass a value from a release pipeline to a test assembly and a console application (.exe)? In this particular case, I need to pass a Personal Access Token (PAT) that is used by both the test assembly and the console application, something like this:

string token = Environment.GetEnvironmentVariable("appSettings_personalAccessToken");

I've been trying to set an environment variable in one particular task but I'm not able to use it in the the other tasks.

Details

I have configured a release pipeline that runs some integration tests and runs a powershell script that executes a console application:

Release pipeline tasks

Both the integration tests and the console application use a Personal Access Token (PAT) to access the Azure DevOps REST API. I'm reading this value from an environment variable named appSettings_personalAccessToken, which should be set in the release pipeline.

Set token release task

I'm trying to set the PAT in the first task (Powershell task - inline script) but it seems to be ignored in the other tasks, what I am doing wrong?

I tried to set the PAT in the powershell task like this:

Write-Host ##vso[task.setvariable variable=appSettings_personalAccessToken;isSecret=false;isOutput=true;]$personalAccessToken

Or like this:

[Environment]::SetEnvironmentVariable('appSettings_personalAccessToken', $personalAccessToken, 'User')
[Environment]::SetEnvironmentVariable('appSettings_personalAccessToken', $personalAccessToken, 'Machine')

But the value seems to be ignored in the other tasks. What am I missing here?

EDIT 1

Even trying to set the PAT hard-coded in the powershell task doesn't work:

Write-Host "##vso[task.setvariable variable=appSettings_personalAccessToken;isSecret=false;isOutput=true;]MY_TOKEN_VALUE"

Upvotes: 0

Views: 1255

Answers (4)

Rui Jarimba
Rui Jarimba

Reputation: 18094

I'll post my solution here, hopefully it will be useful for someone else that might have the same problem. Thanks @Matt and @Theo for pointing me in the right direction.

I had a wrong understanding on how the release variables work. I thought these would be available at the task level only, not to any app or test assembly executed from the release pipeline - that's why I was trying to set them using Powershell.

So the solution is very simple - I removed the powershell tasks that try to set the environments variables and set the variable as follows:

Release definition variables

Upvotes: 0

Daniel Mann
Daniel Mann

Reputation: 59035

There is no need for you to use a PAT for this. Use $(System.AccessToken). You can grant your build or release access to a system-provided OAuth token and then refer to that in instances where you need an auth token.

Please note that you need to grant access to the OAuth token, otherwise this will not work.

Upvotes: 2

Scott Heath
Scott Heath

Reputation: 890

When you used this command:

Write-Host ##vso[task.setvariable variable=appSettings_personalAccessToken;isSecret=false;isOutput=true;]$personalAccessToken

You need quotes around data:

Write-Host "##vso[task.setvariable variable=appSettings_personalAccessToken;isSecret=false;isOutput=true;]$personalAccessToken"

Otherwise PowerShell sees the # and comments the rest of the line

Upvotes: 0

Theo
Theo

Reputation: 61188

The problem here is that Windows does not automatically refresh the environment variables on create/change or remove. This only happens after a restart of the explorer.exe process or if you set the variable manually in
My Computer | Properties | Advanced | Environment Variables.

Explorer then broadcasts a WM_SETTINGCHANGE message to all windows to inform them of the change.
However, even when doing this manually, processes that were already running may not pick up the changes unless they handle the setting change message.

If restarting explorer is not a problem, you might try:

[Environment]::SetEnvironmentVariable('appSettings_personalAccessToken', $personalAccessToken, 'User')
[Environment]::SetEnvironmentVariable('appSettings_personalAccessToken', $personalAccessToken, 'Machine')
Stop-Process -ProcessName explorer

The explorer.exe process should restart automatically. This is controlled by the registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon AutoRestartShell value.

Hope that helps.

Upvotes: 0

Related Questions