Skip to main content

Command Palette

Search for a command to run...

πŸš€ Day 10 of 30 Days of DevOps Interview Prep – Jenkins Git Trigger + Build/Test/Deploy Pipeline

Published
β€’3 min read

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 UI

    • Port 22 β†’ SSH

    • App 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

  1. Go to Jenkins Dashboard β†’ New Item

  2. Select Pipeline β†’ Name it GitTriggeredPipeline

  3. Under Build Triggers β†’ Select:

    • βœ… GitHub hook trigger for GITScm polling
  4. Under Pipeline β†’ Select Pipeline script from SCM

  5. SCM β†’ Git β†’ Enter repo URL

  6. 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/json

  • Select: 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.pem with 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

More from this blog

Cloud Enthusiast

116 posts