Passing parameter from the user to maven via Jenkins parameterized build - maven

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.

Related

Jenkins "forgets" parameters when executing a shell script in Build section

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

Put output of a command in a variable GoCD

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.

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.

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.

Resources