Set environment variables from other variables

2,347 views
Skip to first unread message

Michael Kunze

unread,
Jul 14, 2014, 12:36:08 PM7/14/14
to go...@googlegroups.com
Hey,

Did not find anything in the (short) docs or on user groups.
Simple question:

Is it possible to define an environemnt variable which is a composite of other variables. Simplest case would be:

<variable name="VERSION">
    <value>${GO_PIPELINE_LABEL}</value>
</variable>

Will produce
[go] setting environment variable 'VERSION' to value '${GO_PIPELINE_LABEL}'

Desired:
[go] setting environment variable 'VERSION' to value '1.17.2463'

Any thoughts on the matter?
Thanks

Tushar Madhukar

unread,
Jul 15, 2014, 12:39:40 AM7/15/14
to go...@googlegroups.com
I too have sorely missed this functionality. Environment variables can't be used to define other variables. 
Even in job configs, I cannot re-use an already defined environment variable.

While working on a custom plugin, I noticed that environment variables are only available during pipeline runs (e.g. via com.thoughtworks.go.plugin.api.task.TaskExecutionContext.environment()). 
Due to this, there is no way to resolve/validate a config element such as:
<variable name="VERSION">
    <value>${GO_PIPELINE_LABEL}</value>
</variable>

Any thoughts?

Marius Ciotlos

unread,
Jul 18, 2014, 1:04:49 PM7/18/14
to go...@googlegroups.com
We are using Pipeline parameters instead of Environment variables. 
It depends what you are trying to achieve, but for our needs we define a Template which relies on #{VARIABLE} which is defined at pipeline level as a Pipeline parameter. 

We do needed environment variable, we're relaying a lot on GO_PIPELINE_LABEL to extract app version and tell Chef which application version to deploy by pushing it to Chef Server using knife. 

Do you have a specific use case you're trying to achieve? The only valid use case for me was to use a pre-existing script that relies on a command line parameter that is already available in GO as Environment variable. In this case what we've done is create a wrapper script that we deploy automatically on all our agents and use it as the base command and do the transformation. 

Michael Kunze

unread,
Jul 18, 2014, 1:24:20 PM7/18/14
to go...@googlegroups.com
On 18.07.2014 19:04, Marius Ciotlos wrote:
> We are using Pipeline parameters instead of Environment variables.
> It depends what you are trying to achieve, but for our needs we define a
> Template which relies on #{VARIABLE} which is defined at pipeline level
> as a Pipeline parameter.
>
> We do needed environment variable, we're relaying a lot on
> GO_PIPELINE_LABEL to extract app version and tell Chef which application
> version to deploy by pushing it to Chef Server using knife.
>
> Do you have a specific use case you're trying to achieve? The only valid
> use case for me was to use a pre-existing script that relies on a
> command line parameter that is already available in GO as Environment
> variable. In this case what we've done is create a wrapper script that
> we deploy automatically on all our agents and use it as the base command
> and do the transformation.

Hey,

thanks for the answer. I have an existing deploy script that relays on
an environment variable called VERSION to figure out which version it
should deploy. I can't change the script itself because it is used by
other teams as well.
I finally "solved" the problem by using a wrapper script which does
nothing more than:

export VERSION=${GO_PIPELINE_LABEL}
/bin/bash -x path/to/deploy_script.sh

Michael

Message has been deleted

Marius Ciotlos

unread,
Jul 18, 2014, 1:40:46 PM7/18/14
to go...@googlegroups.com, mku...@chaos-inc.de
You didn't need an extra script, you could have done it directly in the task. 
Below you can find a complex example of how i extracted from an environment variable something and injected back for a parameter for a script.

              <exec command="/bin/bash">
                <arg>-c</arg>
                <arg>python ./Script.py --params={\"BUILD_NUMBER\":\"$(echo $GO_PIPELINE_LABEL | sed 's/[\.[:alpha:]]*$//g' | rev | cut -d- -f1 | rev)\"} #{URL} #{JOB}</arg>
              </exec>
Reply all
Reply to author
Forward
0 new messages