Jenkins Fundamentals

Installation of Jenkins

  • Update the system before installing anything.

sudo apt update

  • Install Java in the system as it is a pre-requisite for Jenkins.

sudo apt instal openjdk-11-jre

  • Check the JAVA version installed on the system.

    java --version

\=> Now, go to google.com search Jenkins install on Ubuntu, and click on the first result

-> There are two commands for the installation of Jenkins

: Long-term support- It is a stable update of Jenkins

: Weekly release- It is released weekly and might have bugs.

Select any one:

  • Install Jenkins on Ubuntu

  • Check the status of Jenkins is running.

systemctl status jenkins

  • Adding port 8080 to access Jenkins

->As Jenkins runs on port 8080, we have to add port 8080 on ec2-instance.

-> Go to the security group of ec2-instance and then inbound rules, add rule port 8080 to access from My IP.

  • Running Jenkins on port 8080 of ec2-instance.

->Now copy the IP address of ec2-instance and add port 8080 at the end.

  • Unlock Jenkins to access

There is a password located in the /var/lib/jenkins/secrets/initialAdminPassword

  • To view the password located in the location

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

->copy and paste the password on the Administrator password

->After that it will prompt to install plugins-> Click on install suggested plugins

->Now, add the First Admin user which will have all the access.

->After filling in the information, save and continue.

->Jenkins is installed and ready to use.

Freestyle project delivery using Jenkins build with docker and running on ec2

This project will bring the code from GitHub build it through docker and run on the ec2 instance.

  • Click on create a job:

  • Enter a project name and choose Freestyle project:

-> Fill in the details for the job.

Description: To describe the brief function of the job.

GitHub project:

Project URL: Copy and paste the URL of the GitHub project from the GitHub repository.

->This project is parameterized: to pass parameters to be executed

->Throttle builds: to give time between the builds and avoid override

->Execute concurrent builds: to execute two builds simultaneously.

-> Source code management

Where the source of the code is situated.

Git: As the code is situated in the GitHub repo

Repository URL:https://github.com/ManishNegi963/react_django_demo_app.git

Credentials: Add the credentials if this is a private repository

Branches to build:

Branch specifier: Go to the GitHub repo see the branch and fill it (main)

-> Build triggers

These are used to automate any task when an event occurs.(leaving these blank as we want to do this manually by clicking button)

->Build steps

Steps for build to be executed while CICD.

  • Printing to the console.

echo " This job worked and my code is cloned".

->Now, a job has been created.

->Click on the green arrow to build

now, click on django-todo-app-delivery, and click on#1 build and console output

The build has been successful, cloned code, and copied.

Copy the line building in workspace /var/lib/jenkins/workspace/django-todo-app-delivery to check the copied code from GitHub.

  • Check the code copied into system from GitHub

cd /var/lib/jenkins/workspace/django-todo-app-delivery

  • Build again with a different output.

Now, go to the app and then configure, edit the commands, and save.

  • To check the current date:

date

  • To check the current user:

whoami : Jenkins is currently accessing Ubuntu as a user.

Now, click on build now and a new build has been started.

Go to the console output to check the new output with the build.

  • Build again with a different output.

Edit build steps and save:

  • Print the line:

echo "code cloned"

  • Build the image from Dockerfile:

docker build . -t django-app

  • Print the line:

echo "code build"

  • Print the line:

echo "code deployed"

Build Now and then go to console output to check results:

Failure: As docker was not installed on Ubuntu.

+ docker build . -t django-app
/tmp/jenkins3113040934025775550.sh: 3: docker: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE

Now go to the console and install Docker.

  • Installing docker:

sudo apt-get install docker.io

->Now build again:

Click on build now:

Build failure: Permission denied error as Jenkins as user is not part of group docker

  • Adding Jenkins as a user of the docker group

sudo usermod -aG docker jenkins

  • Build Now:

FAILURE: As we need to reboot the system after modifying the group permission.

  • Reboot the system:

sudo reboot

Go to console output:

Build successful

Now build with a different output:

  • Make a container from the image with port 8001 as it is exposed in the Dockerfile.

docker run -d -p 8001:8001 django-app:latest

Go to console output:

Build successfully as a docker container has been created.

->Now, go to the security group and add port 8001 from anywhere.

Now, go to the browser and add port 8001 at the end of the IP address.

The application is running.

Now the build application using docker-compose and change code in GitHub.

Edit the build steps:

  • Run the docker-compose file in detached/background mode:

docker-compose up -d

But, first, we have to install docker-compose into the system.

  • Installing docker-compose in console.

sudo apt-get install docker-compose

Go to GitHub and edit the index.html file with React django TODO APP and commit changes.

Build now

Build failure as the port 8001 is already allocated.

Now, go to the console.

  • View all the running containers:

sudo docker ps

  • To remove the port mapping and stop the container:

sudo docker kill 0002f483b1b6

Build Now-> Go to console:

Build successful.

Go to the browser and refresh page, the React Django TODO APP has been updated:

NOTE: In order to prevent the port-blocked error, use the docker-compose down command before docker-compose up as it will first down the container and free the port and then allocate every time a new build is done.

Change the build steps

  • To down the container and free the port allocated to the container

docker-compose down

Build Now:

But sometimes, it doesn't refresh the new build and to prevent that use the following:

  • To run the container where service names web will have no dependencies.

docker-compose up -d --no-deps --build web

Build success.

Django-TODO-app delivery completed