Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon Apr 03, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Configure SSH on the Remote Agent

    From the Jenkins Controller node, SSH into the Agent worker node and 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
    
  2. Challenge

    Set Up the Node on the Jenkins Controller

    Log in to the Jenkins controller web interface using http://<PUBLIC_IP_OF_JENKINS_CONTROLLER>:8080. On the Jenkins controller, type:

    • Username: student
    • Password: OmgPassword!

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

    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:

    • Number 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.

  3. Challenge

    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 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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans