26

I want to use the parameters that we define in the Jenkins job as arguments to the shell commands in the same job.

I have created a parameterized build with the following parameters:

high.version: 234
low.version: 220

I want to use these variables as arguments for the build's shell script:

/bin/bash /hai/mycode/scripts/run_script.sh high.version

How do I these parameters in the same job?

alex
  • 6,818
  • 9
  • 52
  • 103
IMRAN SHAIK
  • 615
  • 2
  • 6
  • 11

5 Answers5

20

Jenkins will create environment variables with the parameters' names.

The caveat here is that Jenkins will also do that for parameters that do not represent valid variable names -- those are difficult to access in bash. This is the case in your example, as bash variable names must not contain the . character.

The easiest solution is that you

  • rename your parameters, e.g. to high_version and low_version (which are valid bash variable names)
  • then use the corresponding variable names when calling your script

Example:

/bin/bash /hai/mycode/scripts/run_script.sh "$high_version"

If you cannot rename parameters to represent valid bash variable names (e.g., for usability reasons: Jenkins presents variable names to end users in the Web form for starting a build): you can still access such parameters by grepping for the parameter name in the output of the env command.

Alex O
  • 7,746
  • 2
  • 25
  • 38
17

What really helped me was Hudson: How to pass parameters to shell script

Solution: the variables are UPPERCASE even you define them in lowercase!

Community
  • 1
  • 1
Florian Straub
  • 826
  • 9
  • 18
  • 2
    It looks like meanwhile they changed it and the variables are case sensitive. – Florian Straub May 27 '19 at 15:00
  • 1
    Yes, as of 2.210 (at least, that's the version I'm running), parameters are turned into variables without modifying . That is, I created a parameter named "choice" and was able to `echo $choice` and see the value of my parameter. – mooreds Dec 23 '19 at 17:51
1

Use following syntax to pass jenkins parameter to shell script -

eg. YourScript.sh %JENKINS_PARAMETER% after that in your script,you can use that parameter like normal shell script command line parameter. eg. myParam = $1;

0

Have you try this?

echo "function hello() {   " > gg.sh
echo "echo \$1">> gg.sh
echo "}" >> gg.sh
echo "hello \$1"  >> gg.sh
chmod 777 gg.sh

./gg.sh $hello_version

Be careful of the variable name, dot is not that well supported, for detail, you can ref this. https://issues.jenkins-ci.org/browse/JENKINS-7180

Draken
  • 3,134
  • 13
  • 34
  • 54
Tim
  • 2,006
  • 2
  • 18
  • 25
0

It is not a good practice to have dot(.) in your parameters. You should either choose highVersion OR high_version as your param names.

As per your question, it seems that you're working with a Freestyle job but many devs coming here would also be interested in the Pipeline syntax as well, so I'm giving a solution to use params in Jenkins pipeline DSL. There are two ways you can use Jenkins parameters in the Jenkins Pipeline shell script -

  1. As a Shell parameter
stage('Test'){
  sh "/bin/bash /hai/mycode/scripts/run_script.sh $highVersion"
}
  1. As a Groovy parameter
stage('Test'){
  sh "/bin/bash /hai/mycode/scripts/run_script.sh ${params.highVersion}"
}

I would recommend to use a second method, as we're using groovy as a pipeline DSL.

Swaps
  • 1,450
  • 24
  • 31