Search code examples
thoughtworks-go

am i able to reference environment variables in go templates?


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


Solution

  • 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