Automating CI/CD: Setting Up a Node.js Pipeline with Jenkins, Docker, and AWS
Tags: devops, node, cloud, tutorial
Published: 2024-02-25 05:33:31

_Automate Your Deployment: Step-by-Step Guide to Deploying a Web App with Jenkins on AWS EC2_ 

**Pre-requisites**

1. [EC2 instance](https://docs.aws.amazon.com/efs/latest/ug/gs-step-one-create-ec2-resources.html)
2. [Installed Docker & Docker Compose](https://gist.github.com/npearce/6f3c7826c7499587f00957fee62f8ee9)
3. [Nodejs app](https://github.com/Sharker3312/Node-App)

**Building the docker image**

- Create [docker-compose.yml](https://github.com/Sharker3312/Devops-Projects/blob/main/CD_pipeline_for_an_app/jenkins-service.yml) with jenkins service and run docker-compose

```bash
nano docker-compose.yml
docker-compose up -d
```

> _At this point the jenkins service is running on port 8080 of our EC2 instance but we cannot access it because this port is closed in the EC2 security group_

**Open port 8080**

1. Go to EC2 dashboard
2. Open Security groups 
![Security Groups](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/algu8d7ttq9h9wltlmgj.png)
- Press Edit inbound rules & Add rule

![Add rule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0kugbxgttletg1oww9fc.png)

**Jenkins website**
- _Access **public_ipv4:8080**_

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

> _Where is the password ?_

`docker ps`

![ID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4z0he9yokjrt2xv28x7.png)

`docker logs <ID>`

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

> _Then select Install suggested plugins and create the user._

**Nodejs Plugin Configuration**

> _The first step to upload a node.js application is to install the Node.js plugin, since jenkins is developed in java and does not have node.js applications by default_

1. Go to **Manage jenkins -> Plugins**
2. **Search** Nodejs plugin and install **without** restart
3. Go to **Manage jenkins -> Tools**
4. Search **NodeJs Installations** and Add **NodeJs**

![Add NodeJS version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxo14tso4jbbupx0nnv3.png)

**GitHub Repository Integration**
- [Fork](https://github.com/Sharker3312/Node-App) the project
- Add **Webhook**

> **Why is a webhook added**?

> _The primary objective of this configuration is to enable Jenkins to receive automatic notifications whenever a commit is made to the GitHub repository_

- Go to NodeJs repository -> Click Settings -> Webhook (Left panel)

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

**Add ssh keys**

> _Adding an SSH key to your GitHub profile is especially useful when you are working with private repositories._
- Open bash shell **inside** jenkins container -> **Run ssh-keygen**

![Docker exec](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhk37vzq8r0s5i7h6a8e.png)

![Inside container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzozno349zjv7vz4jqp2.png)

`cat /var/jenkins_home/.ssh/id_rsa.pub`

- Go to Github -> Profile -> Settings

![Settings Github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a079i7h8h7xr3hkimjbm.png)
- **SSH and GPG keys** -> Add new SSH key -> Add SSH key

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4va0n18x4obyh0j40iyc.png)

**Deploy App**
- Go to **Jenkins Website** -> **New Job**-> **Select Pipeline**

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

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8noronivxlbxa4s487xr.png)

> _Get url of github_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3amcfyy17oc60zfp2os0.png)

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

> _OK guys this nodejs application is exposed through port 8000 so we have to open the port in the docker-compose.yml and in the security group._

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rn46fx01a4tkuss8pvs.png)

> _Let’s make a small change in the project and apply commit_

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

> Open Console Log

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62yvnu543clzuq8dlpqp.png)

- Open **_Ip:8000_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76onqq6tvt0vxq2xwgbw.png)

_In the **next tutorial** , we’ll enrich our workflow by integrating a powerful **code analysis** tool for enhanced development_

---
<a href="https://linkedin.com/in/lesterdprez" rel="nofollow"><img src="https://camo.githubusercontent.com/d94940866c98cb4fca5783c4e8ac95776d2f52df6bbf3d5ab9e30d76836f30ae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c696e6b6564496e2d2532333030373742352e7376673f6c6f676f3d6c696e6b6564696e266c6f676f436f6c6f723d7768697465" alt="LinkedIn" data-canonical-src="https://img.shields.io/badge/LinkedIn-%230077B5.svg?logo=linkedin&amp;logoColor=white" style="max-width: 100%;"></a>