Referencing environment variables in powershell?

303 views
Skip to first unread message

RC

unread,
Oct 29, 2015, 7:16:20 AM10/29/15
to go-cd
I am using the go Powershell plugin (Start to execute task: Plugin with ID: gocd-powershell-runner) and I cannot work out any way to either pass go environment variables to the powershell script or reference go environment variables within the script.


I have defined an environment variable called my ENV_VAR and have tried the following to reference it without success:
  • define a Parameter at the job level call MY_PARAM that references the environment variable (e.g. MY_PARAM = MY_ENV_VAR) and then pass MY_PARAM to the powershell script. All that happens is the value MY_ENV_VAR is passed to the script
    • I have tried multiple different syntax formats to reference MY_ENV_VAR: ${MY_ENV_VAR}, ${env.MY_ENV_VAR}, $MY_ENV_VAR, %MY_ENV_VAR%
  • reference environment variables directly within the script, but they don't seem to be set in the same way they are for shell jobs
Is it not possible to get go to set a parameter based on an environment variable?

Is there another way to get hold of environment variables in powershell?

Jyoti Singh

unread,
Oct 29, 2015, 8:37:19 AM10/29/15
to go-cd
>> define a Parameter at the job level call MY_PARAM that references the environment variable (e.g. MY_PARAM = MY_ENV_VAR)

That will not work. Parameters (evaluated at the config level) cannot be set to have a value derived from an environment variable which are set only during pipeline execution. Also, MY_ENV_VAR is more like a text, an environment variable can be referenced using $MY_ENV_VAR or %MY_ENV_VAR% based on the platform you use, that is when they can be referenced. Also, 

Coming to using it in a script, well I haven't really used gocd-powershell-runner, but in case of windows you should be able to use something like %MY_ENV_VAR% in your script file. 
Check the job console log to see if the value for MY_ENV_VAR is indeed set.

Cheers,
Jyoti.
Reply all
Reply to author
Forward
0 new messages