Pipeline Jobs in Jenkins

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

In this lab we will go through creating and executing a Jenkins pipeline. There are two types of pipelines, declarative and scripted.

We’ll be setting up a project whereby code is pulled onto Jenkins, prepared, built, and then the output artifact is saved.

We will be pulling down sample code, written in C language, and compiling it via Jenkins. The sample code is basically a popular CS course exercise for creating Mario-styled (think Nintendo) pyramids. The user provides input for the height of the pyramid. This is beyond the scope of this lab, but the compiled binary being output may not be compatible with all types of Unix systems, due to the platform that it was compiled on.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Log into the Jenkins Server Provided by Hands-on Lab

Once the hands-on lab spins up and provides the public IP of Jenkins server to log into, use the following URL and credentials to log into the Jenkins instance setup for you:

  • URL: http://<Public-IP-of-Jenkins-VM>:8080/
  • Username: student
  • Password: OmgPassword!
Create a New Pipeline Jenkins Job

Once you log into the landing page for Jenkins, click on New Item (on the left side of the screen) to proceed towards selecting the type of Jenkins job you want to create.

Once the new page loads, click on Pipeline. In the input box, give a name to your job such as MyPipeline.

After selecting the type of job, Pipeline, and giving it a name, click on OK to proceed with configuring the job.

Configuring Jenkins Pipeline Job (Scripted Pipeline)

On the new page which loads for configuring the pipeline job, scroll down to the Pipeline section. Ensure the Pipeline Script option is selected in the Definition drop-down. An input area should be visible.
Copy the following body of text (which is basically in Groovy DSL format, a format understood by Jenkins) into the input area:

node {
   def gccHome = "/usr/bin/gcc"
   stage('Preparation') { 
   //pull down Mario C code from repo
      git 'https://github.com/linuxacademy/content-pipelines-cje-labs.git'
   }
   stage('Build') {
      //if system is Unix compile C source code
      if (isUnix()) {
         sh "'${gccHome}' --std=c99 -o mario lab1_lab2/mario.c"
      } else {
         echo "Not a Unix system, build not possible"
      }
   }
   stage('Results') {
      archive 'mario'
   }
}

Leave everything else to default settings and click on Save.

Running Your Jenkins Pipeline Job

From the previous step, after hitting Save you will be taken to the control page for the job you just configured, where you can see the job status and other job statistics, including being able to edit the configuration again. <br />
To kick off a build, click on Build Now. <br />
This should kick off the build and you should see the output of build status in a couple of places on your current screen.

1) Under the Build History box to the left of your screen you’ll see a build number and a progress bar if the build is currently running
2) The page will also be updated with a Stage View giving details of execution success(green) and time taken by each build stage that was defined in our Scripted Pipeline. Subsequent builds will add more data to average run times in the same area.

<br />
Under Build History if you click on the build number for example #1 you will be taken to the details of the specific build such as Console Output for execution of commands defined in the pipeline for each step. <br />

On this page you’ll also find the build output files or Build Artifacts which are the outcome of the pipeline stages that you built. You can click on the actual file name shown under build artifacts to download it to your system.

Additional Resources

You work at a start-up software house which has been a huge success, and your days of compiling source code by hand are over. The focus is now on a hands-off approach to building code. The VP of Engineering has chosen Jenkins as a CI tool, and wants to create a scripted pipeline to pull down your source code in C programming language, compile it using the gcc compiler, and present an output artifact (the result of your compile).

You have a rough idea of how you would tackle this: <br />

  1. Ensure that you define a variable within your Pipeline for the compile tool gcc, so that it won't make a dozen alterations in your pipeline if its path ever changes. You can assume that gcc will be at /usr/bin/gcc for now.
  2. Have at least three stages:<br /> <p>a. A Preparation stage where you clone code from the Git repo: <br />     <code>git 'https://github.com/linuxacademy/content-pipelines-cje-labs.git'</code></p> <p>b. A Build stage where actual code is assembled, linked, and built, to provide an executable binary.<br />     Sample build command: <code>gcc --std=c99 -o mario lab1_lab2/mario.c</code> </p> <p>c. An Archiving stage where you save or preserve the output of your Build stage.<br />     The archive in this case is the file <code>mario</code>. <br /></p>

    <p><strong>Hint:</strong> Use the archiveArtifacts step.</p>

With the above in mind, you now have to come up with a Jenkins scripted pipeline to automate the task at hand.

Logging In

A Jenkins server has been already been set up and made available to you. Once the lab interface provides details of the Jenkins VM, please navigate to the lab server's IP address, on port 8080, in a web browser to access it:
http://<public-ip-of-public-instance>:8080/ <br />

The credentials for logging into the instance are: <br />

  • Username: student
  • Password: OmgPassword!

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?