Jenkins CI with GitHub Integration

Launch Instance

Create AWS EC2 - t2.micro

sudo apt update
clear

Install JAVA because Jenkins is built on Java language

sudo apt install openjdk-11-jdk -y

To check JAVA version whether it's installed or not

java --version

Install Jenkins

Jenkins Installation Official Page

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

sudo apt-get update

sudo apt-get install jenkins

Start Jenkins

sudo systemctl enable jenkins
sudo systemctl start jenkins

To check our Installed Jenkins is working or not, we use command

sudo systemctl status jenkins

Above image shows that our Jenkins is Actively running

Jenkins works on port 8080 so we have to add port 8080 in our Instance security group

After adding port 8080 we will be able to access our Jenkins

Copy the /var/lib/jenkins/secrets/initialAdminPassword

and use command

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Use this password to access jenkins

Install suggested plugins

And reset the password

Create New item in the Jenkins

Use these image steps

Also specify branch here

First Apply then Save

Click to Build Now

Click to console Output after clicking the build

Copy the Building in workspace full line and paste it to your terminal to check our code arrived or not

You can see our code which was present in the GitHub, now it is showing in our Server also

To run this code we need to install Nodejs in our Instance

sudo apt install nodejs -y
sudo apt install npm -y
sudo npm install
app node.js

Now we have to add port 8000 in our Instance security group

Our Code is working now

Now we have Create Container so that we can access this app on any device hassle free

Installing Docker

sudo apt install docker.io -y
sudo nano Dockerfile

Search docker base image with node

Writing a code inside a Docker file

FROM node: 12.2.0-alpine
WORKDIR app
COPY ..
RUN npm install
EXPOSE 8000
CMD ["node", "app.js"]

From node: 12.2.0-alpine (we are fetching the node the docker base image with node js version, in simple way Installing OS with Node js)

Workdir app (we are creating the app folder where we are working now)

Cop . . (we copied all the source code here)

Run npm install (we are installing all the dependencies)

Dockerfile got created

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=project-node&target=&ulimits=null&version=1": dial unix /var/run/docker.sock: connect: permission denied

(Above error will come to fix this error use following below commands)

Give permission to your docker

ls -l /var/run/docker.sock
sudo usermod -aG docker $USER

Check the status of the Docker it's working or not

sudo systemctl status docker

If not working then use command

sudo systemctl start docker

Use the final command to build docker container

sudo docker build . -t project-node

Use this command to run the docker container

sudo docker run -d --name project-node -p 8000:8000 project-node

To check whether our container is running or not

sudo docker ps

We have integrated our GitHub code with the help of Docker

CONTINOUS INTEGRATION is Done (CI)