Passing environment variables to a job

Follow

Comments

8 comments

  • Avatar
    Anju Antony

    Hi Brett



    You specify variables on an job by adding an <environmentvariables> section to the job definition.


    <job name="my-job">
    <environmentvariables>
    <variable name="FOO">bar</variable>
    <variable name="MULTIPLE_LINES">Variable values can have
    multiple lines (assuming that your operating system supports this correctly).</variable>
    <variable name="COMPLEX"><![CDATA[<complex
    values>]]></variable>
    </environmentvariables>
    ...
    </job>

    So in your case it would look something like below

    <jobs>
        <job name="defaultJob">
    <environmentvariables>
     <variable name="REVISION">${GO_REVISION}</variable>
    </environmentvariables>
          <tasks>
            <exec command="/bin/echo" />
          </tasks>
        </job>


  • Avatar
    Ian Bridson

    Hi Brett


    I think it is a syntax issue - The variable should be specified as %GO_REVISION% when passed in an argument. I can certainly use the revision variables when I specify them in this form

  • Avatar
    Brett Cave

    Hi Ian,


    Thanks for the comment. That would most likely be on a Windows platform. I forgot to mention that we are running a linux build environment.


    Anju: testing now.

  • Avatar
    Brett Cave

    Anju: I already had that config. Here is the relevant code snippet and output:


    Configuration:


    <pipeline name="Test">
      <materials>
        <svn url="http://svn.mycompany.com/svn/PROJECT/trunk/component" username="username" password="password" autoUpdate="false" materialName="svn_material" />
      </materials>
      <stage name="Test">
        <jobs>
          <job name="test">
            <environmentvariables>
              <variable name="REVISION">${GO_REVISION_SVN_MATERIAL}</variable>
            </environmentvariables>
            <tasks>
              <exec command="/bin/echo" args="REVISION: $REVISION" />
            </tasks>
          </job>
        </jobs>
      </stage>
    </pipeline>


    Output:


    [cruise] setting environment variable 'GO_REVISION_SVN_MATERIAL' to value '7187'
    [cruise] setting environment variable 'REVISION' to value '${GO_REVISION_SVN_MATERIAL}'
    REVISION: $REVISION


    As you can see, I have named my material. Go output indicates that it assigns GO_REVISION_SVN_MATERIAL
     a value of 7187 but that value is not assigned. I have tried with both ${VAR} syntax and $VAR syntax.

  • Avatar
    Rajesh Muppalla

    Hi Brett,


    Since echo command does not understand environment variables, its the shell thats does the substitution, the exec task needs to have the command set as "bash" with echo and its arguments as argument. So your exec task now looks like


    <exec command="/bin/bash" args="-c 'echo REVISION: $REVISION'"/> /* Notice the single quote around echo. That has stumped us (Go devs) a few times */


    This would print - REVISION: ${GO_REVISION_SVN_MATERIAL} 


    But I guess you want REVISION: 7187 to be output, so you can change the exec task to


    <exec command="/bin/bash" args="-c 'echo ${GO_REVISION_SVN_MATERIAL}'"/>


    FWIW, for your scripts (shell, ANT, NANT), the environment variables that Go sets are available like any other system environment variables.


    Hope this helps. Let me know if you need any further questions.


    Regards,


    Rajesh

  • Avatar
    Brett Cave

    Ok, great. Thanks Rajesh, that does work. I think it would probably be best to then write system scripts or project-specific scripts and execute them, rather that doing it all directly.


    What am I doing wrong here (this is declared within a <job> tag:


            <environmentvariables>
              <variable name="REVISION">${GO_REVISION_SVN_MATERIAL}</variable>
            </environmentvariables>


    Output:


    [cruise] setting environment variable 'GO_REVISION_SVN_MATERIAL' to value '7187'
    [cruise] setting environment variable 'REVISION' to value '${GO_REVISION_SVN_MATERIAL}'
  • Avatar
    Rajesh Muppalla

    Hi Brett,


    In this case, Go is just assigning the value ${GO_REVISION_SVN_MATERIAL} to the REVISION environment variable and not substituting it. Go currently does not support recursive environment variable substitution. 


    To solve your problem, you can use the env. variable GO_REVISION_SVN_MATERIAL in your scripts directly.


    However, if your scripts need the REVISION env. variable to be set and its value to be same as GO_REVISION_SVN_MATERIAL you can use this exec task (or do it in separate script)


    <exec command="/bin/bash" args="-c 'REVISION=${GO_REVISION_SVN_MATERIAL}'"/> 


    Hope this helps.


    Rajesh

  • Avatar
    Brett Cave

    Ok great, thanks Rajesh.

Please sign in to leave a comment.