forked from cvamsikrishna11/devops-fully-automated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
154 lines (138 loc) · 5.38 KB
/
Copy pathJenkinsfile
File metadata and controls
154 lines (138 loc) · 5.38 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def COLOR_MAP = [
'SUCCESS': 'good',
'FAILURE': 'danger',
]
pipeline {
agent any
environment {
WORKSPACE = "${env.WORKSPACE}"
}
tools {
maven 'localMaven'
}
stages {
stage('Git checkout') {
steps {
echo 'Cloning the application code...'
git branch: 'main', url: 'https://github.com/harsh2503/devops-fully-automated.git'
}
}
stage('Checkstyle Code Analysis') {
steps {
sh 'mvn checkstyle:checkstyle'
}
post {
success {
echo 'Checkstyle analysis completed successfully.'
}
failure {
echo 'Checkstyle violations found.'
}
}
}
stage('Clean and Compile') {
steps {
sh 'mvn -U clean compile'
}
}
stage('Test') {
steps {
sh 'mvn test'
junit '**/target/surefire-reports/*.xml'
}
}
stage('Package') {
steps {
sh 'mvn package -DskipTests'
}
post {
success {
echo 'Package completed. Archiving artifacts...'
archiveArtifacts artifacts: '**/target/*.war', followSymlinks: false
}
failure {
echo 'Package failed. Skipping artifact archival.'
}
}
}
stage('SonarQube scanning') {
steps {
withSonarQubeEnv('SonarQube') {
withCredentials([string(credentialsId: 'sonarqube-token', variable: 'SONAR_TOKEN')]) {
sh """
mvn sonar:sonar \
-Dsonar.projectKey=maven \
-Dsonar.host.url=http://172.31.53.174:9000 \
-Dsonar.login=$SONAR_TOKEN
"""
}
}
}
}
stage('Quality Gate') {
steps {
waitForQualityGate abortPipeline: true
}
}
stage('Upload artifact to Nexus') {
steps {
withCredentials([usernamePassword(credentialsId: 'nexus-credentials', passwordVariable: 'PASSWORD', usernameVariable: 'USER_NAME')]) {
sh "sed -i \"s/.*<username><\\/username>/<username>$USER_NAME<\\/username>/g\" ${WORKSPACE}/nexus-setup/settings.xml"
sh "sed -i \"s/.*<password><\\/password>/<password>$PASSWORD<\\/password>/g\" ${WORKSPACE}/nexus-setup/settings.xml"
sh 'cp ${WORKSPACE}/nexus-setup/settings.xml /var/lib/jenkins/.m2'
sh 'mvn deploy -DskipTests'
}
}
post {
success {
echo 'Arfiacts has been backed up onto Nexus..!'
}
failure {
echo 'Artifact upload failed hence removing the settings.xml file which might cause issues on the check-style'
sh 'sudo rm -f /var/lib/jenkins/.m2/settings.xml'
}
}
}
stage('Deploy to DEV env') {
environment {
HOSTS = 'dev'
}
steps {
withCredentials([usernamePassword(credentialsId: 'ansible-deploy-server-credentials', passwordVariable: 'PASSWORD', usernameVariable: 'USER_NAME')]) {
sh "ansible-playbook -i ${WORKSPACE}/ansible-setup/aws_ec2.yaml ${WORKSPACE}/deploy.yaml --extra-vars \"ansible_user=$USER_NAME ansible_password=$PASSWORD hosts=tag_Environment_$HOSTS workspace_path=$WORKSPACE\""
}
}
}
stage('Deploy to STAGE env') {
environment {
HOSTS = 'stage'
}
steps {
withCredentials([usernamePassword(credentialsId: 'ansible-deploy-server-credentials', passwordVariable: 'PASSWORD', usernameVariable: 'USER_NAME')]) {
sh "ansible-playbook -i ${WORKSPACE}/ansible-setup/aws_ec2.yaml ${WORKSPACE}/deploy.yaml --extra-vars \"ansible_user=$USER_NAME ansible_password=$PASSWORD hosts=tag_Environment_$HOSTS workspace_path=$WORKSPACE\""
}
}
}
stage('Approval') {
steps {
input('Do you want to proceed?')
}
}
stage('Deploy to PROD env') {
environment {
HOSTS = 'prod'
}
steps {
withCredentials([usernamePassword(credentialsId: 'ansible-deploy-server-credentials', passwordVariable: 'PASSWORD', usernameVariable: 'USER_NAME')]) {
sh "ansible-playbook -i ${WORKSPACE}/ansible-setup/aws_ec2.yaml ${WORKSPACE}/deploy.yaml --extra-vars \"ansible_user=$USER_NAME ansible_password=$PASSWORD hosts=tag_Environment_$HOSTS workspace_path=$WORKSPACE\""
}
}
}
}
post {
always {
echo 'I will always say Hello again!'
slackSend channel: '#team-devops', color: COLOR_MAP[currentBuild.currentResult], message: "*${currentBuild.currentResult}:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} \n More info at: ${env.BUILD_URL}"
}
}
}