Site Search:

DevOps Simplified

Jenkins CI/CD Pipeline – Setup Guide with AWS Deployment

Jenkins CI/CD Pipeline – Automate Your Software Deployment

Introduction

In today’s fast-paced software development world, automation is key. A Jenkins CI/CD pipeline helps developers automate the process of building, testing, and deploying applications, ensuring faster and more reliable software releases.

In this guide, we’ll walk through the fundamentals of a Jenkins CI/CD pipeline and provide step-by-step instructions on how to set it up for server and AWS cloud deployment.

What is a Jenkins CI/CD Pipeline?

A Jenkins CI/CD pipeline is an automated workflow that handles software integration, testing, and deployment. It follows the principles of:

  • Continuous Integration (CI): Automatically builds and tests code after every commit.
  • Continuous Deployment (CD): Deploys the application to staging or production automatically if all tests pass.

This ensures that every code change is validated and deployed with minimal human intervention.

How Jenkins CI/CD Pipelines Work

A typical Jenkins pipeline consists of four main stages:

  1. Source (Version Control): Jenkins pulls code from GitHub, GitLab, or Bitbucket.
  2. Build: Jenkins compiles or packages the application.
  3. Test: Runs unit tests and integration tests.
  4. Deploy: Deploys the application to a server, cloud, or containerized environment.

Example 1: Deploying a Java Application Using Jenkins CI/CD

This example shows a Jenkins pipeline that automates deployment to a remote Linux server.

Jenkinsfile for Server Deployment

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/user/repository.git'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                sh 'scp target/myapp.war user@server:/var/www/myapp'
            }
        }
    }
}
    

How It Works:

  • Jenkins pulls the latest code from GitHub.
  • The application is built using Maven.
  • Automated tests are executed.
  • If all tests pass, Jenkins deploys the app to a remote Linux server using SCP.

Example 2: Deploying to AWS EC2 Using Jenkins CI/CD

Let’s extend our pipeline to deploy a Java application to an AWS EC2 instance.

Jenkinsfile for AWS Deployment

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/user/repository.git'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Deploy to AWS') {
            steps {
                sshagent(['aws-key']) {
                    sh '''
                    scp -i ~/.ssh/aws-key.pem target/myapp.war ec2-user@ec2-instance-ip:/var/www/myapp
                    ssh -i ~/.ssh/aws-key.pem ec2-user@ec2-instance-ip "sudo systemctl restart tomcat"
                    '''
                }
            }
        }
    }
}
    

How to Set Up Jenkins for CI/CD Deployment

Step 1: Install Jenkins

Run the following commands to install Jenkins on Ubuntu:

sudo apt update
sudo apt install openjdk-11-jdk -y
sudo apt install jenkins -y
sudo systemctl start jenkins
sudo systemctl enable jenkins
    

Step 2: Install Required Plugins

Go to Jenkins Dashboard → Manage Jenkins → Manage Plugins and install:

  • Pipeline
  • Git Plugin
  • SSH Agent Plugin
  • Maven Integration Plugin
  • Amazon EC2 Plugin

Step 3: Create a New Pipeline Job

1. Go to Jenkins Dashboard → New Item → Pipeline.

2. Under Pipeline, choose Pipeline script from SCM and enter your GitHub repository URL.

3. In the Script Path, enter: Jenkinsfile

Step 4: Set Up SSH for Deployment

To deploy using SSH, generate an SSH key and copy it to the target server:

ssh-keygen -t rsa -b 4096 -C "jenkins@server"
ssh-copy-id -i ~/.ssh/id_rsa.pub user@server-ip
    

Step 5: Trigger the Pipeline

Click Build Now in Jenkins to run the pipeline.

Step 6: Automate Deployment with Webhooks

To trigger Jenkins automatically on GitHub pushes:

  • Go to your GitHub repository → Settings → Webhooks.
  • Add a webhook with this URL: http://your-jenkins-server-ip:8080/github-webhook/.

Conclusion

By following these steps, you have successfully set up a Jenkins CI/CD pipeline that automates software deployment to a Linux server or AWS EC2 instance.

Want to take it further? Try integrating Docker and Kubernetes for containerized deployment.

Let me know in the comments if you need more details or troubleshooting tips! 🚀

No comments:

Post a Comment