I try to create a variable with the version of a project in a GoCD job.
My project is built with maven. The version of the project is set on the pom.xml file (present on the git repository).
Maven allow to retrieve the version of the project with command :
mvn help:evaluate -Dexpression=project.version
So I tried to put the output of this command in a linux variable :
my_version=${mvn help:evaluate -Dexpression=project.version}
And then use it to push the project on a docker repository :
docker push my_project:$my_version
The problem is that GoCD seems to have his own interpreter (see https://docs.gocd.org/current/faq/dev_use_current_revision_in_build.html ).To use a variable, I must do something like this :
sh -c docker push my_project:$my_version
But I can't find a way to set a linux variable.
Is it something that is ok to do with GoCD ? What is the right way to do this ?
GoCD doesn't maintain a single shell session for the entire job. So even if you use sh export MY_VERSION=... and try to use that in the next task within the same job it will not work as expected.
To solve this problem the best way is to wrap the entire set of commands you want to perform within a shell script and execute that shell script as a task. Given the entire shell script would be executed within a single shell instance anything variable you define can be used.
Related
I encounter the issue that I have a parameterized project with several string parameters, e.g., "VARIABLE_A", and that Jenkins doesn't know them anymore when executing a shell script in the Build section, i.e., there is simply no value.
This is very bizarre and I am convinced this should work using a string parameter (variable) in these ways:
"echo $VARIABLE_A"
"python3 $PATH/abc.py $VARIABLE_A"
...
but there is no values anymore.
echo-command does not print anything and the Python script misses the value as VARIABLE_A is apparently empty.
Perhaps someone has an idea ?
Thanks
Rainer
PS
Jenkins version is "2.277.2".
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.
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
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.
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.