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

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.

Related

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

Passing parameter from the user to maven via Jenkins parameterized build

I'm trying to set up a Jenkins job that is parametrized with an svn revision number, called param.svn.revision, entered by a user.
During the execution, I want to launch a maven command like :
clean package -Dsvn.revision=$param.svn.revision
I tried a lot of thing instead of $param.svn.revision, like ${param.svn.revision}, "$param.svn.revision", "%param.svn.revision",... but nothing worked.
What have I to do to make it work ?
Thanks a lot,
Seb
Because this is an invalid shell expansion. Shell doesn't expect dots within variable names. You should remove the dots in the param name in order to successfully use it.

TeamCity - using setParameter to pass information between build steps

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).

Pass variable from Jenkins to Ruby script (Jenkins newbie)

I've pulled a few scripts into Jenkins for a proof of concept and think I'd like to move that direction for all of our scripts. Right now I keep an environment.rb file with my code (watir-webdriver, cucumber) which tells the script which environment we're testing and which browser to use (global variables). Jenkins fires off the script using rake.
I'd love to let the user choose the environment and browser through Jenkins 'choice' variable or similar, and then pass that to the script. While I see the framework in that for Jenkins and set up a choice list for environment, I'm having trouble determining what the next step is.
I could write to environment.rb, I could pass a variable to rake - I have many options for how to pass the information, I just need some assistance finding the first step to find the Jenkins way of accomplishing them. Google results and previous Stack questions weren't what I was looking for.
Thanks
Sure. Give the user either a text entry field a dropdown after telling Jenkins that this is a parameterized build. You'll give them a name, something like BuildEnvironment. Then when you call the build, you can pass these from the environment variables. For example, if you were using ANT, you'd add a line to the parameters that said environment = ${MyEnvironment} Jenkins will then pass the value along for your build tool to use.
There is a way to pass Jenkins Environment Variable to Ruby script. Please see the following example:
workspace_path = `echo $WORKSPACE`.strip # note the use of backticks
puts workspace_path
In the "echo $WORKSPACE".strip # the code work only if you replace quotes with backticks
This code example works in Jenkins on a Linux system.

Xcode variables

In Xcode, I know that you can get variables such as PROJECT_DIR to use in some situations, such as a run script build phase. I am wondering if it's possible to get the build type (i.e., Release or Debug). Any ideas?
The best source is probably Apple's official documentation. The specific variable you are looking for is CONFIGURATION.
Here's a list of the environment variables. I think you might want CURRENT_VARIANT. See also BUILD_VARIANTS.
They're not all documented. For example, you won't find ARCHIVE_PATH, MARKETING_VERSION (the version string set in Xcode) in Naaff's or smorgan's answer. These 2 are very common pieces of information someone would need! Here's a list of all of them I got: https://gist.github.com/ben-xD/c063b0ca2c33684de1eb379bb9d6405d
How I got them
I found the best way was to print them using set, I just wrote this including a method to list all the environment variables available.
Add this to your run script (either Archive post run script, or your build phases run script, etc.):
#!/bin/sh
exec > ${PROJECT_DIR}/environment_variables.log 2>&1
set
Look in environment_variables.log and you'll see them all.

Resources