-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJenkinsfile
133 lines (119 loc) · 4.77 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
#!groovy
pipeline {
agent{node {label 'master'}}
options {
timestamps ()
ansiColor('xterm')
}
environment {
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
PROJECT_NAME="gradle-demo-with-docker"
GITHUB_CREDENTIAL_ID="Github-credential"
NEXUS_URL="nexus.example.com:8082"
NEXUS_CREDENTIAL_ID="Nexus-credential"
SSH_USER="root"
SSH_PORT="22"
APP_PORT="8083"
}
stages {
stage("Functional test"){
steps{
sh """
echo "[INFO] Run functional test"
./auto/run-functional-test
"""
}
}
stage("Build and Release Image"){
steps{
withCredentials([usernamePassword(credentialsId: "${env.NEXUS_CREDENTIAL_ID}", usernameVariable: 'Nexus_USERNAME', passwordVariable: 'Nexus_PASSWORD')]) {
sh """
echo "[INFO] Login Nexus registry"
./auto/login-nexus ${env.Nexus_USERNAME} ${env.Nexus_PASSWORD} ${env.NEXUS_URL}
echo "[INFO] Build and Release Image"
./auto/release-image
"""
}
}
}
stage("Initialize test env"){
environment {
DEPLOY_ENV="test"
}
steps{
echo "[INFO] Initialize test env"
sh """
echo "Clean up docker resources"
./auto/clean-resource
echo "[INFO] Checking SSH connection:"
./auto/test-ssh-connection ${env.SSH_USER} ${env.DEPLOY_ENV} ${env.SSH_PORT}
echo "[INFO] Checking Disk space:"
./auto/check-resource ${env.SSH_USER} ${env.DEPLOY_ENV} ${env.SSH_PORT}
"""
echo "[INFO] Env is ready to go..."
}
}
stage("Deploy test env via ansible"){
environment {
DEPLOY_ENV="test"
}
steps{
echo "[INFO] Start deploying war to the destination server"
withCredentials([usernamePassword(credentialsId: "${env.NEXUS_CREDENTIAL_ID}", usernameVariable: 'Nexus_USERNAME', passwordVariable: 'Nexus_PASSWORD')]) {
sh "auto/ansible-playbook ${env.PROJECT_NAME} ${env.DEPLOY_ENV} ${env.Nexus_USERNAME} ${env.Nexus_PASSWORD} ${env.NEXUS_URL}"
}
echo "[INFO] Deployment is complete in ${env.DEPLOY_ENV} :)"
}
}
stage("Health Check in test env"){
environment {
DEPLOY_ENV="test"
}
steps{
echo "[INFO] Health check for destination server"
sh "./auto/health-check ${env.DEPLOY_ENV} ${env.APP_PORT} ${env.PROJECT_NAME}"
echo "[INFO] Health check is complete, deployment is accomplished in ${env.DEPLOY_ENV} :)"
input("Start to deploy in prod?")
}
}
stage("Initialize prod env"){
environment {
DEPLOY_ENV="prod"
}
steps{
echo "[INFO] Initialize prod env"
sh """
echo "Clean up docker resources in build agent"
./auto/clean-resource
echo "[INFO] Checking SSH connection:"
./auto/test-ssh-connection ${env.SSH_USER} ${env.DEPLOY_ENV} ${env.SSH_PORT}
echo "[INFO] Checking Disk space:"
./auto/check-resource ${env.SSH_USER} ${env.DEPLOY_ENV} ${env.SSH_PORT}
"""
echo "[INFO] Env is ready to go..."
}
}
stage("Deploy prod env via ansible"){
environment {
DEPLOY_ENV="prod"
}
steps{
echo "[INFO] Start deploying war to the destination server"
withCredentials([usernamePassword(credentialsId: "${env.NEXUS_CREDENTIAL_ID}", usernameVariable: 'Nexus_USERNAME', passwordVariable: 'Nexus_PASSWORD')]) {
sh "auto/ansible-playbook ${env.PROJECT_NAME} ${env.DEPLOY_ENV} ${env.Nexus_USERNAME} ${env.Nexus_PASSWORD} ${env.NEXUS_URL}"
}
echo "[INFO] Deployment is complete in ${env.DEPLOY_ENV} :)"
}
}
stage("Health Check in prod env"){
environment {
DEPLOY_ENV="prod"
}
steps{
echo "[INFO] Health check for destination server"
sh "./auto/health-check ${env.DEPLOY_ENV} ${env.APP_PORT} ${env.PROJECT_NAME}"
echo "[INFO] Health check is complete, deployment is accomplished in ${env.DEPLOY_ENV} :)"
}
}
}
}