62

New to TeamCity. I have multiple build steps. Step 3 generates an id that is needed in step 4. What is the best way to pass the id (a string) between step 3 and step 4? The build steps are written in Ruby. Can I set an environment variable?

Dave Schweisguth
  • 31,916
  • 10
  • 88
  • 112
Mike Jr
  • 1,549
  • 3
  • 13
  • 20

1 Answers1

97

Yes, you can set an environment variable in one build step and use it in the following step. You will need to use a service message in your build script as described here http://confluence.jetbrains.net/display/TCD65/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-AddingorChangingaBuildParameterfromaBuildStep to dynamically update a build parameter, which you can use in the next step. Note, that it won't be available in the step that generates it, only in the next one.

Note that to set the variable, it must be written out somehow (echo for bash command-line, write-host for Powershell), in quotes. Example:

echo "##teamcity[setParameter name='env.ENV_AAA' value='aaaaaaaaaa']"

and to use this variable write %env.ENV_AAA% in the box in the next build step (Atleast in TeamCity 9.1.7))

Gi0rgi0s
  • 1,532
  • 2
  • 17
  • 28
Maria Khalusova
  • 1,240
  • 10
  • 10
  • 2
    Maria, thank you for the pointer. I set the following in the ruby program executed by my TeamCity build step 3 puts "##teamcity[setParameter name='env.USER_DATETIME' value='#{DateTime.now}']" and I set the following in the ruby program executed by my TeamCity build step 4 puts "... USER_DATETIME = '#{ENV['USER_DATETIME']}'" And it worked! – Mike Jr Nov 23 '11 at 21:02
  • I am not able to retrieve parameter value in next step without pre-defining it in `Build Configuration Settings->Parameters` section. According to documentation: https://confluence.jetbrains.com/display/TCD9/Build+Script+Interaction+with+TeamCity (see "Adding or Changing a Build Parameter") @MikeJr: This did not work for me: `USER_DATETIME = '#{ENV['USER_DATETIME']}'"` This worked: `echo %env.USER_DATETIME%` Teamcity version is 9.1.6 (build 37459) – antonbormotov Apr 07 '16 at 09:20
  • 5
    Updated documentation for anyone looking at this now: https://confluence.jetbrains.com/display/TCD10/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-changingBuildParameterAddingorChangingaBuildParameterfromaBuildStepAddingorChangingaBuildParameter – BoredAndroidDeveloper Jan 09 '17 at 18:30