Passing a Parameter Downstream
Overview
This is a continuation of the previous lab on Upstream and Downstream Projects
From the previous lab, we have two projects currently inside Test-builds.
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:
Install the Parameterized Plugin
Before we proceed, we need to install a plugin.
Manage Jenkins --> Manage Plugins --> Available --> Search for 'Parameterized Trigger'
Check the box beside the plugin and hit Download now and install after restart. In the next page, also check the box for Restart Jenkins when installation is complete and no jobs are running.
Once it's done, you will need to log in again.
Configure the Downstream Job with the Parameter to be received
Go to your job-downstream again and click Configure. Under General, click
This project is parameterized --> Add Parameter --> String Parameter
Set a variable called IMPORTANT_PARAM
:
In the Build Triggers section, make sure that none of the boxes are checked.
Under the Build section, change the command to:
echo 'I am the downstream proect, triggered by the first one'
echo 'Received this current build number from Upstream:' $IMPORTANT_PARAM
Hit Save afterwards.
Configure the Upstream Job with the Parameter to be passed
Exit out to the folder Test-builds then go to your job-uptream again and click Configure.
Under Build step, edit the Execute shell command:
echo 'I am the upstream project'
echo 'Current build number of upstream project:' $BUILD_NUMBER
Still in Build step, add a second step.
Add Build Step --> Trigger/call builds on other projects --> Projects to build --> job-downstream
Then add parameters,
Add parameter --> Predefined parameters
In the Parameters field, enter the variable we set in the downstream project. Set the variable to the build number. Click Save afterwards.
IMPORTANT_PARAM=$BUILD_NUMBER
Click Build now to test if it works. Then click the latest build under the Build History in the left panel and click the Console Output.
We see this line:
Current build number of upstream project: 24
Exit out to the folder Test-builds then go to your job-downtream again. Then click the latest build under the Build History in the left panel and click the Console Output.
We see this line:
Received this current build number from Upstream: 24