-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathJenkinsfile
More file actions
137 lines (126 loc) · 5.37 KB
/
Copy pathJenkinsfile
File metadata and controls
137 lines (126 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def getDockerTag(){
def tag = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
return tag
}
pipeline {
agent { label 'linux' }
environment {
Docker_tag = getDockerTag()
account_id = "941277531445" // Replace with your actual AWS account ID
region = "ap-south-1" // Replace with your desired AWS region
cluster_name = "jenkins-k8s" // Replace with your EKS cluster name
}
stages {
stage('Validation & Checks') {
parallel {
stage('Commit Message Validation') {
steps {
script {
// Extract the latest commit message and save it to a temp file
def commitMsg = sh(script: 'git log -1 --pretty=%B', returnStdout: true).trim()
writeFile file: 'commit_msg.txt', text: commitMsg
echo "Validating commit message..."
// Execute the verification script (assumed to be in your repo at scripts/check_commit.sh)
// If the script exits with status 1, the pipeline will fail here.
sh "chmod +x scripts/check_commit.sh"
// Catch errors from the validation script to mark the stage UNSTABLE instead of FAILURE
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
sh "./scripts/check_commit.sh commit_msg.txt"
}
}
}
}
stage('Check Dependencies') {
steps {
script {
echo "Verifying external service availability..."
sh "chmod +x scripts/check_dependencies.sh"
sh "./scripts/check_dependencies.sh"
}
}
}
}
}
stage('Static code analysis') {
steps {
script {
// Securely inject the credentials you created
withSonarQubeEnv(credentialsId: 'sonarqube-token') {
sh "mvn sonar:sonar"
}
timeout(time: 1, unit: 'HOURS') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
}
stage('Build & Test') {
steps {
script {
echo "Building the application..."
sh "mvn clean install"
}
}
}
stage('docker build') {
steps {
script {
echo "Building Docker image..."
sh "docker build -t myapp:${Docker_tag} ."
}
}
}
stage('Authentication of ECR and push the image') {
steps {
script {
echo "Authenticating to ECR..."
sh "aws ecr get-login-password --region ${region} | docker login --username AWS --password-stdin ${account_id}.dkr.ecr.${region}.amazonaws.com"
echo "Pushing Docker image to ECR..."
sh "docker tag myapp:${Docker_tag} ${account_id}.dkr.ecr.${region}.amazonaws.com/myapp:${Docker_tag}"
sh "docker push ${account_id}.dkr.ecr.${region}.amazonaws.com/myapp:${Docker_tag}"
}
}
}
stage('prepare manifest files and check connection with k8s cluster') {
agent {
dockerfile {
filename 'Dockerfile-Jenkins' // Specify the Dockerfile to use for the Jenkins agent
// The args line bypasses entrypoint blocks so Jenkins can run sh commands
args '--entrypoint=""'
reuseNode true
}
// docker {
// image 'bitnami/kubectl:latest'
// args '--entrypoint=""'
// reuseNode true // Ensures it runs on the same 'linux' workspace node
// }
}
environment {
// Point HOME to the current workspace directory so plugins/tools have write permissions
HOME = "${WORKSPACE}"
}
steps {
script {
echo "Deploying to Kubernetes..."
sh "ls -l"
sh "sed -i 's|image_name|${account_id}.dkr.ecr.${region}.amazonaws.com/myapp:${Docker_tag}|g' deployment.yaml"
// Check connection to Kubernetes cluster
echo "Checking connection to Kubernetes cluster..."
sh "aws eks update-kubeconfig --region ${region} --name ${cluster_name}"
sh "kubectl get po"
sh "kubectl apply -f deployment.yaml"
sh "kubectl rollout status deployment/devops-training"
}
}
}
}
post {
always {
echo 'Cleaning up...'
cleanWs()
}
}
}