-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
175 lines (120 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
() () || | | |
/\ _ _ __ /\/|| | __ | | _
/ \/ |/ | / \_| | |_/ ||/ / \_|/ \_|/
/(__/ | |_/\__/ \/ \/ /(__/ |__/\__/ \_/ |__/
*/
import groovy.transform.Field
@Field
def buildEnv;
//////////////////////////////////////////////////////
node {
slackSend channel: "#snowglobe", message: "Build commenced - ${env.JOB_NAME} ${env.BUILD_NUMBER}"
stage(name:"Checkout") {
checkout scm;
buildEnv = load 'jenkins/buildEnv.groovy';
buildEnv(currentBuild);
}
stage(name:"Build") {
build()
}
stage(name:"Publish") {
publishDocker()
}
stage(name:"Deploy") {
if( buildEnv.isPR() ) {
// Pull requests : Build a test environment
def testurl = spinUpTestEnvironment();
slackSend channel: "#snowglobe", message: "Docker :whale: test environment :snowflake: available now - ${env.JOB_NAME} ${env.BUILD_NUMBER} - ${testurl}"
commentOnPR(buildEnv.PR(), "Test environment :snowflake: available now : ${testurl}" )
} else {
// Master builds : Continuously Deploy
updateDeployedVersion();
slackSend channel: "#snowglobe", message: "Docker :whale: LIVE ENV UPDATED - ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
slackSend channel: "#snowglobe", message: "Build complete OK - ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
def build() {
def mvnHome = tool 'latest'
env.MAVEN_OPTS="-Xmx2G";
withJavaEnv {
sh "${mvnHome}/bin/mvn -DskipTests clean install"
}
}
def publishDocker() {
// Prepare
sh './prod/build.sh';
if( buildEnv.isPR() ) {
String tags = buildEnv.buildTags("dev.nirima.com", "snowglobe");
def image = docker.build(tags, "-f prod/Dockerfile prod");
withDockerRegistry([ credentialsId: "registry_nirima", url: "" ]) {
image.push();
image.push('latest');
}
}
else {
String tags = "nirima/snowglobe:${env.BUILD_NUMBER}";
def image = docker.build(tags, "-f prod/Dockerfile prod");
withDockerRegistry([ credentialsId: "docker_hub", url: "" ]) {
image.push();
image.push('latest');
}
// Also in release push docker images for snowglobe-autopilot
tags = "nirima/snowglobe-autopilot:${env.BUILD_NUMBER}";
image = docker.build(tags, "-f prod/Dockerfile.autopilot prod");
withDockerRegistry([ credentialsId: "docker_hub", url: "" ]) {
image.push();
image.push('latest');
}
// Also in release push nginx images
tags = "nirima/nginx-snowglobe-autopilot:${env.BUILD_NUMBER}";
image = docker.build(tags, "-f prod/nginx/Dockerfile prod/nginx");
withDockerRegistry([ credentialsId: "docker_hub", url: "" ]) {
image.push();
image.push('latest');
}
}
}
def updateDeployedVersion() {
// Stage 1: Just re-apply a snowglobe
snowglobe_apply globeId: 'snowglobe-cd'
}
def spinUpTestEnvironment() {
String name1 = "${buildEnv.branch}".replace("/","-").toLowerCase();
String properties = """name="${name1}"
build=${env.BUILD_NUMBER}""";
String name = "${name1}-${env.BUILD_NUMBER}";
snowglobe_clone createAction: true, sourceId: 'snowglobe-ci-template', targetId: name
snowglobe_set_variables globeId: name, variables: properties
snowglobe_apply globeId: name
def response = snowglobe_state globeId: name
//def info_response = readJSON text: response.content;
def info_response = readJSON text: response;
def ip = info_response['modules']['base']['resources']['docker_container_info']['realtime']['items']['info']['NetworkSettings']['IPAddress'];
return "http://${ip}:8888/";
}
void withJavaEnv(List envVars = [], def body) {
String npmTool = tool name: 'Node', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'
echo npmTool;
List javaEnv = ["PATH+JDK=${npmTool}/bin"]
// Add any additional environment variables.
javaEnv.addAll(envVars)
// Invoke the body closure we're passed within the environment we've created.
withEnv(javaEnv) {
body.call()
}
}
def commentOnPR(String pullRequestNumber, String comment) {
def yourCredential = 'github'
def yourOrga = "nirima"
def yourRepo = "SnowGlobe"
def body="""{
"body": "${comment}",
"path": "/",
"position": 0
}"""
httpRequest authentication: "${yourCredential}", httpMode: 'POST',
requestBody: body,
url: "https://api.github.com/repos/${yourOrga}/${yourRepo}/issues/${pullRequestNumber}/comments"
}