Day 80 - Project-1

Day 80 - Project-1

ยท

6 min read

Project Description

The project aims to automate the building, testing, and deployment process of a web application using Jenkins and GitHub. The Jenkins pipeline will be triggered automatically by GitHub webhook integration when changes are made to the code repository. The pipeline will include stages such as building, testing, and deploying the application, with notifications and alerts for failed builds or deployments.

Task-01

  • Open your AWS Management Console and launch an instance. Choose the AMI ID as Ubuntu and the instance type as t2.micro.

  • Now update the virtual machine and install Jenkins and Docker on your system using this script: vim install-docker-jenkins.sh.
#!/bin/bash

sudo apt update
sudo apt install docker.io -y

sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian/jenkins.io-2023.key
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 -y

docker --version
jenkins --version

sudo systemctl start docker
sudo systemctl enable docker

sudo systemctl start jenkins
sudo systemctl enable jenkins

sudo systemctl status docker
sudo systemctl status jenkins

sudo chown $USER /var/run/docker.sock
sudo reboot # After that again connect your ssh connection.
  • After connecting to SSH, use these commands to add the Docker group to Jenkins.
sudo usermod -aG docker jenkins
  • Go to your running instance, select your instance, click on security, then security groups, and click on edit inbound rules. Enable port number 8080, which is the default port number for the Jenkins server.

  • Now copy your public IP address from your instance and paste it into the web browser with port number 8080 <your IP address>:8080 and your interface will open like this.

  • Now copy that red-link and paste it into your virtual machine using this command sudo cat <red-link>.

  • Now copy that password from your local machine and paste it on your Jenkins server, then click on Continue.

  • After that, your interface will open like this. Click on "Install suggested plugins."

  • After installing, create a username, password, and email for your Jenkins server. Then click on Save and Continue, followed by Save and Finish, and finally Start using Jenkins.

  • After completing all these steps, your Jenkins dashboard will look like this.

Now lets start our project setuping andDjango-TODO-Application.

  • Now navigate to your Jenkins Dashboard. On the left side, you will see + New Item; click on it.

  • Now, enter the name of your project and select "Freestyle project," then click on "OK."

  • Now you can configure your project. In the configuration, under the General section, write the Description and select GitHub project, then paste your project's GitHub URL.

  • In the Source Code Management section, select Git and copy the Code Clone URL from GitHub. Paste it into the Repository URL. Ensure that your Jenkins server branch and GitHub branch are the same in the Branches to Build section.

  • Now scroll down and go to the Build step and select Execute shell.

  • Now, in the execute shell command, follow this command to build. Use these commands in the Jenkins server in the Execute shell, and click on Apply and Save.
echo "code build..."
docker build . -t django-app

echo "code run..."
docker run -d -p 8000:8000 django-app

  • Now, we will run it by clicking the 'Build Now' option. A build history will be created; then click on it.

  • Now, click on Build History #1 and then click on Console Output. You can see your output there. The Console Output will also show whether your job has failed or succeeded.

  • Now, open AWS, navigate to your instance, select 'Security', click on the security group, and choose 'Edit inbound rules'. Click on 'Add rule', set your project port range to 8000, select 'anywhere-ipv4' in the source, and save the rules.

  • Select the IP address of your AWS instance, copy it, and paste it into a new tab followed by the port number 8000.

  • To stop that container, navigate to your virtual machine and use these commands
docker ps
docker kill <container-ID> && docker rm <Container-ID>

Note: If you do not kill your application, your next build by webhook will not be created.

GitHub webhook integration:

  • Now navigate to your Jenkins dashboard, click on your project, then click on Configure. Scroll down to Build Triggers, select the GitHub hook trigger for GITScm polling, and click on Apply and Save.

  • Now navigate to your project GitHub repository and click on Settings. On the left side, click on Webhooks and then Add webhook. In the payload URL, copy this http://your-ip-address:8080/github-webhook/, select "Send me everything," and click on Add webhook.

Note: After clicking on "Add webhook," your webhook must get a green tick โœ….

Now let's make some changes in GitHub and see them reflected on your server. For that, follow these steps:

  • Now navigate to your project repository like Django-todo-cicd/todos/templates/todos/index.html and change Todo List - for batch 5 to Todo List - for DevOps Engineer. Click on Commit changes, and then navigate to build history. Our application will automatically build on the Jenkins server.

  • Select the IP address of your AWS instance, copy it, and paste it into a new tab followed by port number 8000, and your application is changed.

Hope you found this helpful. Do connect/follow for more such content.

Configure Notifications and Alerts

  • Navaigate to jenkins dashboard > click on Manage jenkins > click on System > and scroll down to E-mail notification in the SMTP server write smtp.gmail.com and in Default user e-mail suffix write @gmail.com > click on Advanced and select Use SMTP Authentication and in User Name write your email id and in password use this link๐Ÿ‘ˆ and It will give you 16 chars token past on it > Select Use SSL > in SMTP Port write 465 and click on Apply and Save.

  • Now navigate to jenkins dashboard and click on your application on the left side panel click on Configure and in the left side panel it self you will see Post-build Actions click on it and write your Email-id and click on Apply and Save.

  • Modify the Pipeline Build Steps to ensure the job fails, and wait for an email notification to land in your inbox.


Happy Learning

Thanks For Reading! :)

-SriParthu๐Ÿ’๐Ÿ’ฅ

ย