Job Parameters
Lab Environment
In this lab, we have the following Linux machines, and we will use a local computer (laptop) to connect to them.
- jenkinsmaster
You can choose to set up a virtual machine on your computer or create instances in the cloud. In this case, EC2 instances are used.
Note that for this lab, we'll only be using jenkinsmaster and you can disregard the other tstsvrs and jenkinsslave1 for now.
Setting Up Jenkins
If you have your Jenkins server already setup, you can skip this section. There's an option to manually install Jenkins on a Linux machine or you can also use Ansible playbooks to perform the entire installation of Jenkins on this machine.
To setup Jenkins:
Create the Job with Parameters
Create a simple test job that will echo out a text. Also create a folder named Test-builds. To create the folder
New Item > enter a folder name > Folder > Ok
Inside the folder, repeat the same process by clicking the New item then entering a project name but this time select Freestyle project.
In the next page, configure the job. Enter a description then check the box for This prooject is parameterized. Click Add parameter then in the dropdown menu, select String parameter.
On the name field, let's set a variable called FIRST_NAME. Add a second parameter by clicking the Add parameter again. In the second one, set a variable called LAST_NAME.
Scroll down to the Build section then click Add build step. Select Execute shell.
In the Command field, enter the following line. Click Save afterwards.
echo "Hello there, $FIRST_NAME $LAST_NAME"
Back on the job page, you should now see a new tab on the left panel, Build with Parameters. Click this tab. Here we can specify any "first name" and "last name" before running the job. When the job is ran, this parameters are passed to the variables we set and then used by the job.
Set the names to "John Smith" then click Build.
Under the Build History section on the left panel, select the most recent build which will always be the first one on the list.
Click Console Output on the left panel.
Repeat the steps by going to the job page again and selecting Build withh Parameters. This time, use the names "Jane" and "Doe" then click Build.
On the console output, you should now see a new name printed.
List Parameter
On the job page, click Configure on the left panel.
Click Add parameter to add a third one then select Choice parameter.
In the Choices field, enter the days of the week.
Next, let's modify our shell command. Afterwards, click Save.
echo "Hello there, $FIRST_NAME $LAST_NAME"
echo "How was your $WEEK?"
Back on the job page, click Build with Parameters. Enter any name for the FIRST_NAME
and LAST_NAME
. We now see a third one named WEEK which has a dropdown menu of the days of the week. Select a day then hit Build.
In the console output of the most recent job, we now see a new message.
Basic Logic and Boolean
Click Configure and Add parameter. From the dropdown menu, select Boolean Parameter.
Set the variable DISPLAY
for this parameter.
On the Build section, replace the command with this:
if [ "$DISPLAY" = "true" ]; then
echo "Hello $FIRST_NAME $LAST_NAME. How was your $WEEK?"
else
echo "Can't display message, please ensure you enabled display"
fi
Click Save afterwards.
Back on the job page, click Build with Parameters. Set the following values and let the box for Display unchecked. Hit Build.
On the console output of the latest build, you should see the "error" message saying it can't display the message.
Go back to the job page and click Build with Parameters again. This time, check the box for Display then click Build.
Checking the console output of the latest build, we now see the names displayed.