π Day 10 of 30 Days of DevOps Interview Prep β Jenkins Git Trigger + Build/Test/Deploy Pipeline
Series: 30 Days DevOps Interview Preparation
Todayβs topic is one of the most important real-world DevOps skills:
β
Triggering Jenkins builds from Git
β
Creating a multi-stage CI/CD pipeline (Build β Test β Deploy)
β
Running everything on AWS EC2
π Understanding Jenkins Git Triggers
Jenkins can automatically start a build when you push code to a Git repository.
This is achieved using SCM Polling or Webhooks.
SCM Polling β Jenkins checks your repo periodically for changes.
Webhook β GitHub/GitLab sends a push notification to Jenkins instantly (recommended for faster pipelines).
π AWS Practical β Setting up Jenkins with Git Trigger and Build/Test/Deploy
Step 1 β Launch AWS EC2 Instance
AMI: Ubuntu 22.04 (or Amazon Linux 2)
Instance Type: t2.micro (for testing)
Security Group: Allow:
Port
8080β Jenkins UIPort
22β SSHApp port (e.g., 8081 for deployment)
Step 2 β Install Jenkins
sudo apt update
sudo apt install openjdk-17-jdk -y
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
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 update
sudo apt install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
Unlock Jenkins:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Login at: http://<EC2-Public-IP>:8080
Step 3 β Install Maven & Git
sudo apt install maven git -y
Step 4 β Prepare a Sample Java Application
git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
mvn clean package
Step 5 β Configure Jenkins Job
Go to Jenkins Dashboard β New Item
Select Pipeline β Name it
GitTriggeredPipelineUnder Build Triggers β Select:
- β GitHub hook trigger for GITScm polling
Under Pipeline β Select Pipeline script from SCM
SCM β Git β Enter repo URL
Script Path β
Jenkinsfile
Step 6 β Add Webhook in GitHub
Go to your GitHub repo β Settings β Webhooks β Add webhook
Payload URL:
http://<EC2-Public-IP>:8080/github-webhook/Content type:
application/jsonSelect: Just the push event
π Tested Runnable Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
sh '''
echo "Deploying to AWS EC2..."
scp -o StrictHostKeyChecking=no -i /home/ubuntu/my-key.pem \
target/*.jar ubuntu@<DEPLOY_SERVER_IP>:/home/ubuntu/app.jar
ssh -o StrictHostKeyChecking=no -i /home/ubuntu/my-key.pem \
ubuntu@<DEPLOY_SERVER_IP> "nohup java -jar /home/ubuntu/app.jar > app.log 2>&1 &"
'''
}
}
}
}
Make sure:
Replace
/home/ubuntu/my-key.pemwith your SSH key path.Replace
<DEPLOY_SERVER_IP>with the server where you want to deploy.
π‘ Common Interview Questions
1οΈβ£ How do you configure a Git webhook in Jenkins?
β Set up a webhook in your Git repo pointing to http://<JENKINS_URL>/github-webhook/ and enable the GitHub trigger in the job.
2οΈβ£ Difference between Poll SCM and Webhook?
β Poll SCM checks periodically β slower. Webhook pushes changes instantly β faster.
3οΈβ£ How do you pass artifacts between stages in Jenkins?
β Use stash/unstash or archive artifacts with archiveArtifacts.
4οΈβ£ How do you deploy securely from Jenkins?
β Store credentials in Jenkins Credentials Manager, never hardcode keys in Jenkinsfile.
5οΈβ£ How do you rollback a deployment in Jenkins?
β Keep previous build artifacts stored and redeploy them on rollback.
β Key Takeaways
Webhooks = instant builds, faster feedback.
Separate Build, Test, Deploy stages = cleaner pipelines.
AWS EC2 is a perfect low-cost environment to test Jenkins CI/CD.
#DevOps #AWS #Jenkins #CI_CD #GitHub #Automation #LearningInPublic #InterviewPreparation #30DaysOfDevOps




