0

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"
4
  • I wouldn't think the integration test task would need the PAT. How are you accessing the variable where you say it isn't being recognized in your console app? Make sure you reference them the correct way in the powershell script. Commented Jan 8, 2019 at 17:26
  • @Matt both the integration tests and the console application use the Azure DevOps REST API, that's why I need a PAT. I'm trying to avoid setting the PAT in configuration files, for security reasons. Commented Jan 8, 2019 at 17:44
  • I was trying to say really that you are only showing one side of the equation. Can you add details about how you are referencing the variable that you've set. Is there a reason you cant just use a secret variable on the release definition instead of trying to set it from powershell? Commented Jan 8, 2019 at 18:16
  • @Matt "Is there a reason you cant just use a secret variable on the release definition instead of trying to set it from powershell?" - yes, I can use a secret variable instead, I just tried it and it works fine. I thought the release variables wouldn't be available as environment variables inside the console app, that's why I was trying to set them using Powershell. Thanks! Commented Jan 9, 2019 at 8:26

4 Answers 4

2

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.

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

2 Comments

I tired using $(System.AccessToken) but it didn't work. My problem is that I need to set, somehow, an environment variable that can be used in both the integration tests assembly and a console app (.exe) that is executed via a powershell task.
I must have done something wrong before, I was now able to use $(System.AccessToken). I'm no longer trying to set an environment variable using Powershell, I just added the $(appSettings_personalAccessToken) to the variables section and set the value to $(System.AccessToken). Thanks for the help!
0

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.

2 Comments

This might be true for using the SetEnvironmentVariable, but shouldn't be a requirement for the ##vso command.
@theo tried to execute that but I get an error when trying to execute the Stop-Process cmdlet, maybe the build agents don't have permissions to do it in the release pipeline. This didn't work either: stackoverflow.com/a/37528395/558486
0

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

9 Comments

You're absolutely right - I actually had the quotes before but for some reason I removed them (go figure).... Anyway, I tried again putting back the quotes but no success :(
@RuiJarimba In your inline script where is the value of $personalAccessToken set? Are you trying to set $personalAccessToken to the value of your release variable or are you trying to update the value of the release variable?
@RuiJarimba I also don't see isOutput as a valid property for task.setvariable on this page - github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/… - but maybe I am missing something
$personalAccessToken is set in the variables section of the release pipeline. I tried using $System.AccessToken as well, without success. Oh, and no luck using isOutput either!
If the release variable is named personalAccessToken and the local environment variable is named appSettings_PersonalAccessToken you need to switch them around in the statement. Write-Host "##vso[task.setvariable variable=personalAccessToken;isSecret=false]$env:appSettings_personalAccessToken"
|
0

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

Comments

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.