Day 25 - Complete Jenkins CI/CD Project Documentation

Day 25 - Complete Jenkins CI/CD Project Documentation

So, In this blog we will see all the steps and process we have done till Day 24.
Here is the link for all the previous blogs, please have a look: Smriti's blog.

Steps for Jenkins CI/CD Project

Let's start with detailed documentation.

  1. First create your account on GitHub by following the steps given in Creating-an-account-on-GitHub.

  2. Create a Repository on GitHub, you can follow steps given in the link Create a GitHub Repository.

  3. You can do the steps for setting your email and username, cloning the repository, adding file in repository, committing the changes and pushing the changes to GitHub.

     #For setting username and email id.
    
     git config --global user.email "your email id"
     git config --global user.name "Your name"
     git config --global --list -> List all the git config properties
    
     #For cloning the repository.
     git clone <Your repo URL>
    
     #For making this Repo as a git Repo.
     git init
    
     #For adding files.
     git add <filename>
    
     #For commiting your changs.
     gir commit -m "message"
    
     #For pushing your local changes to remote repository.
     git push origin <branchname>
    
  4. For more details, you can go through this Basic Git Commands.

  5. Create an AWS account, follow the steps given in Create an AWS Account.

  6. and launch an EC2 instance, you can check this out on How to create EC2 Instance and connect an instance to a client server on this link How to connect an instance to a client server.

  7. Now list of software you need to download on your instance are:
    1. docker
    2. docker-compose
    3. java
    4. Jenkins

     #For installing docker
     sudo apt-get update && sudo apt-get install docker.io
    
     #For installing docker compose
     sudo apt-get install docker-compose
    
     #For installing java
     sudo apt install openjdk-11-jre
    
     #For adding the Jenkins repository key to the system
     curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
       /usr/share/keyrings/jenkins-keyring.asc > /dev/null
    
     #For adding the Jenkins repository to the package sources
     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
    
     #For installing Jenkins
     sudo apt-get install jenkins
    
     #The Jenkins service should start automatically after installation.
     #If it's not running, start it with the following command:
    
     sudo systemctl start jenkins
    
     #Check whether Jenkins is active or not
     systemctl status jenkins
    
  8. Add User to the group "docker".

     sudo usermod -aG $USER docker
    
  9. To check User is added to the group or not, use the below command:

     grep docker /etc/group
    
  10. Give permissions to the below file as in previous blogs we have seen this permission error on Jenkins.

    sudo chown $USER /var/run/docker.sock
    sudo chmod 777 /var/run/docker.sock
    
  11. Now, you can use your Dockerfile to build image. To create a Dockerfile, follow this link Creating a Dockerfile.

    #Dockerfile Example
    
    FROM python:3.9-slim-buster
    WORKDIR /app
    COPY . .
    RUN pip install flask
    EXPOSE 5000
    CMD [ "python", "main.py"]
    
    docker build Dockerfile -t <appname>
    
  12. To see the list of images

    docker images
    
  13. Now, to run a container using your image

    docker run -d -p hostport:containerport <imagename>
    
  14. To see the running containers

    docker ps
    docker ps -a -> For all containers whether it is running or exited or stopped.
    
  15. Set up inbound rules in your security group of your instance on AWS Console, refer this link Setting up inbound rules. Basically adding the specific port to your instance so that you can access your application via browser using Your_Public_IP:PORT

  16. You can also use Docker-compose to run your application.

    docker-compose.yml Example
    
    version : "3.3"
    services:
      web:
        image: nginx:latest
        ports:
          - "80:80"
      db:
        image: mysql
        ports:
          - "3306:3306"
        environment:
          - "MYSQL_ROOT_PASSWORD=test@123"
    
  17. You can use the below commands to up and down your application.

    docker-compose up
    docker-compose down
    
  18. Now, as we have seen previously, Jenkins runs on port "8080", so also add this port in the security group, repeat the same process as in Step 15.

  19. Access your Jenkins using your <Your_Public_IP:8080> via your browser.

  20. You will see the below page.

    You can find the Admin password on the location given in the above snapshot.

  21. For setting up your Jenkins, refer this link Setting up Jenkins.

  22. You can begin with creating your Job, using the link First Freestyle Job .

  23. Now, you can setup Webhook in GitHub, refer this link How to create a Webhook.
    Basically you have to give a payload URL which is <Jenkins_URL:port/github-webhook/> . And select the event that should trigger the webhook.

  24. Once you have configured the webhook, click on the “Add webhook” button.
    After setting up the Webhooks do not forget to refresh the page unless the webhook URL is ticked as shown below.

  25. Add webhook functionality in your Jenkins Job, refer to the link Jenkins Freestyle Job with Webhook.

  26. Now, make some changes to your GitHub repo in any file. After you commit the changes, you will see the Job getting triggered in Jenkins.
    So, basically this step was the integration of GitHub with Jenkins.

  27. You can follow the previous blogs on Jenkins if you get some error while triggering your job, there is an example of an error you can get.

  28. You can access your application via browser, using <Your_Public_IP>:Port you have specified in your application> .

Conclusion

So, this is the detailed Documentation on Complete Jenkins CI/CD project using GitHub and Docker Commands.✅

By creating a new Jenkins job for your CI/CD pipeline, you automate the entire process from source code management to deployment, ensuring consistent and reliable software delivery. Jenkins orchestrates these steps based on triggers you define, providing a streamlined and efficient workflow for development and deployment.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~Smriti Sharma✌