Jenkins pipeline demo / lab steps

Contents

Build Pipeline in Jenkins

Why Pipeline

Pipeline Terms

Step

Node

Stage

Steps involved in implementing a sample pipeline project

Setup up a docker Jenkins master

Forking a GitHub Repo

Generate SSH Keys for the Containerized Jenkins Master

Configure and Run the Pipeline

Build Pipeline in Jenkins

is a suite of plugins which supports implementing and integrating continuous delivery pipeline into Jenkins. Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines “as code” via the Pipeline Domain Specific Language (DSL) syntax.

Why Pipeline

Jenkins is, fundamentally, an automation engine which supports a number of automation patterns. Pipeline adds a powerful set of automation tools onto Jenkins, supporting use cases that span from simple continuous integration to comprehensive continuous delivery pipelines. By modeling a series of related tasks, users can take advantage of the many features of Pipeline:

  • Code: Pipelines are implemented in code and typically checked into source control, giving teams the ability to edit, review, and iterate upon their delivery pipeline.
  • Durable: Pipelines can survive both planned and unplanned restarts of the Jenkins master.
  • Pausable: Pipelines can optionally stop and wait for human input or approval before continuing the Pipeline run.
  • Versatile: Pipelines support complex real-world continuous delivery requirements, including the ability to fork/join, loop, and perform work in parallel.
  • Extensible: The Pipeline plugin supports custom extensions to its DSL [1] and multiple options for integration with other plugins.

Pipeline Terms

Step

A single task; fundamentally steps tell Jenkins what to do. For example, to execute the shell command make use the sh step: sh ‘make’. When a plugin extends the Pipeline DSL, that typically means the plugin has implemented a new step.

Node

Most work a Pipeline performs is done in the context of one or more declared node steps. Confining the work inside of a node step does two things:

  1. Schedules the steps contained within the block to run by adding an item to the Jenkins queue. As soon as an executor is free on a node, the steps will run.
  2. Creates a workspace (a directory specific to that particular Pipeline) where work can be done on files checked out from source control.

Stage

Stage is a step for defining a conceptually distinct subset of the entire Pipeline, for example: “Build”, “Test”, and “Deploy”, which is used by many plugins to visualize or present Jenkins Pipeline status/progress. [6]

Steps involved in implementing a sample pipeline project

For this example, we have taken Ubuntu aws server

Setup up a docker Jenkins master

  • First add the docker repository by running the below command from the terminal

sudo yum-config-manager –add-repo https://download. docker.com/linux/centos/docker-ce.repo

  • Next run sudo clean all
  • Next run
    yum install -y –setopt=obsoletes=0 \ docker-ce-17.03.1.ce-1.el7.centos \ docker-ce-selinux-17.03.1.ce-1.el7.centos
    This command will install Dokcer CE
  • Once docker is installed start the services
    sudo systemctl start docker
  • Next pull down the docker Jenkins image
    sudo docker pull Jenkins
  • Create a directory for Jenkins container
    sudo mkdir /var/jenkins_home
  • Next run the container using below command
    sudo docker run -d -u root -p 8080:8080 -p 50000:50000 -v /var/jenkins_home:/var/jenkins_home Jenkins

Forking a GitHub Repo

In your web browser, access and log in to github.com. Navigate to the repository for this lab, located at the following URL:
https://github.com/devops81/content-jenkins-pipeline Press the Fork button in the upper right corner to fork it to your account.

Generate SSH Keys for the Containerized Jenkins Master

We need to generate an SSH key for this we will be requiring docker container ID and same we will get using docker PS command.

To get the docker ID run the below command from the terminal

  • Sudo docker ps

Now we will use docker exec to generate the key using the above container ID

  • sudo docker exec -it bash

This drop us into a Bash shell inside of the Docker container. Generate the SSH key, using the defaults

Copy the public key to add to the Github:

cat .ssh/id_rsa.pub

Return to your fork of the Jenkins Pipeline project on GitHub and press Settings, then Deploy keys. Press Add deploy key.

Copy your public key into the text box. Give the key a title. Check Allow write access. Add key. Press Integrations & services on the left menu. Under Services, select Add service, then find Jenkins (Git plugin). For the Jenkins URL input the server’s IP address, including http:// and the port 8080, then press Add Service.

Deploy keys
1-2017-12-25 17_43_37-pipelinejenkins.docx - Word

Integration and services
pic2

Configure and Run the Pipeline

  • Press New Item on the Jenkins Dashboard. Enter an item name of Lab Pipeline and select Pipeline from the list of options. Press OK.
    pic3
  • Check GitHub project and input the URL to your fork of the Jenkins Pipeline repository.
    pic4
  • Under Build Triggers, select Poll SCM, not inputting anything into the text box.
    pic5
  • Scroll down to the Pipeline section, and for Definition set it to Pipeline script from SCM. Set SCM to Git and input the Repository URL, which is the Clone with SSH link found on your GitHub fork, under the green Clone or Download button. For Credentials, press Add, Jenkins, then set the Kind to Username with private key. Set the Username to root and the Private Key to From the Jenkins master ~/.ssh. Add. Select root under the credentials dropdown.
  • Leave Branches to build set on the master branch, and change the Repository browser to githubweb, copying in the URL to your fork of the project. Save the pipeline.
    pic6
  • From workstation computer, clone the git repository using the HTTPS or SSH via the Clone or download button: [user@workstation] git clone Make a new file under the repository directory using your preferred text editor. The file should be called Jenkinsfile. Add the following:

pipeline {

agent any

stages {

stage(‘build’) {

steps {

sh ‘javac -d . src/*.java’

sh ‘echo Main-Class: Rectangulator > MANIFEST.MF’

sh ‘jar -cvmf MANIFEST.MF rectangle.jar *.class’

}

}

stage(‘run’) {

steps {

sh ‘java -jar rectangle.jar 7 9’

}

}

}

post {

success {

archiveArtifacts artifacts: ‘rectangle.jar’, fingerprint:

true

}

}

}

Save and exit the file

  • Commit your changes using below command under the github repository

[user@workstation] git commit -am “Added post step with archive”

[user@workstation] git push origin master

Once the commit is done it will trigger a build under Jenkins and below is the dashboard view of the pipeline plugin we have configured.
Also we should see the archived rectangle.jar file under Build Artifacts.
pic7