TeamCity - using setParameter to pass information between build steps - teamcity

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.

you need to echo that string, e.g.
echo "##teamcity[setParameter name='env.TEST' value='test']"

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.

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.

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.

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

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

Related

TeamCity build number override from script

I have a problem with setting up the build number for the build from the script. I use the shell command in TeamCity build step:
echo "##teamcity[buildNumber '%build.counter%.%tp.environment%.%tp.environment.contentSpreadsheetSelectOverride%']"
And it results in build number:
\''868.car_hdev.Dev'\'
When I perform the test:
echo '%build.counter%.%tp.environment%.%tp.environment.contentSpreadsheetSelectOverride%'
It gives me the right output:
868.car_hdev.Dev
I believe I do everything as instructed here: https://www.jetbrains.com/help/teamcity/service-messages.html#Reporting+Build+Number
Do you have any idea why there are additional characters in the resulting build number?
Turns out I had to change:
#!/bin/sh -x
To:
#!/bin/bash
In the build step script.

Replace a specific character in Jenkins shell step

I have a variable $svn_tag in a Jenkins job.
Its value is branches/sprint-77.
I want the output branches-sprint-77, but I didn't get any output.
I tried these two methods:
$svn_tag|tr "/" -
${svn_tag////-}
It is giving output in bash script, but it is not working in the Jenkins job.
What am I doing wrong?
Your first approach does not make sense, not the least because you try to run `$svn_tag´ as a command. Your second approach works for me:
svn_tag=branches/sprint-77
echo ${svn_tag////-}
prints branches-sprint-77.
For the safe side, you could also check your bash version, by doing a
echo bash=$BASH_VERSION
though I don't think that this feature is version-dependent. Mine is 4.4.12(3)-release.
Use code like below
${svn_tag.replaceAll('/', '-')}

Jenkins run a failing test n times

I'm trying rerun a test n times on failure in Jenkins. I've seen Naginator, but my team would prefer not to add additional plugins.
(Note: we use EnvInject to inject environmental variables into the build process)
The next idea was to keep a variable with the number of times to retry, and decrement it on each new build. There was a stack overflow link (that I'm struggling to find at the moment) suggesting a groovy script that looks like this
def map = [:]
Int newRetries = RETRIES.toInteger() - 1
map.put(“RETRIES”, newRetries)
return map
However, groovy scripts in the "prepare an environment to run" section need admin privilages (which again my team would prefer to avoid).
The next idea was to use a property file and just do something along the lines of echo "RETRIES=$((RETRIES-1))" > env.properties and add an Inject Environmental variable step that reads in env.properties.
The problem is that within our Jenkins bash script echo "RETRIES=$((RETRIES-1))" prints "RETRIES=$((RETRIES-1))"
In a local terminal I can run
RETRIES=5
echo "RETRIES=$((RETRIES-1))"
> RETRIES=4
, but in Jenkins, RETRIES-1 doesn't get evaluated. Do any of you have an idea of why?
So I found 3 mistakes that I was making.
Jenkins pipelines execute a single step in parallel. This means you can't write to and read from a file in different portions of the same step without reading from the unupdated file (in practice) (From this stackoverflow Override environment variable created locally in Jenkins)
The default shell on Jenkins is /usr/bin/sh which isn't necessarily bash. I explicitly ran my script with bash -c "echo $Var" (the real issue here was that the remote machines were windows, oops)
My pipeline was failing before reading in the updated variable value from the file. I've moved the reading step earlier in the pipeline.

How to fetch the value of environment variable in Jenkins

I'm trying to inject an environment variable at build step Invoke Maven whose value was set at pre-build step through Execute Shell
#!/bin/bash
ipAddressHub=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' selenium-hub)
echo $ipAddressHub
echo 'ipAddress=$ipAddressHub' > ipAddress.properties
Now I want to fetch the value of ipAddress stored in ipAddress.properties. I'm using Inject environment variables after Execute Shell and provide ipAddress.properties in Properties File Path field (not sure if that's the right way) and then i use build step Invoke Maven Artifactory and provide the command below.
clean install -DipAddress=${ipAddressHub} -Denv=${env} -Durl=${appURL} -DserverIP=${ipAddress}
But i don't get the value in serverIP, instead i get ${ipAddressHub} in console. I know i'm making some mistake, can anybody point out what's the correct way?
I hadn't used the plugin (at least not for a while), and I was going to suggest that you are just referencing it incorrectly?
I believe if you are adding it as an environment variable (and you can check it is adding by clicking on Environment Variables on the left side of the build screen).
You should be able to reference it like below?
${env.ipAddressHub}
This is untested though. Just going from memory.
Did some browsing and found an answer to it.
You can embed variables only in double-quoted strings. So the problem was
echo 'ipAddress=$ipAddressHub' > ipAddress.properties
changed it to
echo 'ipAddress='"$ipAddressHub"' > ipAddress.properties
And it worked like a charm

How to use Teamcity commands (##teamcity[...])?

I want to pass some information to another build step. E.g. for build tagging. How do I do that?
I've tried ##teamcity[setParameter name='xxx' value='111'] in my script, but it doesn't seem to do anything.
Well, first you need to define custom parameter in Build configuration -> Parameters. Then you should set it like in question, but with one nuance: you should echo command! And there isn't a word about that in docs :(
In the end, you need to do this: echo ##teamcity[setParameter name='xxx' value='111'] in your script, and then, in next build step you could use it as usual Teamcity variable: %xxx%.
P.S. xxx would be initialized only in next build step, so don't use it for anything else but setting value in this build step.

Resources