Jenkins end to end project: Create CICD pipeline

Install Java:

sudo apt update
sudo apt install default-jdk

Install Jenkins:

curl -fsSL https://pkg.jenkins.io/debian/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 binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

Start Jenkins

You can enable the Jenkins service to start at boot with the command:

sudo systemctl enable jenkins

You can start the Jenkins service with the command:

sudo systemctl start jenkins

You can check the status of the Jenkins service using the command:

sudo systemctl status jenkins

add port 8080 into security group of ec2 instance on which we are installing jenkins and source is my ip (or can choose anywhere IPV4):

go to url and paste 184.73.113.195:8080 (my instance ip's address) jenkins will start on this ip address.

go to terminal :

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

Paste the password into the setup wizard and follow the on-screen instructions to complete the Jenkins installation.

go to create job, write the name of job and select pipeline and select ok. then follow the below steps: 1. write desription about the job

  1. Add github project.

  1. Set crobjob: go to cron guru and schedule your build trigger:

go to pipline and write the following script, this script is called groovy script:

(groovy syntax and shell scripting are not same). Add every stage and build after every stage. then click on build and go to console output of build.

Before build just make sure docker is installed on your instance. go to terminal and install docker:

sudo apt install docker.io

Then build the job again, if you got permission denied error, then on terminal write the following command, which add jenkins user to users group and give all the required pemissions.

sudo usermod -aG docker jenkins

sudo rebbot

go to build history click #3(any build history) and open console output of that history:

go to terminal:

  1. FROM node:12.2.0-alpine: This sets the base image for your Docker image. It uses the official Node.js Docker image with version 12.2.0, based on the Alpine Linux distribution. Alpine is a lightweight Linux distribution, which makes the resulting image smaller in size.

  2. WORKDIR app: This sets the working directory inside the container to /app. This directory will be used as the root directory for any subsequent commands.

  3. COPY . .: This copies all the files and folders from your host machine's current directory (the one where your Dockerfile is located) to the container's /app directory. This effectively adds your application code and files into the image.

  4. RUN npm install: This command runs the npm install command inside the container. It installs the dependencies listed in your package.json file. This step prepares your application for execution.

  5. RUN npm run test: This command runs the npm run test command inside the container. It executes your application's tests if you have any defined in your package.json scripts.

  6. EXPOSE 8000: This instruction indicates that the container will expose port 8000. This doesn't actually publish the port; it's just a hint to anyone who might run the container based on this image.

  7. CMD ["node", "app.js"]: This sets the default command that will be executed when a container is started based on this image. In this case, it runs the app.js file using the node command.

Before build just make sure docker is installed on your instance. go to terminal and install docker:

sudo apt install docker.io

Then build the job again, if you got permission denied error, then on terminal write the following command, which add jenkins user to users group and give all the required pemissions.

sudo usermod -aG docker jenkins
sudo rebbot

after reboot reconnect to instance and build again this time docker image will be created and from that build we can create number of builds.

go to terminal, docker image is created there:

Now make container from docker image. now, push docker image to dockerhub (create a account on dockerhub) where all are images are present, push your image to docker hub, use dockerhub account credentials as follows:

go to dashboard of jenkins >>manage jenkins >> global >> add credentials >> add details as requirement:

credentialsId is different from user Id. username and password have one unique id that "Id" is here. use these credentials in pipeline.

Now push this image to docker hub:

install docker-compose:

sudo apt install docker-compose

I hope, this article is helpful.

Thank you