Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 614
TimmyD Avatar asked Apr 02 '14 09:04

TimmyD


People also ask

Where are go env variables stored?

The GOPATH environment variableIt defaults to a directory named go inside your home directory, so $HOME/go on Unix, $home/go on Plan 9, and %USERPROFILE%\go (usually C:\Users\YourName\go ) on Windows. If you would like to work in a different location, you will need to set GOPATH to the path to that directory.

Can a browser access environment variables?

Accessing Environment Variables in the browser.By default, environment variables are only available in Node. js code and are not available in the browser as some variables should be kept secret and not exposed to anyone visiting the site.

How do I get a list of environment variables?

To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.


1 Answers

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

like image 137
Charles Martin Avatar answered Jan 02 '23 23:01

Charles Martin