CI/CD Using Jenkins, GitHub Webhooks, and Deployment on Nginx/Apache

CI/CD Using Jenkins, GitHub Webhooks, and Deployment on Nginx/Apache

ยท

2 min read

Continuous Integration and Continuous Deployment (CI/CD) pipelines ensure automated code testing and deployment, enhancing the development process. This guide walks you through setting up a CI/CD pipeline using Jenkins, GitHub Webhooks, and deploying to an Nginx or Apache server.


Step 1: Create and Update an EC2 Server

  1. Launch an EC2 server from your AWS Management Console.

  2. Update the server packages by running the following commands:

sudo apt update -y

Step 2: Install Nginx

  1. Install the Nginx web server:
sudo apt install nginx -y
  1. Navigate to the Nginx HTML folder and replace the index.html file to display your webpage:
cd /var/www/html

Replace or add your index.html file here.


Step 3: Install Jenkins

Prerequisite: Install Java

Jenkins requires Java to run. Install it using:

sudo apt install jdk-17-jre -y

Install Jenkins

  1. Add the Jenkins key and repository:
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
  1. Update the server and install Jenkins:
sudo apt-get update
sudo apt-get install jenkins -y

Step 4: Configure GitHub Webhooks

  1. Go to your GitHub repository settings.

  2. Under Webhooks, add a new webhook.

  3. Enter the Jenkins webhook URL:

http://<EC2-Public-IP>:8080/github-webhook/

Replace <EC2-Public-IP> with your EC2 server's public IP address. This ensures the Jenkins pipeline is triggered on every new commit.


Step 5: Create a Jenkins Freestyle Job

  1. Log in to Jenkins.

  2. Create a new Freestyle Project.

  3. Configure the Git repository URL for the project. For example:

https://github.com/imkiran13/Irrigation.git
  1. Enable the GitHub hook trigger option in Jenkins.

Step 6: Configure Deployment to Nginx

  1. Add a build step to copy project files to the Nginx HTML folder:
cp -r * /var/www/html/
  1. Run the pipeline. If you encounter a "Permission Denied" error, proceed to the next step.

Step 7: Resolve Permission Denied Error

  1. Grant the Jenkins user permission to access the Nginx folder:
sudo chown -R jenkins:jenkins /var/www/html/
  1. Rerun the pipeline.

Step 8: Verify the Deployment

  1. Open your browser.

  2. Enter your EC2 server's public IP address.

  3. Verify that your deployed webpage is displayed.


Conclusion

You have successfully set up a CI/CD pipeline using Jenkins and GitHub Webhooks, and deployed the application to an Nginx server. This process ensures seamless integration and automated deployment for your web applications.

Feel free to modify this pipeline to suit your project's specific requirements!

ย