Simple CI/CD Stack : Jenkins & Supervisor

Using Go as Example

In this tutorial, I will show how to create a Jenkins item, listen to Github Push, automatically build it and re-deploy it using Supervisor

Prerequisites:

  1. Jenkins
  2. GitHub account
  3. Supervisord, for Ubuntu, you may follow this step ← Install this if you want to follow the CD steps, otherwise, you may skip this

If you think you have all prepared, then let’s start to the first step:

Create a project in GitHub, mine is called as go_jenkins_demo

My Go project in GitHub

Mine is a public repo, I use Golang as my example code, the output of my code is as follow:

actual output of my code

(Optional) Setting SSH Key for Jenkins to GitHub

I prefer to use SSH as the auth method, so we have to add our public SSH Key to Git. By default, the user of Jenkins App installed in Ubuntu is jenkins, and it has no SSH Key yet. Let’s generate the key.

Logs to jenkins user:

gilang:~$ sudo su jenkins

Generate SSH Key

jenkins:~$ ssh-keygen

*When execute ssh-keygen, if you have no idea what are you doing, just leave all by default

Copy the generated Public Key

jenkins:~$ cat ~/.ssh/id_rsa.pub

Paste to the Github Repo -> Setting -> Deploy Keys

Name the title as you like, Paste the public key to field Key

We need to set Jenkins so it can be integrated with GitHub, install this plugin:

Mine is version 0.2.8

Create a new item as Freestyle Project with this following configuration

In General section, set GitHub project URL with the value of the code repository

In Source Code Management check the Git option and paste the SSH URL of GitHub repository. The url is git@github.com:<Repository Name>

Now, in Build Trigger section, check the GitHub Hook Trigger for GITScm Polling

Now, navigate to the GitHub repository and add new WebHook in the Setting menu and create new WebHook, set the Payload URL to be :

<JENKINS URL>/github-webhook/

(!IMPORTANT : don’t forget the trailing / )

Then, save the Jenkins Item. Test if the Jenkins item works by pushing some commit to the repo.

As we can see, the build trigger works successfully, it runs whenever there is a trigger from GitHub Web hook. You may modify the web hook to trigger in any other activity such as branch merging or pull.

Continuously Deploy the Code

Simply we just has our CI (Continuous Integration) running, now we will configure the CD (Continuous Deliveries). In this article, I would like to serve my code using Supervisor as the background service handler for my Go Project, so these are the additional steps.

gilang :~$ sudo visudo

add this text to the bottom of %sudo ALL=(ALL:ALL) ALL

jenkins ALL=(ALL) NOPASSWD: ALL

Now, our jenkins user can execute Sudo command without passing any password. Beware that it may lead to security risk if not handled properly.

Clone Code for the GitHub project And Do Initial Build

Clone the GitHub project to some folder that User jenkins has Read and Write Access.

For me it is : /opt/service/go-jenkins-demo

And I build it using Go as a file : go-jenkins-demo

Create Supervisor config

gilang :~$ sudo su jenkins
jenkins :~$ sudo nano /etc/supervisor/conf.d/go_jenkins_demo.conf

and write this (Don’t forget to change the command value

[program:jenkins_demo]
command=<YOUR ABSOLUTE PATH OF EXECUTABLE FILE>
autostart=true
autorestart=true
stderr_logfile=/var/log/jenkins_demo.err.log
stdout_logfile=/var/log/jenkins_demo.out.log

Now all set, update Supervisor service so it can recognize new config file

jenkins :~$ sudo supervisorctl rereadjenkins :~$ sudo supervisorctl update

Make sure that the code is running already, mine is as follow:

initial code build

The build command runs as follow:

  1. For every Push, navigate Jenkins to the cloned folder of GitHub Repo
  2. Pull latest version of code
  3. Rebuild code
  4. Restart Service in Supervisor

To do this, configure the created Jenkins item and navigate to Build tab, add new Build Step and create your own scenario, mine is as follow:

Save it and that’s all, every time there is a push of new version to the GitHub project, Jenkins will automatically pull the code, rebuild and re-deploy the code.

See figure initial code build to see the different

And, this, I think is the very old-school way we can configure Jenkins to create a working CI/CD.

I prefer old-school song and technology, as they are obvious and more understandable.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store