Environment variables in custom commands...

475 views
Skip to first unread message

Jeff

unread,
Mar 21, 2016, 2:20:56 PM3/21/16
to GoCD User Mailing List
I've seen a few discussions on this but can't seem to wrap my head around what GoCD is doing under the covers in order to get this to work.  

I am running a couple of groovy scripts and/or remote ssh commands that I want to parameterize using an environment variable defined at the pipeline level.

In this case I want to define an environment variable called "DEPLOY_ENV":

Inline image 1 and have it substituted in the locations for both a '/bin/bash' and 'groovy' command noted below:

<exec command="/bin/bash">
  <arg>-c</arg>
  <arg>sshpass -mypass ssh -o StrictHostKeyChecking=no user@salt-server "mkdir -p /srv/pillar/$DEPLOY_ENV/myservice" &amp;&amp; sshpass -mypass scp ./myservice_materials/pillar/$DEPLOY_ENV/* myuser@salt:/srv/pillar/$DEPLOY_ENV/myservice</arg>
  <runif status="passed" />
</exec>
<exec command="groovy">
  <arg>./continuous-deployment/groovy/src/gocd/OrchestrateSaltApi.groovy</arg>
  <arg>-saltUrl</arg>
  <arg>https://salt:8000</arg>
  <arg>-payload</arg>
  <arg>./myservice_materials/pillar/$DEPLOY_ENV/salt-api_request.json</arg>
  <runif status="passed" />
</exec>

I've tried the following variations with the same error:

$DEPLOY_ENV
${DEPLOY_ENV}
${env.DEPLOY_ENV}
$(go.DEPLOY_ENV}

I don't really care that it is an environment variable, as long as I can define it once at the pipeline level and reuse it in multiple stages/jobs/tasks.

Any guidance is much appreciated!

--
Jeff Vincent
See my LinkedIn profile at:
http://www.linkedin.com/in/rjeffreyvincent

Aravind SV

unread,
Mar 21, 2016, 4:51:50 PM3/21/16
to go...@googlegroups.com
Hello Jeff,

You didn't say what error shows up for the first one. I can guess what happens with the groovy command. Once I know what error the first one shows, I'll respond with some suggestions for both.

Cheers,
Aravind

--
You received this message because you are subscribed to the Google Groups "go-cd" group.
To unsubscribe from this group and stop receiving emails from it, send an email to go-cd+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeff

unread,
Mar 21, 2016, 6:03:09 PM3/21/16
to GoCD User Mailing List
The error depended on how I split the command up and where I tried putting the quotes :).

I actually found that using a parameter works better for my case as it seems GoCD does the substitution for parameters before running the command instead of trying to figure out how to get the shell substitution to work right.

So I do have it working for both using parameters.

Thanks!

Aravind SV

unread,
Mar 21, 2016, 6:39:10 PM3/21/16
to go...@googlegroups.com
I'd written a little response which started like this: :)

If you really don't mind if it's not an environment variable, then making this a parameter and using it as #{DEPLOY_ENV} might be the quickest way for you to go forward with this.

The difference between using an environment variable and a parameter is that a parameter is replaced by the server, during config save. So, the agent will never see the #{DEPLOY_ENV} param itself. When used as an environment variable, the server (and agent) do nothing special to it and pass it along to the command. In case of bash -c, it replaces $DEPLOY_ENV or ${DEPLOY_ENV}.

Here is some (new) documentation about environment variables in GoCD that might help.

Using bash -c would have made the groovy command work. The ssh one could be trickier, depending on what ssh allows to be sent to the server (SendEnv config in ssh might be useful). But, since you're using parameters, you don't need anything. Glad it works for you.

Cheers,
Aravind

Reply all
Reply to author
Forward
0 new messages