Project: Scheduled Builds in Jenkins using Cron Syntax

Project Objective

Configure Jenkins to execute automated builds on a scheduled basis using cron syntax. The pipeline will:

Technology Stack

Implementation Steps

Step 1: Set Up Jenkins

  1. Install Jenkins on an AWS EC2 instance or on-premise.
  2. Install the following Jenkins plugins:

Step 2: Configure GitHub Repository

  1. Create a GitHub repository for the application.
  2. Push the application code along with a Jenkinsfile defining the pipeline.

Step 3: Create Jenkins Pipeline with Scheduled Builds

  1. Navigate to Jenkins Dashboard → New Item → Select Pipeline.
  2. Add the GitHub repository as the source code.
  3. In the Build Triggers section, enable "Build periodically" and define the cron syntax.

Example cron expressions:

Step 4: Write the Jenkinsfile

Below is the Jenkinsfile to automate the build, test, and deployment process:

pipeline {
    agent any

    environment {
        IMAGE_NAME = 'your-dockerhub-username/app'
        AWS_REGION = 'us-east-1'
    }

    triggers {
        cron('H 0 * * *') // Runs every midnight
    }

    stages {
        stage('Checkout Code') {
            steps {
                git branch: 'main', url: 'https://github.com/your-repo.git'
            }
        }

        stage('Code Quality Check') {
            steps {
                script {
                    sh 'mvn sonar:sonar -Dsonar.host.url=http://sonarqube-server:9000'
                }
            }
        }

        stage('Build Application') {
            steps {
                script {
                    sh 'mvn clean package -DskipTests'
                }
            }
        }

        stage('Build & Push Docker Image') {
            steps {
                script {
                    sh 'docker build -t $IMAGE_NAME .'
                    sh 'docker login -u your-dockerhub-username -p your-dockerhub-password'
                    sh 'docker push $IMAGE_NAME'
                }
            }
        }

        stage('Deploy to AWS ECS') {
            steps {
                script {
                    sh 'aws ecs update-service --cluster your-cluster --service your-service --force-new-deployment --region $AWS_REGION'
                }
            }
        }
    }

    post {
        success {
            slackSend channel: '#devops-alerts', message: "Build Successful: ${env.JOB_NAME} - ${env.BUILD_NUMBER}"
        }
        failure {
            slackSend channel: '#devops-alerts', message: "Build Failed: ${env.JOB_NAME} - ${env.BUILD_NUMBER}"
        }
    }
}

Step 5: Configure AWS ECS for Deployment

  1. Create an ECS Cluster with Fargate.
  2. Define a Task Definition that pulls the Docker image from Docker Hub.
  3. Set up an ECS Service that runs the application container.
  4. Allow Jenkins to update ECS using IAM roles.

Step 6: Run and Monitor Scheduled Builds

  1. The Jenkins job will run automatically based on the configured cron schedule.
  2. Monitor build logs and results in Jenkins Console Output.
  3. Check Slack notifications for build status updates.

Outcome

This project ensures continuous integration and automated deployment using Jenkins scheduled builds, Docker, and AWS services.