How to fetch the value of environment variable in Jenkins - bash

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

Related

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.

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.

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.

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