CI/CD using Jenkins
Tags: devops, docker, opensource, beginners
Published: 2022-05-24 08:33:00

Hello there, I hope you're staying in shape. We'll take a look at a Continuous Development and Continuous Integration (CI/CD) tool today. 

_Jenkins is an open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software._ [cite](https://www.jenkins.io/doc/).

---

##### Get your laptops and let's automate some tasks
![automation](https://media.giphy.com/media/PlLanl8Bzcvr14IfjJ/giphy.gif)

---



### Prerequisite
🎯 Jenkins [click ⬇️](https://www.jenkins.io/doc/book/installing/)
🎯 A bit of scripting knowledge
🎯 Java installed on your machine

---

### Agenda
🎯 Jenkins setup
🎯 Creating a job
🎯 Scheduling jobs using Cron expression
🎯 Remote triggers
🎯 Webhook and Poll SCM
🎯 Switching Users

---

### Jenkins setup
The default port for jenkins after following the installation guide is `8080`. Visit `http://localhost:8080` in any browser. I've included the installation guide in the resources section below. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45xbpjssk8t8dvmt7giq.PNG)

---

⏭️ After that, type in the username and password you created during the setup process. This is what your dashboard should look like: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jmdqt1qi4m4a5crq7tgx.PNG)


---

### Creating jobs
Let us now create our first job. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55f4czhnm7h5urzce6qg.PNG)


##### Give it a name, choose `free style project` and click OK
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgqhb9lpzt20bgy4wize.PNG)

 ---   

##### You are redirected to this page. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vc0px8oy7y3mxmd74j91.PNG)

I chose the window shell because I have Jenkins installed on a Windows machine. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pppow9loubllafy3wpe0.PNG)

---

⏭️ Following that, we will build our job.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h30g8l7fkgd7hwdjn097.PNG)

---

📝 Green denotes success. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kx8wxpyu6g4cu0avnne.PNG)
 
---
 
##### Let's look at the results. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzaqunieosmwklysm480.PNG)
 
---

##### Result
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5hnhy0g2i2cfcvrjcngq.PNG)


---

## Job scheduling
Now that we've established how to make a new item, let's looking at automating this task using <mark>Cron</mark>.

_**Cron is the baked in task scheduler - run things at fixed times, repeat them and so on**_ 

Now let's schedule our build using <mark>Cron Expression </mark>. Locate the `configure` icon and click on it.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rosv90fsnf169uruvtdj.PNG)
 
---

##### ⏭️ Select the build periodically option
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zob7gsrkfyo85m01qd0v.PNG)

##### Fig 1.8
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nowcwuum3iu6wqcno38s.PNG)
In the describe image `Fig 1.8`, here we trigger a build at an interval of 1 minute.
 
⚠️ Remember to leave a space between the 2 adjacent stars.

A bit about cron/scheduler: Each stars represent time i.e first star (seconds), second star (minutes) and so on.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9cnade530jlrvqle4gb.PNG)

[Read more about cron expression here](https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm) 



---


##### ⏭️  Let's check our build history to see if the <mark>cron</mark> scheduler actually works.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur7pdhn1s5oe1ndhu57g.PNG)
 

---

## Remote triggers
We have other options of building our files. Let's `untick` the build periodically and let's choose `build triggers`.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qe3cotc1tiko9gkc9obf.PNG)

⏭️  The `firebuild` is the token. And we enter our remote trigger in the form of url. Click apply and save.
The URL should look like:
`http://localhost:8080/job/myfirstjob/build?token=firebuild`

⏭️  I have cleared the build history for clarity's sake. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ixzv8fy2r8pxohrgfvzp.PNG)

💡 Let us now run the URL above in our browser; we now have a new build trigger. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpkpvi5go0d0gzjyq28k.PNG)
 
> So, as long as you have the remote trigger link, you can build from anywhere, using any browser. 


---

## Parameterized Job:
_A build parameter allows us to pass data into our Jenkins jobs. Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on._

Click the configure icon
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19pj5cdk8ticskf2svht.PNG)

⏭️ Choose the parameterized option
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9vezukayo34cuwxkokb.PNG)
  
⏭️ Add parameter, choose string parameter
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/752bgxxf7pi16xmeg14q.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e20gghs2letnfz87552t.PNG)
  
⏭️ If you observe closely you will see `build with parameter`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yk60nf0o8rtghqjlam72.PNG)

⏭️ Click on the build
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lut3g0zd266c6ev2k7co.PNG)
 
---

## Webhook and Poll SCM
_A Webhook is a mechanism to automatically trigger the build of a Jenkins project in response to a commit pushed to a Git repository._  

**while**

_Poll SCM periodically polls the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits where pushed since the last build._


To use web-hook, we must first connect to our source code manager, git. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yougijel424om1yog7u6.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erqu89gjc9w3sg8xesjc.PNG)
Depending on the repository, change the git branch to main or master. 
⚠️ For private repositories, credentials will be required. 

---

⏭️ We use the poll with cron schedule
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esos5pp4yimoq43xq3t5.PNG)
 

---

## GitHub web hook
📝 Go to configure and choose GitHub web-hook
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0dri6z8vzplndahf3wz.PNG)
 
📝 Go to your the repository on GitHub, locate and click web-hook under the `settings section`.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f61gic86a35bp76sfi9g.PNG)
 
📝 Click on add
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwj84vyp41v0s9co6b9u.PNG)

Add the Jenkins URL to the payload
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8modun5slcob1v3oiu4v.PNG)
  

⚠️ Using localhost with the above will not work, to make our localhost public ip we need ngrok to forward it.

[Download](https://ngrok.com/download) it and install. Expose Jenkins port which is `8080` to get the URL
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p62jydm7np6r6fqnpg5f.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ly3i6evea5flojk4pyag.PNG)


##### When we visit the URL, we get:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rbmv03us5dpty31agfv.PNG)
   
⏭️ Next, we paste the forwarded URL from ngrok to the github webhook
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0n898ucd244ze7g8ss9.PNG)
Click on `Add webhook`, and refresh. On refresh we get a green ✅
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xucmzbybta57wgqv1gvi.PNG)
 
⏭️ Now, lets login into Jenkins using the forwarded URL generated from ngrok and connected to Jenkin web hook.

To build, edit your GitHub repo, commit changes and refresh the jenkins page
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hb31jw76c17ckpn22fb0.PNG)
 
On success, we get
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jriyy54c03wzg2wqlpr.PNG)
 

---

## Manage users 
Let's create another user apart from the first user created initially

⏭️ Click manage Jenkins
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1r1m8gz7k1i0hgicsyx1.PNG)

⏭️ Under the security section, click `manage users`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obx2gijtud3pkcqfzvb8.PNG)
 
 
⏭️ Click `create users`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25p2532lw8dfzalm3cpv.PNG)

##### ⏭️ Enter the credentials
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrxjpkuvl6dxs2v1u2gk.PNG)

##### Result:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbsp63fsf2ucki0bw5kb.PNG)


---
## Switching Users
Now we can logout of our admin  user and use the `testuser` account to login.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adhkxiznxh94a8bggz5x.PNG)

> On login, you can see that the access is denied because we need to give this user permission or elevated rights to perform certain roles in Jenkins. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4jvixve9xz3cru5akxo.PNG)
 

> Let us log out of the test user and login as the admin user to grant access rights via the Role Base Access control. 

 ⏭️  Let us begin with the user being able to see all of the jobs we have. Go to Manage Jenkins and then Configure Global Security. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3rsd5g1rqb4hahg5ept.PNG)
 
I have set the `authorization` to Role Based before now, I will change it to `Anyone can do anything`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cedmpx29yn3eh1tfg7r3.PNG)
Click apply, save and logout. Thereafter, you can login with the test account.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4smg7rqbh66hq7g96ta.PNG)

> Now the <mark>testuser</mark> account can see all jobs created. This is not the best practice, because we should not give any user the permission to do certain things on Jenkins. 

We use the Role-Based Authorization strategy plugin for this. Click `manage plugin` and search role based
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ch9j154e956727bbwi58.PNG)
 
To use the Role base, Go to manage jenkins and Config Global Security. The role base option should now be available
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpqnamimiaq1w539a749.PNG)
 Then go back to manage jenkins and click on `Manage and Assign Roles`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6948kt6v1vyotx4vx8p.PNG)

You get redirected to this page
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3yocgtzzr6nz7bpaw59.PNG)
 

Click on manage roles
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lsqm9uk48xp53fqdjxo.PNG)

Now you can check the access you want to enable for this particular role. Save and Apply
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgqfd1d7yb4lymxxenxg.PNG)

Now let's go and assign role by adding our `testuser`.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3d6p8pbve4ashp8dcqt6.PNG)

Now we can tick the role we want to assign  to the testuser account
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzjf2t9tna9n70nkwgab.PNG)
  
Click Apply and Save.
Now let's logout from the main account and login with our testuser`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iuqrmnpeqbaxv4frpee1.PNG)

We see `access denied ` because i did not give this user account view access based on the configuration we set earlier. Let me edit and give it <mark>view access</mark>. I will logout from the `testuser` into the admin and enable the view access and overall access.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aq8g1yq8ostetv6uge36.PNG)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wecvhdbk6qwwnrsgyla4.PNG)
Click apply and save. Now logout from the admin and login with the `testuser` account
 
  Result: ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/article/shdow50kmccn53ayfqxq.PNG)

---

## Config system
This is where you can set certain parameters, like the Jenkins URL, GitHub servers etc
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgxcqv5j1ng0vaxe9j5w.PNG)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/its0gtjzrb4z7f5lu2rw.PNG)

---

##### Email notifications
Email notifications allows us to get notified when we build or have failed transaction during our CI/CD Pipeline.

To set up, go `Configure System` and locate the email section.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fysdqnkjgn2y9u7ta7u0.PNG)

##### Go back to configure
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzaqyfoq8d8yu7lelsz4.PNG)

And click on the preferred post build action
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvbhl12ptn14m8yxzcxj.PNG)


---
### Conclusion
I hope this post was helpful in starting out with Jenkins for your CI/CD workflow. I hope to deploy a NodeJS Project in the next tutorial. Thanks for reading.

### Resources
I highly recommend [Thetips4you, Jenkins Tutorial for Beginners](https://www.youtube.com/watch?v=MTm3cb7qiEo&t=14s). I extracted part of this tutorial here with his full permission.

[Learn about CI/CD here](https://www.redhat.com/en/topics/devops/what-is-ci-cd)

[Installation guide  by guru99](https://www.guru99.com/download-install-jenkins.html)