How to pass parameters to Hudson job's shell commands - jenkins

I have a Hudson job that execute shell script on a remote server.
Its shell command is:
/usr/bin/deployWar.sh ${warfileName}
I marked this build as parameterized, and added a string parameter:
name: warFileName
default value: none
description: name of war file
When I run it, the parameter gets assigned, but it get passed into the shell script.

Parameterized Build Jenkins plugin documentation states that
all the environment variables added by parameters are in upper case
In your case this should work:
/usr/bin/deployWar.sh ${WARFILENAME}

There is nothing wrong in your approach. How do you know it's not passed to the shell script? The console log will show the "execute shell" command line.
Please paste the whole console log in here

For me the accepted answer did not work. ${WARFILENAME} using the brackets did not find the parameter. The way it seems to work only for shell scripts execution in Jenkins is $WARFILENAME (without the brackets).
This tutorial helped.
Everywhere else in jenkins options used ${WARFILENAME}.

Related

In Jenkins, How can I pass Job parameter values to a shell script which runs inside 'Check job prerequisites' section?

In Jenkins, How can I pass Job parameter values to a shell script which runs inside 'Check job prerequisites' section?
In the attached image, as you can see I am executing a shell script in the jenkins job's prerequisites part but want to pass a few job parameters to the shell script. But not able to do so, since jenkins doesn't recognize these parameters in prerequisites section.
Try removing sh from the command.
Verify if the relative path provided is correct. Click on "?" to see how this option really works.
The variables you are passing, are these environment variables? Check if Jenkins recognizes these variables.

Paramertized Build - Jenkins Pipeline

Hoping someone can point me in the right direction.
I have just started to look at the Jenkins pipeline and am trying to work out how to trigger a parameterized build for a job that already exists using the Jenkinsfile
In my Jenkinsfile i have
node {
stage 'Build My Job'
build job: 'my-build'
}
I need to be able to pass a branch name from the Jenkinsfile config to the job that is running? If i am misunderstanding anything then please let me know
Thanks
Instead of starting with a Jenkinsfile, it's easier to start with a pipeline job in which you can directly edit the pipeline script. By clicking the 'Pipeline Syntax' link you can open the snippet generator, where you can generate the Groovy for a particular step:
This Snippet Generator will help you learn the Groovy code which can be used to define various steps. Pick a step you are interested in from the list, configure it, click Generate Groovy, and you will see a Groovy statement that would call the step with that configuration. You may copy and paste the whole statement into your script, or pick up just the options you care about. (Most parameters are optional and can be omitted in your script, leaving them at default values.)
In the configuration page select 'This project is parameterized' and sleect parameter type and enter parameter name
You can access this new parameter value in you jenkinsfile using 'env.parameterName'

Jenkins - How to read the environment variables in groovy post build step

I am trying to read the environment variables in Groovy Postbuild step. I am able to read the values of parameters passed to builds but unable to read the values of parameters which are set in one of my Execute Windows batch command.
In one example of my Execute Windows batch command I do this:
SET custom_param=myValue
if I use ${custom_param} in other jenkins steps/jobs, it gets my value. So I am sure it has the value. I just can't get it in groovy script
I have tried followings to do so, none of them have worked:
manager.envVars['custom_param']
build.buildVariableResolver.resolve('custom_param')
build.getEnvironment(listener).get('custom_param')
Any help here would be great
(Assuming you're not running your script in groovy sandbox)
Try the bellow:
build = Thread.currentThread().executable
String jobName = build.project.getName()
job = Hudson.instance.getJob(jobName)
my_env_var = job.getLastBuild().getEnvironment()["YOUR_ENV_VAR"]
Groovy Post build step run as separate process. It has access to environment as normal JVM process.
You could use EnvInject plugin as a a build step. Subsequent steps in build will able to read this via normal environment access (System.env in your groovy script)
When you set some custom variables in your "Windows command batch" step, these variables are available only during this Jenkins step.
Once Jenkins move on the next step, your variables are lost...
If you want to set some variables permanently, you can try to use the SETX command:
What is the difference between setx and set in environment variables in Windows?

Jenkins: using parameters in command used by ssh

I am trying to build a generic job that connects to a server and executes command.
I use the "Execute shell script on remote using ssh" job. The commands are taken from a "Text Parameter"
If i don't use the parameter but write the commands in the "Command" field, they are executed. However, when i use the parameter, ${SOME_COMMANDS}, the commands are not executed.
Should there be a way to use the text parameter in the command field?
I don't think you can use a parameter directly in the command field, but you could get around it by calling a shell script with a fixed name in the command field and then having the script dereference the SOME_COMMANDS variable.

How to get the jobname from jenkins

Is there a way to get the jobname for the current build in jenkins and pass it as a parameter to an ant build script?
Jenkins sets some environment variables such as JOB_NAME (see here) for more details on the variables set.
You can then access these in ant via ${env.JOB_NAME}.
Edit: There's also a little howto for environment variables on the same page here.
A similar issue, I was looking for job name for shell script.
In the 'Execute shell' > 'Command' textbox,
both the below worked for me:
echo $JOB_NAME
echo "$JOB_NAME"
You may set special variable for that based on global variable. Simple:
THEJOB="${JOB_NAME.substring(JOB_NAME.lastIndexOf('/') + 1, JOB_NAME.length())}"
Now $THEJOB is your job name
If you can run any Job, you can easily go to the Build section of that job and go to environment variables and see all the information there.
Nowadays there is an environment variable JOB_BASE_NAME which contains the last component of JOB_NAME.
For example: if JOB_NAME contains Cool_Jobs/Very_Cool_Jobs/The_Coolest then JOB_BASE_NAME will just contain The_Coolest

Resources