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
Launch an EC2 server from your AWS Management Console.
Update the server packages by running the following commands:
sudo apt update -y
Step 2: Install Nginx
- Install the Nginx web server:
sudo apt install nginx -y
- 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
- 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
- Update the server and install Jenkins:
sudo apt-get update
sudo apt-get install jenkins -y
Step 4: Configure GitHub Webhooks
Go to your GitHub repository settings.
Under Webhooks, add a new webhook.
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
Log in to Jenkins.
Create a new Freestyle Project.
Configure the Git repository URL for the project. For example:
https://github.com/imkiran13/Irrigation.git
- Enable the GitHub hook trigger option in Jenkins.
Step 6: Configure Deployment to Nginx
- Add a build step to copy project files to the Nginx HTML folder:
cp -r * /var/www/html/
- Run the pipeline. If you encounter a "Permission Denied" error, proceed to the next step.
Step 7: Resolve Permission Denied Error
- Grant the Jenkins user permission to access the Nginx folder:
sudo chown -R jenkins:jenkins /var/www/html/
- Rerun the pipeline.
Step 8: Verify the Deployment
Open your browser.
Enter your EC2 server's public IP address.
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!