Experiment No. 7 Title: Applying CI/CD Principles to Web Development Using Jenkins, Git, using Docker Containers
Experiment No. 7
Title: Applying CI/CD Principles to Web Development Using Jenkins, Git, using Docker Containers
The objective of this experiment is to set up a CI/CD pipeline for a web application using Jenkins, Git, Docker containers, and GitHub webhooks. The pipeline will automatically build, test, and deploy the web application whenever changes are pushed to the Git repository, without the need for a pipeline script.
Continuous Integration and Continuous Deployment (CI/CD) principles are integral to modern web development practices, allowing for the automation of code integration, testing, and deployment. This experiment demonstrates how to implement CI/CD for web development using Jenkins, Git, Docker containers, and GitHub webhooks without a pipeline script. Instead, we'll utilize Jenkins' "GitHub hook trigger for GITScm polling" feature.
In the fast-paced world of modern web development, the ability to deliver high-quality software efficiently and reliably is paramount. Continuous Integration and Continuous Deployment (CI/CD) are integral principles and practices that have revolutionized the way software is developed, tested, and deployed. These practices bring automation, consistency, and speed to the software development lifecycle, enabling development teams to deliver code changes to production with confidence.
Continuous Integration (CI):
CI is the practice of frequently and automatically integrating code changes from multiple contributors into a shared repository. The core idea is that developers regularly merge their code into a central repository, triggering automated builds and tests. Key aspects of CI include:
Automation: CI tools, like Jenkins, Travis CI, or CircleCI, automate the building and testing of code whenever changes are pushed to the repository.
Frequent Integration: Developers commit and integrate their code changes multiple times a day, reducing integration conflicts and catching bugs early.
Testing: Automated tests, including unit tests and integration tests, are run to ensure that new code changes do not introduce regressions.
Quick Feedback: CI provides rapid feedback to developers about the quality and correctness of their code changes.
Continuous Deployment (CD):
CD is the natural extension of CI. It is the practice of automatically and continuously deploying code changes to production or staging environments after successful integration and testing. Key aspects of CD include:
Automation: CD pipelines automate the deployment process, reducing the risk of human error and ensuring consistent deployments.
Deployment to Staging: Code changes are deployed first to a staging environment where further testing and validation occur.
Deployment to Production: After passing all tests in the staging environment, code changes are automatically deployed to the production environment, often with zero downtime.
Rollbacks: In case of issues, CD pipelines provide the ability to rollback to a previous version quickly.
Benefits of CI/CD in Web Development:
Rapid Development: CI/CD accelerates development cycles by automating time-consuming tasks, allowing developers to focus on coding.
Quality Assurance: Automated testing ensures code quality, reducing the number of bugs and regressions.
Consistency: CI/CD ensures that code is built, tested, and deployed consistently, regardless of the development environment.
Continuous Feedback: Developers receive immediate feedback on the impact of their changes, improving collaboration and productivity.
Reduced Risk: Automated deployments reduce the likelihood of deployment errors and downtime, enhancing reliability.
Scalability: CI/CD can scale to accommodate projects of all sizes, from small startups to large enterprises.
A computer with Docker installed (https://docs.docker.com/get-docker/)
Jenkins installed and configured (https://www.jenkins.io/download/)
A web application code repository hosted on GitHub
Step 1: Set Up the Web Application and Git Repository
Create a simple web application or use an existing one. Ensure it can be hosted in a Docker container.
Initialise a Git repository for your web application and push it to GitHub.
Step 2: Install and Configure Jenkins
Install Jenkins on your computer or server following the instructions for your operating system (https://www.jenkins.io/download/).
Open Jenkins in your web browser (usually at http://localhost:8080) and complete the initial setup, including setting up an admin user and installing necessary plugins.
Configure Jenkins to work with Git by setting up Git credentials in the Jenkins Credential Manager.
Step 3: Create a Jenkins Job
Create a new Jenkins job using the "Freestyle project" type.
In the job configuration, specify a name for your job and choose "This project is parameterized."
Add a "String Parameter" named GIT_REPO_URL and set its default value to your Git repository URL.
Set Branches to build -> Branch Specifier to the working Git branch (ex */master)
In the job configuration, go to the "Build Triggers" section and select the "GitHub hook trigger for GITScm polling" option. This enables Jenkins to listen for GitHub webhook triggers.
Step 4: Configure Build Steps
In the job configuration, go to the "Build" section.
Add build steps to execute Docker commands for building and deploying the containerized web application. Use the following commands:
# Remove the existing container if it exists
docker rm --force container1
# Build a new Docker image
docker build -t nginx-image1 .
# Run the Docker container
docker run -d -p 8081:80 --name=container1 nginx-image1
These commands remove the existing container (if any), build a Docker image named "nginx-image1," and run a Docker container named "container1" on port 8081.
Step 5: Set Up a GitHub Webhook
In your GitHub repository, navigate to "Settings" and then "Webhooks."
Create a new webhook, and configure it to send a payload to the Jenkins webhook URL (usually http://jenkins-server/github-webhook/). Set the content type to "application/json."
Step 6: Trigger the CI/CD Pipeline
Push changes to your GitHub repository. The webhook will trigger the Jenkins job automatically, executing the build and deployment steps defined in the job configuration.
Monitor the Jenkins job's progress in the Jenkins web interface.
Step 7: Verify the Deployment
Access your web application by opening a web browser and navigating to http://localhost:8081 (or the appropriate URL if hosted elsewhere).
This experiment demonstrates how to apply CI/CD principles to web development using Jenkins, Git, Docker containers, and GitHub webhooks. By configuring Jenkins to listen for GitHub webhook triggers and executing Docker commands in response to code changes, you can automate the build and deployment of your web application, ensuring a more efficient and reliable development workflow.
Exercise / Questions :
Explain the core principles of Continuous Integration (CI) and Continuous Deployment (CD) in the context of web development. How do these practices enhance the software development lifecycle?
Discuss the key differences between Continuous Integration and Continuous Deployment. When might you choose to implement one over the other in a web development project?
Describe the role of automation in CI/CD. How do CI/CD pipelines automate code integration, testing, and deployment processes?
Explain the concept of a CI/CD pipeline in web development. What are the typical stages or steps in a CI/CD pipeline, and why are they important?
Discuss the benefits of CI/CD for web development teams. How does CI/CD impact the speed, quality, and reliability of software delivery?
What role do version control systems like Git play in CI/CD workflows for web development? How does version control contribute to collaboration and automation?
Examine the challenges and potential risks associated with implementing CI/CD in web development. How can these challenges be mitigated?
Provide examples of popular CI/CD tools and platforms used in web development. How do these tools facilitate the implementation of CI/CD principles?
Explain the concept of "Infrastructure as Code" (IaC) and its relevance to CI/CD. How can IaC be used to automate infrastructure provisioning in web development projects?
Discuss the cultural and organisational changes that may be necessary when adopting CI/CD practices in a web development team. How does CI/CD align with DevOps principles and culture?