-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
154 lines (147 loc) · 5.74 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
pipeline {
agent {
label 'jenkins-host'
}
environment {
PREVIOUS_VERSION = '1.14.32.1' // Version before update
MC_VERSION = '1.14.60.5' // We will update server to this version
MAJOR_VERSION = '1.14'
MY_EMAIL = credentials("my_gmail")
// Used in vm_setup.sh and vm_halt.sh
// VAGRANT_PROJECT_PATH = "/root/vagrant_projects/mc_centos7"
}
stages {
stage('Build') {
when {
branch "dev"
}
steps {
retry (3) {
// Tag a specific version like 1.14.32.1
sh "./jenkins/build.sh ${MC_VERSION}"
}
}
}
// Push test image (new version of game server) to Docker Hub
stage('Push stage 1') {
when {
branch "dev"
}
environment {
DOCKER_HUB = credentials("Docker_hub_herealways")
}
steps {
// Tag specific version, major version and latest.
retry(3) {
sh "./jenkins/push_test.sh ${MC_VERSION}"
}
}
}
// Setup test environment
stage('Test') {
when {
branch "dev"
}
steps {
// sh './jenkins/vm_setup.sh'
sh 'ansible-galaxy install -r requirements.yml --force'
withCredentials([sshUserPrivateKey(credentialsId: 'ansible_key',\
keyFileVariable: 'ANSIBLE_KEY')]) {
ansiblePlaybook(playbook: 'mc_server.yml',\
inventory: '/root/ansible-playbooks/playbooks/mc_bedrock-server/ansible_inventory/remote_test_server',\
//credentialsId: "${ANSIBLE_KEY}",\
tags: 'deploy',\
extraVars: [MC_VERSION: "${PREVIOUS_VERSION}"],\
hostKeyChecking : false,\
colorized: true,\
extras: "--private-key ${ANSIBLE_KEY}")
ansiblePlaybook(playbook: 'mc_server.yml',\
inventory: '/root/ansible-playbooks/playbooks/mc_bedrock-server/ansible_inventory/remote_test_server',\
//credentialsId: "${ANSIBLE_KEY}",\
tags: 'update',\
extraVars: [MC_VERSION: "${MC_VERSION}"],\
hostKeyChecking : false,\
colorized: true,\
extras: "--private-key ${ANSIBLE_KEY}")
input message: "Did the test pass? Should we continue?"
}
}
}
stage('Stop test server') {
input {
message '''Should we stop test server?
(Only works on dev branch.
We can safely proceed if the branch is not master)'''
ok "Choose"
parameters {
choice(name: 'IF_STOP_TEST_SERVER', choices: [true, false], description: '')
}
}
when {
branch 'dev'
environment name: 'IF_STOP_TEST_SERVER', value: 'true'
}
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'ansible_key',\
keyFileVariable: 'ANSIBLE_KEY')]) {
ansiblePlaybook(playbook: 'mc_server.yml',\
inventory: '/root/ansible-playbooks/playbooks/mc_bedrock-server/ansible_inventory/remote_test_server',\
//credentialsId: "${ANSIBLE_KEY}",\
tags: 'halt',\
extraVars: [MC_VERSION: "${MC_VERSION}"],\
hostKeyChecking : false,\
colorized: true,\
extras: "--private-key ${ANSIBLE_KEY}")
}
}
}
// Tag and push tested docker image to Docker Hub
stage('Push stage 2') {
when {
branch "master"
}
input {
message '''Should we push docker image and update production server?
(It only works when branch is master.
We can safely proceed if the branch is not master)'''
submitter "here"
ok "Proceed "
}
environment {
DOCKER_HUB = credentials("Docker_hub_herealways")
}
steps {
retry(3) {
sh "./jenkins/push.sh ${MC_VERSION} ${MAJOR_VERSION}"
}
}
}
stage('Deploy') {
when {
branch "master"
}
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'ansible_key',\
keyFileVariable: 'ANSIBLE_KEY')]) {
ansiblePlaybook(playbook: 'mc_server.yml',\
inventory: '/root/ansible-playbooks/playbooks/mc_bedrock-server/ansible_inventory/production_server',\
//credentialsId: "${ANSIBLE_KEY}",\
tags: 'update',\
extras: "--private-key ${ANSIBLE_KEY}",\
extraVars: [MC_VERSION: "${MC_VERSION}"],\
hostKeyChecking : false,\
colorized: true)
}
}
}
}
post {
always {
// sh './jenkins/vm_halt.sh'
emailext subject: "${env.JOB_NAME} - Branch: ${env.BRANCH_NAME} - Build # ${env.BUILD_NUMBER} - ${currentBuild.currentResult}!",
body: """${env.JOB_NAME} - Branch: ${env.BRANCH_NAME} - Build # ${env.BUILD_NUMBER} - ${currentBuild.currentResult}:
Check console output at ${env.BUILD_URL} to view the results.""",
to: "${MY_EMAIL}"
}
}
}