Project: Deploying Notes-app Using Jenkins

Project: Deploying Notes-app Using Jenkins

Hello everyone today I am going to deploy and run a complete functional CICD pipeline. I will be using an open-source project present on GitHub: Django-notes-app.

  • Let's start with Launching an instance. I am creating an Ubuntu instance on t2.micro (free tier).

  • Start with installing Jenkins which requires the installation of Java first. You can refer to the Jenkins Documentation.

  • First start with the installation of Java. You can then install Jenkins weekly release or LTS support as you wish.

  • Once done check if the installation was successful:

systemctl status jenkins

  • Now to access the Jenkin in GUI format first you need to allow port 8080 on your instance from the security group.

  • Now on your browser type your instance IP address/8080

  • You see this interface open. When ever first install Jenkins on your system a password is stored on your system and you need to access it via the given link. So in your instance type:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password and paste it. Press Continue.

Then on the next, you will see something like this:

Install the suggested plugins.

  • Next, you will land on the home page of Jenkins.

  • Let's start with building the pipeline. Click on New Item --> Choose pipeline and Write your project name.

You press OK. General configure page Opens:

  • In the picture, as you see I have ticked the GitHub project check-box and added my GitHub repo. link, I have a tick Do not allow concurrent builds checkbox this prevents the process from not running at the same time.

  • Next, we will scroll down. We will start to write the pipeline code. Pipeline codes are written in Groovy syntax.

  • Whenever we start making a pipeline, I always prefer to first write a basic script of my pipeline and then step by step I write code for each part.

In the above, I have first given the command to clone the repo to my system.

Each time I update my steps I build my pipeline so that continuous error fixing and debugging go on

In the above image same thing is shown I took five builds to correct my code and clone to repo to my system.

I will go in this sequence and build the complete pipeline.

Now let me explain each stage of the pipeline:

  1. The cloning stage clones the git repo on your system.

  2. The building stage builds the image from the Dockerfile.

  3. The pushing stage pushes the image on my DockerHub account.

    1. For this, I use my credentials to first log in and then push the image on DockerHub. Jenkins provides an option to create a global credential:

    2. Now to access credentials:

       withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                       sh "docker tag notes-app ${env.dockerHubUser}/notes-app:latest"
                       sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                       sh "docker push ${env.dockerHubUser}/notes-app:latest"
                       }
      
  4. Once, this is done our last step is to build the container and run our Python app.

To run the pipeline successfully we need to install docker-compose on our system.

sudo apt install docker-compose

Also, I have to add the $USER and Jenkins to my docker group and restart the instance.

sudo usermod -aG docker $USER
sudo usermod -aG docker jenkins
sudo restart

If you don't follow these steps you can encounter these kinds of errors:

Once this is done build the pipeline.

Hurray! The pipeline runs successfully, I then go to the docker-compose file on the repo and check the port on will the app will run

In the security group, I then allow port 8000. On the browser, I type my ipaddress/8000

This is the website and it runs successfully.

I also can add a GitHub webhook that instantly enables poll SCM, which triggers my pipeline to build and a circular cycle gets created.

At last, to build this project for me it took around one and a half hours. Previously I had made a different project that was similar to it but it took me about 5 hours because of several errors that I was committing, now Doing it again was a cakewalk for me.