29

I must be doing something differently than what was asked and answered here because the solution does not appear to be working for me: TeamCity, passing an id generated in one build step to a later build step

I want use a string generated by one build step in a final build step. So far I have set up an environmental variable called "TEST" that is empty. Both build steps use the Command Line Runner.

Build Step #1:

\##teamcity[setParameter name='env.TEST' value='test']

Build Step #2:

echo $TEST

echo %env.TEST%;

Placeholder for now, but if I could access the test string ('test') set in Build Step 1 I would be so happy.

Community
  • 1
  • 1
user1467961
  • 291
  • 1
  • 4
  • 6

6 Answers6

48

you need to echo that string, e.g.

echo "##teamcity[setParameter name='env.TEST' value='test']"
dmitko
  • 2,607
  • 2
  • 21
  • 15
  • 3
    This was exactly what I was looking for - the docs don't indicate that it needs to be echoed out, much less in quotes. After that, my next step had `$TEST` available. – John C Jun 04 '15 at 16:54
  • 14
    just to re-iterate, the variable is only updated from the next TeamCity step onwards, the changes are not reflected in the current step – sawe Dec 03 '18 at 07:18
10

What I found is that with long values, as soon as TeamCity is breaking down the log output into two separate lines echo will not work anymore - you have to use Write-Host instead.

Write-Host "##teamcity[setParameter name='env.TEST' value='test']"

This should always work, just a side note - this value will be available only on subsequent build steps.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 4
    This does not address if you are not using windows. PS commands do not work on Unix agents –  Nov 14 '17 at 19:51
6

I think you have an extra "\" in there. Try removing that and add double quotes around it and it should work.

 "##teamcity[setParameter name='env.TEST' value='test']"

If it doesn't work try using Powershell runner type as I'm using that for setting it and it works.

Adarsh Shah
  • 6,755
  • 2
  • 25
  • 39
6

To expand on the above answers, with powershell it would look like so in build step 1:

Write-Host "##teamcity[setParameter name='env.TEST' value='$test']"

...and you can use the value like this in step 2:

echo %env.Test%

Also as a note, you'll have to set env.Test in the TC build parameters to be equal to something. I just used a space since I know the value will be set via ps script. Hope this helps.

Japster24
  • 1,486
  • 3
  • 21
  • 23
6

It has to be printed to STDOUT, I use cat with heredoc to avoid having to escape single quotes in the event of using variables to dynamically set config parameters. What is heredoc?

MYVARNAME=MYVALUE
cat <<EOF
##teamcity[setParameter name='myConfParameter' value='$MYVARNAME']
EOF

Result:

##teamcity[setParameter name='myConfParameter' value='MYVALUE']

Documentation

  • so you just downvoted without exchanging any more information about the matter? @PhateeP –  May 24 '18 at 16:23
  • sorry for downvote, but it simply does not work. It prints > and then > EOF in another line in console. It does not echo "##teamcity[setParameter name='myConfParameter' value='$MYVARNAME']" in to the console. Can you bring more info? I ll be happy to upvote if it realy works – Phate P May 24 '18 at 16:30
  • `>` is printed by shell when you do multiline stuff like quotes, heredoc etc. you shouldn't use `>` –  May 24 '18 at 16:45
  • @PhateeP try again,I edited my answer to make it easier to understand –  May 24 '18 at 16:47
  • I've just checked it and now it prints nothing at all. – Phate P May 24 '18 at 16:50
  • can you share your code in a github gist or pastebin maybe? @PhateeP . I tried again it works https://imgur.com/a/SwfNZ6t –  May 24 '18 at 16:51
  • https://pastebin.com/xPmfaDwz I execute it in Build Steps, Runner type: Command Line. I also have defined that param in Parameters so it could be modified. Trying that code without defining param behaves exacly the same - prints nothing – Phate P May 24 '18 at 16:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171715/discussion-between-sdkks-and-phatee-p). –  May 24 '18 at 17:06
  • Options with "cat" was the only one working for me... echo did not catch it up ! – Viji Jul 16 '21 at 09:01
0

Here is official ticket about addition double quotes and echo (Write-Host - OS dependency).

dikkini
  • 1,184
  • 1
  • 23
  • 52