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
- GitHub account
- 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:
Creating dummy/example GitHub project
Create a project in GitHub, mine is called as go_jenkins_demo
Mine is a public repo, I use Golang as my example code, the output of my code is as follow:
(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
*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
Install GitHub Integration plugin to Jenkins
We need to set Jenkins so it can be integrated with GitHub, install this plugin:
Create New Item to Jenkins
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 email@example.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 :
(!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.
Make Jenkins as Sudo and skip password prompt
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
command=<YOUR ABSOLUTE PATH OF EXECUTABLE FILE>
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:
Write Build Command
The build command runs as follow:
- For every Push, navigate Jenkins to the cloned folder of GitHub Repo
- Pull latest version of code
- Rebuild code
- 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.
And, this, I think is the very old-school way we can configure Jenkins to create a working CI/CD.