6

Trying to reference environment variables from go templates but i don't think it's possible? Can't see anything in the documentation and the examples I've pawed through use parameters aplenty but never an environment variable. What I'd like is something like

<templates>
<pipeline name="TestEcho">
  <stage name="Echo">
    <jobs>
      <job name="Echo">
        <tasks>
          <exec command="echo ${SOME_ENVIRONMENT_VARIABLE}">
          </exec>
        </tasks>
      </job>
    </jobs>
  </stage>
</pipeline>
<templates>

Thanks in advance!

Tim

TimmyD
  • 81
  • 1
  • 11

3 Answers3

6

Yes, you can !

You should use %SOME_ENVIRONMENT_VARIABLE% instead of ${SOME_ENVIRONMENT_VARIABLE} (on a windows agent).

I guess you are using a windows agent. Thoughtworks' documentation is Linux focused, which is why their example is not working for you.

You can use all Go Standard environment variables in your tasks.

You can set environment variables at :

  • the environment level
  • the pipeline level (override environment level)
  • the stage level (override pipeline level)

You can use all those environment variables in your task :

<pipeline name="TestEcho">
  <stage name="Echo">
    <jobs>
      <job name="Echo">
        <tasks>
          <exec command="echo %SOME_ENVIRONMENT_VARIABLE%">
          </exec>
        </tasks>
      </job>
    </jobs>
  </stage>
</pipeline>

You can set the environment variable at the environment level :

<environments>
  <environment name="SomeEnvironment">
    <environmentvariables>
      <variable name="SomeVariable">
        <value>SomeValue</value>
      </variable>
    </environmentvariables>
    <pipelines>
      <pipeline name="TestEcho" />
    </pipelines>
  </environment>
</environments>

You can set the environment variable at the pipeline level :

<pipeline name="TestEcho">
  <environmentvariables>
    <variable name="SomeVariable">
      <value>SomeValue</value>
    </variable>
  </environmentvariables>
  <stage name="Echo">
    <jobs>
      <job name="Echo">
        <tasks>
          <exec command="echo %SomeVariable%">
          </exec>
        </tasks>
      </job>
    </jobs>
  </stage>
</pipeline>

You can set the environment variable at the stage level :

<pipeline name="TestEcho">
  <stage name="Echo">
    <jobs>
      <job name="Echo">
        <environmentvariables>
          <variable name="SomeVariable">
            <value>SomeValue</value>
          </variable>
        </environmentvariables>
        <tasks>
          <exec command="echo %SomeVariable%">
          </exec>
        </tasks>
      </job>
    </jobs>
  </stage>
</pipeline>

You can override an environment variable :

<pipeline name="TestEcho">
  <environmentvariables>
    <variable name="SomeVariable">
      <value>Value1</value>
    </variable>
  </environmentvariables>
  <stage name="Echo">
    <jobs>
      <job name="Echo">
        <environmentvariables>
          <variable name="SomeVariable">
            <value>Value2</value>
          </variable>
        </environmentvariables>
        <tasks>
          <exec command="echo %SomeVariable%"><!-- Write Value2 -->
          </exec>
        </tasks>
      </job>
      <job name="Echo2">
        <tasks>
          <exec command="echo %SomeVariable%"><!-- Write Value1 -->
          </exec>
        </tasks>
      </job>
    </jobs>
  </stage>
</pipeline>

Source that helped me

Charles Martin
  • 420
  • 4
  • 12
  • Tom, can you tell me more ? What did you try ? How is your pipeline ? How did you set your environment variable ? – Charles Martin Feb 23 '15 at 12:33
  • In the end I found I was able to refer to environment variables using ${VAR} or $VAR (but not %VAR% as you suggest) - but not between different stages in the pipeline, so it didn't do me much good anyway! – Tom McIntyre Feb 23 '15 at 15:22
  • @TomMcIntyre, i added some details about how to set environment variables. Could you check if your configuration is similar ? – Charles Martin Feb 23 '15 at 15:56
  • @TomMcIntyre, i bet you are using a linux agent. The syntax i suggested is for windows only. I am pretty sure ${SOME_ENVIRONMENT_VARIABLE} should work on Linux. – Charles Martin Mar 02 '15 at 11:11
2

Currently, this is not possible and only parameters can be referenced, using the #{parameter_name} syntax

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Thanks for the answer. Yes found this out the hard way! In the end doesn't cause us too much pain that this is the case. Though it did cause me a lot of pain trying to make it happen! – TimmyD May 01 '14 at 13:13
  • 2
    You are wrong. This is possible, using the %environment_variable_name% syntax. – Charles Martin Jun 15 '14 at 07:43
1

Yes, this is possible. You just need to run it as a custom command task, using /bin/bash as the command, and -c as the first parameter to tell bash to expand the parameters.

So your pipeline config would look like this:

<templates>
<pipeline name="TestEcho">
  <stage name="Echo">
    <jobs>
      <job name="Echo">
        <tasks>
          <exec command="/bin/bash">
            <arg>-c</arg>
            <arg>echo ${SOME_ENVIRONMENT_VARIABLE}</arg>
          </exec>
        </tasks>
      </job>
    </jobs>
  </stage>
</pipeline>
<templates>

See here for similar: http://support.thoughtworks.com/entries/22628904-Custom-environment-variables-referencing-other-variables

Mark Crossfield
  • 124
  • 1
  • 6