How to store Jenkins jobs in your code — Part 2

Jose Manuel Cardona
4 min readApr 23, 2017

--

You can read the first part at How to store Jenkins jobs in your code — Part 1.

The demo is based in Docker, but if you don’t know about Docker, it doesn’t matter, you just have to modify the Jenkins that we will create to run your own build/deploy commands.

Now that we know what type of jobs we can use to store in our SCM, lets see how can we create a Jenkinsfile that allow us to deploy our project.

I created a dummy project based in the official voting app that runs in Docker. We want to deploy that project to our Docker every time that we push something to master, so we are going to create our Jenkinsfile to do that job.

You can install docker using their guide for your platform.

Configuring our Jenkins

You can skip this step if you have a working Jenkins with docker enabled

Our first step is run Jenkins in our Docker production and allow it to access docker daemon to execute the needed commands. The basi/jenkins container allow us to do it easily.

$ docker run -d -p 8080:8080 --name jenkins-demo \
-v jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker basi/jenkins:latest

Now we can access our jenkins using http://localhost:8080/ if you used docker in local or http://<docker-ip-address>:8080/ if you are using docker machine or any other host. I’m going to use localhost in the examples to simplify.

To start using Jenkins it requires us to unlock it using an administrator password.

Jenkins unlock request

We can get the token easily executing

$ docker exec -it jenkins-demo cat /var/jenkins_home/secrets/initialAdminPassword32f1126867924385a4a172156ff79722

After insert the administrator password, we can choose between install the suggested plugins or choose what you want. We choose the suggested plugins installation.

Plugins to install in Jenkins

Now we can create a user or log as admin and we are ready to start coding our Jenkisfile.

Creating our first Jenkinsfile

We are going to create a multibranch pipeline project to know in which branch the job is being executed and deploy in case we are working in master.

Multibranch pipeline branch

The Job requires a Jenkinsfile, so we configure our SCM

SCM configuration

Since now, it will search for a Jenkinsfile in the project root directory, so we need to write the next pipeline script in a Jenkinsfile.

pipeline {
agent any
stages {
stage("Deploy to production") {
when {
branch "master"
}

steps {
sh "docker-compose up -d"
}
}
}
}

This script will deploy the stack in the docker when our Jenkins job is executed.

The script is pretty easy. We just indicates that the script is a pipeline and that we want execute it in our current Jenkins, having one single stage named Deploy to production. This stage will be executed when the branch is master and the command to execute is a simple docker-compose up -d which deploys the new version into docker.

For sure, we can add more stages like for example check if the tests are passing, with something like:

pipeline {
agent any
stages {
stage("Run tests") {
steps {
sh "<command that run your tests>"
}
}
stage("Deploy to production") {
when {
branch "master"
}

steps {
sh "docker-compose up -d"
}
}
}
}

But there aren’t tests in this repository, so we don’t need this step and we will store the first pipeline example script in a file name Jenkisfile.

After push this file into our repository we just need to run our Jenkins job and see how our project is deployed automatically to our last version in master.

Take into account that the Jenkinsfile must be present in that branch, so if you created some other branch and try to execute the pipeline in that branch, the job will fail.

What can we do with the Jenkinsfile

  • We have our Jenkins job stored in our code and it is easily configurable in any Jenkins.
  • We have your Jenkinsfile versioned so you can restore easily old versions or try new ones in other branches using the same job.
  • We can execute different parts of the pipeline depending on the branch that we are executing. In the example we just used when { branch master” } but we can use so many times that we want and using regular expressions.

Conclusions

Using pipeline scripts we can code our jobs instead of do them from GUI.

This was an initial Jenkinsfile to try how it works and take an initial overview. Now you can and as many stages as you need to your deploys.

--

--