Setup Continuos Integration/Delivery system in just 4 steps with Jenkins Pipelines and Blue Ocean
Tags: continuousdelivery, jenkinspipelines, blueocean, cicd
Published: 2017-05-07 09:29:32

---
title: Setup Continuos Integration/Delivery system in just 4 steps with Jenkins Pipelines and Blue Ocean
published: true
description: Tutorial about how you can setup a cool continuous integration /delivery system using Jenkins Pipelines. We cover the 4 initial steps to get the your first pipeline up and running
tags: Continuous Delivery, Jenkins Pipelines, Blue Ocean, CI/CD
cover_image: https://thepracticaldev.s3.amazonaws.com/i/pkajloohhgjkvvq1q475.png
---

Now that the first stable version of [Blue Ocean Plugin](https://jenkins.io/projects/blueocean/) was released, it is easier and cooler than ever to setup your Continuous Integration/Delivery with Jenkins. [Jenkins](https://jenkins.io/) has also evolved quite a lot in recent years, making easier to setup your automation server and builds in just a couple of steps.

First of all, let’s clarify some of the concepts that we will be using in this tutorial:

**[Multibranch Pipelines](https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Multibranch+Plugin):** Pipelines, and especially multibranch pipelines, are a game changer in Jenkins. Thanks to this plugin, you can simply setup your repository url and Jenkins will identify all your branches. It will also automatically start new builds as soon as new commits are pushed, so no more webhooks or cumbersome configuration is needed.

**[Jenkinsfile](https://jenkins.io/doc/book/pipeline/jenkinsfile/):** Together with Pipelines the new concept of `Jenkinsfile` was introduced. This is a file that you create in your repo, which contains your pipeline configuration. Jenkins will then look for this file in your branches and execute the build according to the stages defined in there. That makes possible to have your pipeline configuration together with your project and under version control.

**[Blue Ocean](https://jenkins.io/projects/blueocean/):** This plugin is must for everyone using jenkins pipelines. Blue Ocean it is an opensource plugin that rethinks the user experience of Jenkins. Most amazing feature is the beautiful user interface of Pipelines, allowing for fast and intuitive comprehension of build's status. If the awful user experience of Jenkins was holding you back from using it, there are not more reasons for that.

Ok, so now that these concepts are clear, let's see how we can get our Continuous Delivery environment up and running in just 4 steps:

### 1. Jenkins installation:

You can follow the installation steps for your *OS* or *Docker* in the official docs: 

* [https://jenkins.io/doc/book/getting-started/installing/](https://jenkins.io/doc/book/getting-started/installing/)

For this post, let's assume that you are using *Ubuntu*. You can log in into your system as sudo and execute the following commands:

```bash
wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
```

This automatically creates a jenkins user and daemon that listens on port 8080. That means that your Jenkins system is now reachable on `http://<your_ip>:8080`. In case that you are testing that locally, that would be `http://127.0.0.1:8080`

If you do not know your server public IP, you can execute `ifconfig` to get this information.

Open the url and follow the [installation wizard](https://jenkins.io/doc/book/getting-started/installing/#post-install-setup-wizard). Here we recommend that you to select the default option to install the suggested plugins.

![Installation Wizard](https://thepracticaldev.s3.amazonaws.com/i/4tggspeafhjnlryphjxj.png)

### 2. Intall Plugins

* Go to `Manage Jenkins > Manage Plugins > Available` and filter by `Blue Ocean`.
* Select and Install
* If you use *Bitbucket* for your repository, you must also install `Bitbucket Branch Source Plugin`

![Plugins](https://thepracticaldev.s3.amazonaws.com/i/fqxqqg6jpohnywjqszrf.png)


### 3. Jenkinsfile

* In your project repository root, create a new file called `Jenkinsfile`. This file is written in `Groovy` and will define your `Pipeline Stages` configuration. For this tutorial, just copy the following dummy example and paste it into the `Jenkinsfile` just created.
* Save it, commit and push.

```groovy
node {
 	// Clean workspace before doing anything
    deleteDir()

    try {
        stage ('Clone') {
        	checkout scm
        }
        stage ('Build') {
        	sh "echo 'shell scripts to build project...'"
        }
        stage ('Tests') {
	        parallel 'static': {
	            sh "echo 'shell scripts to run static tests...'"
	        },
	        'unit': {
	            sh "echo 'shell scripts to run unit tests...'"
	        },
	        'integration': {
	            sh "echo 'shell scripts to run integration tests...'"
	        }
        }
      	stage ('Deploy') {
            sh "echo 'shell scripts to deploy to server...'"
      	}
    } catch (err) {
        currentBuild.result = 'FAILED'
        throw err
    }
}

```

### 4. Setup Multibranch Pipeline

* Go to the jenkins home page and click on `new Item`. Give a name to your Job and select `Multibranch Pipeline`. 

	* if you use *Bitbucket*, you must select `Bitbucket Team/Project` option instead.

* After that, we need to configure the repository url and credentials. You can see examples of that in the following screenshots depending on your source control system (Bitbucket, github, git):

	* Very important here is that you select `Jenkinsfile` as Build Configuration Mode.

	![Repository Configuration](https://thepracticaldev.s3.amazonaws.com/i/92p77kpbdvrrgy15sd8o.png)


* Click save. You will notice that Jenkins starts scanning your repo in search for all your branches. In fact, what Jenkins is doing, it is to look for `Jenkinsfiles` inside your branches.

* Click on the header `Blue Ocean` button.

* et voilÃ ! you just setup a Continuous Delivery system that will automatically scan, build and deploy all your branches in your repository. 

* Now click around on the `Blue Ocean` pipelines branches to see how awesome it looks.


As you might have noticed, our `Jenkinsfile` is just defining the stages and printing some `echos`. The idea is that you replace these `echos` with your actual scripts. You can see a real example of what we are doing for our Magento 2 builds at the following link:

* [Jenkinsfile real example](https://github.com/jalogut/jenkinsfile-basic-sample/blob/master/Jenkinsfile.realExample.groovy)

You can also find more info about `Jenkinsfile` options and syntax here: 

* [https://jenkins.io/doc/book/pipeline/jenkinsfile/](https://jenkins.io/doc/book/pipeline/jenkinsfile/)

In a follow up post, I will explain how you can go one step further and keep only one pipeline configuration for all your project in a shared repository. Stay tuned!