forked from pixie-io/docs.px.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
180 lines (158 loc) · 5.04 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
176
177
178
179
180
/**
* Jenkins build definition. This file defines the entire build pipeline.
*/
import java.net.URLEncoder;
import groovy.json.JsonBuilder
import jenkins.model.Jenkins
DEPLOY_URL = ""
DEPLOY_LOGS = ""
/**
* PhabConnector handles all communication with phabricator if the build
* was triggered by a phabricator run.
*/
class PhabConnector {
def jenkinsCtx
def URL
def repository
def apiToken
def phid
def PhabConnector(jenkinsCtx, URL, repository, apiToken, phid) {
this.jenkinsCtx = jenkinsCtx
this.URL = URL
this.repository = repository
this.apiToken = apiToken
this.phid = phid
}
def harborMasterUrl(method) {
def url = "${URL}/api/${method}?api.token=${apiToken}" +
"&buildTargetPHID=${phid}"
return url
}
def sendBuildStatus(build_status) {
def url = this.harborMasterUrl("harbormaster.sendmessage")
def body = "type=${build_status}"
jenkinsCtx.httpRequest consoleLogResponseBody: true,
contentType: 'APPLICATION_FORM',
httpMode: 'POST',
requestBody: body,
responseHandle: 'NONE',
url: url,
validResponseCodes: '200'
}
def addArtifactLink(linkURL, artifactKey, artifactName) {
def encodedDisplayUrl = URLEncoder.encode(linkURL, 'UTF-8')
def url = this.harborMasterUrl("harbormaster.createartifact")
def body = ""
body += "&buildTargetPHID=${phid}"
body += "&artifactKey=${artifactKey}"
body += '&artifactType=uri'
body += "&artifactData[uri]=${encodedDisplayUrl}"
body += "&artifactData[name]=${artifactName}"
body += '&artifactData[ui.external]=true'
jenkinsCtx.httpRequest consoleLogResponseBody: true,
contentType: 'APPLICATION_FORM',
httpMode: 'POST',
requestBody: body,
responseHandle: 'NONE',
url: url,
validResponseCodes: '200'
}
}
/**
* We expect the following parameters to be defined (for code review builds):
* PHID: Which should be the buildTargetPHID from Harbormaster.
* INITIATOR_PHID: Which is the PHID of the initiator (ie. Differential)
* API_TOKEN: The api token to use to communicate with Phabricator
* REVISION: The revision ID of the Differential.
*/
// NOTE: We use these without a def/type because that way Groovy will treat these as
// global variables.
phabConnector = PhabConnector.newInstance(this, 'https://phab.corp.pixielabs.ai' /*url*/,
'PCD' /*repository*/, params.API_TOKEN, params.PHID)
/**
* @brief Add build info to harbormaster and badge to Jenkins.
*/
def addBuildInfo = {
phabConnector.addArtifactLink(env.RUN_DISPLAY_URL, 'jenkins.uri', 'Jenkins')
def text = ""
def link = ""
// Either a revision of a commit to master.
if (params.REVISION) {
def revisionId = "D${REVISION}"
text = revisionId
link = "${phabConnector.URL}/${revisionId}"
} else {
text = params.PHAB_COMMIT.substring(0, 7)
link = "${phabConnector.URL}/r${phabConnector.repository}${env.PHAB_COMMIT}"
}
addShortText(text: text,
background: "transparent",
border: 0,
borderColor: "transparent",
color: "#1FBAD6",
link: link)
}
def isPhabricatorTriggeredBuild() {
return params.PHID != null && params.PHID != ""
}
def codeReviewPreBuild = {
phabConnector.sendBuildStatus('work')
addBuildInfo()
}
def codeReviewPostBuild = {
if (currentBuild.result == "SUCCESS") {
phabConnector.sendBuildStatus('pass')
} else {
phabConnector.sendBuildStatus('fail')
}
if (DEPLOY_URL != "") {
// Since it's only hosted internally add basic auth info.
// This is obviously not secure, but we plan to make this public soon anyways.
def withAuthHeader = DEPLOY_URL.replace('https://', "https://pixie:${params.SITE_PASSWD}@")
phabConnector.addArtifactLink(withAuthHeader,
'customer-docs.uri', 'Customer Docs')
}
if (DEPLOY_LOGS != "") {
phabConnector.addArtifactLink(DEPLOY_LOGS,
'customer-docs-logs.uri', 'Netlify Logs')
}
}
def postBuildActions = {
if (isPhabricatorTriggeredBuild()) {
codeReviewPostBuild()
}
// Master runs are triggered by Phabricator, but we still want
// notifications on failure.
if (!isPhabricatorTriggeredBuild()) {
sendSlackNotification()
}
}
if (isPhabricatorTriggeredBuild()) {
codeReviewPreBuild()
}
node {
currentBuild.result = 'SUCCESS'
deleteDir()
try {
stage('Build & Deploy') {
checkout scm
docker.image("nikolaik/python-nodejs:python3.8-nodejs10").inside() {
sh """
yarn install
./node_modules/.bin/netlify build
./node_modules/.bin/netlify deploy --site=${params.NETLIFY_SITE_ID} \
--auth=${params.NETLIFY_TOKEN} --json > site_deploy.json
"""
def deployInfo = readJSON file: 'site_deploy.json'
DEPLOY_LOGS = deployInfo['logs']
DEPLOY_URL = deployInfo['deploy_url']
}
}
} catch(err) {
currentBuild.result = 'FAILURE'
echo "Exception thrown:\n ${err}"
echo "Stacktrace:"
err.printStackTrace()
}
postBuildActions()
}