Configure Jenkins to execute automated builds on a scheduled basis using cron syntax. The pipeline will:
H 0 * * *
H 8 * * 1
H */6 * * *
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}"
}
}
}
This project ensures continuous integration and automated deployment using Jenkins scheduled builds, Docker, and AWS services.