-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
139 lines (138 loc) · 4.51 KB
/
Jenkinsfile
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
def COLOR_MAP = [
'SUCCESS': 'good',
'FAILURE': 'danger',
'UNSTABLE': 'danger'
]
pipeline {
agent any
environment {
WORKSPACE = "${env.WORKSPACE}"
NEXUS_CREDENTIAL_ID = 'Nexus-Credential'
//NEXUS_USER = "$NEXUS_CREDS_USR"
//NEXUS_PASSWORD = "$Nexus-Token"
//NEXUS_URL = "172.31.18.62:8081"
//NEXUS_REPOSITORY = "maven_project"
//NEXUS_REPO_ID = "maven_project"
//ARTVERSION = "${env.BUILD_ID}"
}
tools {
maven 'localMaven'
jdk 'localJdk'
}
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
post {
success {
echo ' now Archiving '
archiveArtifacts artifacts: '**/*.war'
}
}
}
stage('Unit Test'){
steps {
sh 'mvn test'
}
}
stage('Integration Test'){
steps {
sh 'mvn verify -DskipUnitTests'
}
}
stage ('Checkstyle Code Analysis'){
steps {
sh 'mvn checkstyle:checkstyle'
}
post {
success {
echo 'Generated Analysis Result'
}
}
}
stage('SonarQube Inspection') {
steps {
withSonarQubeEnv('SonarQube') {
withCredentials([string(credentialsId: 'SonarQube-Token', variable: 'SONAR_TOKEN')]) {
sh """
mvn sonar:sonar \
-Dsonar.projectKey=cicd-pipeline-project \
-Dsonar.host.url=http://172.31.23.58:9000 \
-Dsonar.login=$SONAR_TOKEN
"""
}
}
}
}
stage('SonarQube GateKeeper') {
steps {
timeout(time : 1, unit : 'HOURS'){
waitForQualityGate abortPipeline: true
}
}
}
stage("Nexus Artifact Uploader"){
steps{
nexusArtifactUploader(
nexusVersion: 'nexus3',
protocol: 'http',
nexusUrl: '172.31.16.85:8081',
groupId: 'webapp',
version: "${env.BUILD_ID}-${env.BUILD_TIMESTAMP}",
repository: 'maven-project-releases', //"${NEXUS_REPOSITORY}",
credentialsId: "${NEXUS_CREDENTIAL_ID}",
artifacts: [
[artifactId: 'webapp',
classifier: '',
file: "${WORKSPACE}/webapp/target/webapp.war",
type: 'war']
]
)
}
}
stage('Deploy to Development Env') {
environment {
HOSTS = 'dev'
}
steps {
withCredentials([usernamePassword(credentialsId: 'Ansible-Credential', passwordVariable: 'PASSWORD', usernameVariable: 'USER_NAME')]) {
sh "ansible-playbook -i ${WORKSPACE}/ansible-config/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 Staging Env') {
environment {
HOSTS = 'stage'
}
steps {
withCredentials([usernamePassword(credentialsId: 'Ansible-Credential', passwordVariable: 'PASSWORD', usernameVariable: 'USER_NAME')]) {
sh "ansible-playbook -i ${WORKSPACE}/ansible-config/aws_ec2.yaml ${WORKSPACE}/deploy.yaml --extra-vars \"ansible_user=$USER_NAME ansible_password=$PASSWORD hosts=tag_Environment_$HOSTS workspace_path=$WORKSPACE\""
}
}
}
stage('Quality Assurance Approval') {
steps {
input('Do you want to proceed?')
}
}
stage('Deploy to Production Env') {
environment {
HOSTS = 'prod'
}
steps {
withCredentials([usernamePassword(credentialsId: 'Ansible-Credential', passwordVariable: 'PASSWORD', usernameVariable: 'USER_NAME')]) {
sh "ansible-playbook -i ${WORKSPACE}/ansible-config/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 'Slack Notifications.'
slackSend channel: '#cicd-pipeline-project-alerts', //update and provide your channel name
color: COLOR_MAP[currentBuild.currentResult],
message: "*${currentBuild.currentResult}:* Job Name '${env.JOB_NAME}' build ${env.BUILD_NUMBER} \n Build Timestamp: ${env.BUILD_TIMESTAMP} \n Project Workspace: ${env.WORKSPACE} \n More info at: ${env.BUILD_URL}"
}
}
}