- A Cloud Guru
Distributing a Jenkins Build
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.
Table of Contents
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
cloud_user's home directory using the
lscommand. 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
.sshdirectory for the
[cloud_user@agent ]$ sudo mkdir /var/lib/jenkins/.ssh
ssh-keygen. Hit Enter to accept defaults until it completes.
Copy the contents of
~/.ssh/id_rsa.pubto the file
[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
exitto close out of the worker agent session to go back to the Jenkins controller session.
On the Jenkins controller, create an
.sshdirectory on the controller in the
[cloud_user@jenkins ]$ sudo mkdir /var/lib/jenkins/.ssh
known_hostsentry over from the
[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
worker1is 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.
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.txtlisted in the Last Successful Artifacts section. Open the view link that's next to it in a new tab or window.
What's a lab?
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.