Distributing a Jenkins Build

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

In this hands-on lab, we will create a build agent on a second server and then create a build that runs only on that server. This will demonstrate the use of labels for the build.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Configure SSH on the Remote Agent

After giving the lab an extra two minutes, log in to the Jenkins controller via SSH using the credentials provided on the hands-on lab overview page. Confirm that you see a file named SERVER-READY in the cloud_user‘s home directory using the ls command. Once the file shows, you can use SSH from the controller node to the agent worker node.

On the agent worker node, create the directory for the user’s home:

[cloud_user@agent ]$ sudo mkdir /var/lib/jenkins

Add the user, assigning the home directory:

[cloud_user@agent ]$ sudo useradd -d /var/lib/jenkins jenkins

Make the user the owner of their home directory:

[cloud_user@agent ]$ sudo chown -R jenkins:jenkins /var/lib/jenkins

Create an .ssh directory for the jenkins user:

[cloud_user@agent ]$ sudo mkdir /var/lib/jenkins/.ssh

Run ssh-keygen. Hit Enter to accept defaults until it completes.

Copy the contents of ~/.ssh/id_rsa.pub to the file /var/lib/jenkins/.ssh/authorized_keys

[cloud_user@agent ]$ cat ~/.ssh/id_rsa.pub # Copy the output

[cloud_user@agent ]$ sudo vim /var/lib/jenkins/.ssh/authorized_keys # Paste the output of cat and save

We need the contents of id_rsa, as this is the private key that we will paste into Jenkins:

[cloud_user@agent ]$ cat ~/.ssh/id_rsa

Copy and save that command’s output so we can use it later. After that, use exit to close out of the worker agent session to go back to the Jenkins controller session.

On the Jenkins controller, create an .ssh directory on the controller in the jenkins directory:

[cloud_user@jenkins ]$ sudo mkdir /var/lib/jenkins/.ssh

Copy the known_hosts entry over from the .ssh directory in /home/cloud_user to the jenkins user’s .ssh directory:

[cloud_user@jenkins ]$ sudo cp ~/.ssh/known_hosts /var/lib/jenkins/.ssh
Set Up the Node on the Jenkins Controller

Log in to the Jenkins controller web interface using http://<PUBLIC_IP_OF_JENKINS_CONTROLLER>:8080. You will be prompted to enter the initial Jenkins admin password. On the Jenkins controller, to show the initial Jenkins admin password, type:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

After that, install suggested plugins, create a student user in Jenkins, and set the password to be the same as your Jenkins server ssh password. You can use a fake email address for the E-mail address field. Choose Save and Continue, Save and Finish, and Start using Jenkins.

On the Jenkins dashboard, go to Manage Jenkins and select Manage Nodes and Clouds.

On this page, click New Node, and then choose the Node name as worker1. Select Permanent Agent, and then click Create.

Fill out the following web form with these values:

  • # of Executors: Enter 2.
  • Remote root directory: Enter /var/lib/jenkins.
  • Labels: Enter linux.
  • Launch method: Select Launch agents via SSH.
  • Host: Enter the private IP of the worker agent.
  • Credentials: Click Add, and enter Jenkins.
  • Kind: Select SSH Username with private key.
  • Description: Enter jssh.
  • Username: Enter jenkins.
  • Private Key: Enter it directly, and click Add.
  • Paste in the RSA private key for the worker agent.
  • Click Add.
  • Choose the jenkins (jssh) option under Credentials.
  • Leave any fields alone that weren’t mentioned here, and click Save.

Give it a moment, and then click Refresh status to see that worker1 is in sync. If you have a problem, click worker1, and then click Log for more information.

Test a Remote Build

We need to configure the master to use labels, to ensure default builds run on the remote.

Go back into the main Jenkins dashboard screen. In the left-hand menu, click Manage Jenkins.

Click Configure System, and set the following values:

  • On the Labels line, enter jenkins.
  • Set Usage to Only build jobs with label expressions matching this node.
  • Click Save.

Back out on the main Jenkins screen, click New Item. Enter an item name of test, click on Freestyle project, and then click OK.

On the next screen, scroll down to the Build Steps section, and choose Execute shell.

In the Command box, enter this:

hostname > location.txt

Now, in Post-build Actions, click the Add post-build action dropdown, and select Archive the artifacts. Paste location.txt in the Files to archive box. Click on Advanced, and check the box next to Fingerprint all archived artifacts.

Click Save.

Run the build by clicking Build Now in the left-hand menu, then click on the build number to get into the build itself. If we watch the Console Output, we’ll see the build process happening.

Go back to the test environment (using the breadcrumb trail navigation at the top of the screen), and we will see location.txt listed in the Last Successful Artifacts section. Open the view link that’s next to it in a new tab or window.

Additional Resources

Your company has decided that they need to scale out their Jenkins installation, and that running projects on the controller is no longer desirable. You have been tasked with configuring a build agent on a second server.

It should have the following configuration:

  • The node name should be worker1.
  • It should connect via SSH with keys.
  • The root directory should be /var/lib/jenkins.
  • The remote user should be jenkins.
  • The remote should have two executors.
  • All builds should be configured to run on the agent by default.
  • Ensure that the build is running on the agent by issuing a hostname command.
  • Verify that the agent builds are able to be archived and fingerprinted.

At the start of the lab,after giving the lab an extra two minutes, log in to the Jenkins Controller via SSH, using the credentials provided on the hands-on lab overview page. Confirm that you see a file named "SERVER-READY" in the cloud_user's home directory using the ls command. Once the file shows, you can use SSH from the controller node to the agent worker node to begin the activity.

To access the Jenkins dashboard, log in to the Jenkins controller web interface using http://<PUBLIC_IP_OF_JENKINS_CONTROLLER>. You will be prompted to enter the initial Jenkins Admin password. On the Jenkins controller, to show the initial Jenkins Admin password, enter:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

After that, install suggested plugins, create a student user in Jenkins, and set the password to be the same as your Jenkins server ssh password. You can use a fake email address for the E-mail address field. Choose Save and Continue, then Save and Finish, and then Start using Jenkins.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?