Skip to content

Commit 8fdb3db

Browse files
maclover7phillipj
authored andcommitted
jenkins: update to use pipeline (#178)
Refs: https://github.com/nodejs-private/secrets/pull/20 Refs: #175 Will now post comments upon pipeline builds being run
1 parent 2db3264 commit 8fdb3db

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

scripts/trigger-jenkins-build.js

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ function ifBotWasMentionedInCiComment (commentBody, cb) {
2222

2323
// URL to the Jenkins job should be triggered for a given repository
2424
function buildUrlForRepo (repo) {
25-
// e.g. JENKINS_JOB_URL_CITGM = https://ci.nodejs.org/job/citgm-continuous-integration-pipeline
25+
// e.g. JENKINS_JOB_URL_CITGM = https://ci.nodejs.org/blue/rest/organizations/jenkins/pipelines/node-test-pull-request-lite-pipeline/runs/
2626
const jobUrl = process.env[`JENKINS_JOB_URL_${repo.toUpperCase()}`] || ''
27-
return jobUrl ? `${jobUrl}/build` : ''
27+
return jobUrl
2828
}
2929

3030
// Authentication token configured per Jenkins job needed when triggering a build,
@@ -41,26 +41,12 @@ function buildParametersForRepo (options, repo) {
4141
value: `refs/pull/${options.number}/head`
4242
}]
4343
} else {
44-
return [{
45-
name: 'CERTIFY_SAFE',
46-
value: 'true'
47-
},
48-
{
49-
name: 'TARGET_GITHUB_ORG',
50-
value: 'nodejs'
51-
},
52-
{
53-
name: 'TARGET_REPO_NAME',
54-
value: 'node'
55-
},
56-
{
57-
name: 'PR_ID',
58-
value: options.number
59-
},
60-
{
61-
name: 'REBASE_ONTO',
62-
value: '<pr base branch>'
63-
}
44+
return [
45+
{ name: 'CERTIFY_SAFE', value: 'true' },
46+
{ name: 'GITHUB_ORG', value: 'nodejs' },
47+
{ name: 'REPO_NAME', value: 'node' },
48+
{ name: 'PR_ID', value: options.number },
49+
{ name: 'REBASE_ONTO', value: '' }
6450
]
6551
}
6652
}
@@ -70,7 +56,6 @@ function triggerBuild (options, cb) {
7056
const base64Credentials = new Buffer(jenkinsApiCredentials).toString('base64')
7157
const authorization = `Basic ${base64Credentials}`
7258

73-
const payload = JSON.stringify({ parameter: buildParametersForRepo(options, repo) })
7459
const uri = buildUrlForRepo(repo)
7560
const buildAuthToken = buildTokenForRepo(repo)
7661

@@ -88,15 +73,15 @@ function triggerBuild (options, cb) {
8873
uri,
8974
headers: { authorization },
9075
qs: { token: buildAuthToken },
91-
form: { json: payload }
76+
json: { parameters: buildParametersForRepo(options, repo) }
9277
}, (err, response) => {
9378
if (err) {
9479
return cb(err)
95-
} else if (response.statusCode !== 201) {
96-
return cb(new Error(`Expected 201 from Jenkins, got ${response.statusCode}`))
80+
} else if (response.statusCode !== 200) {
81+
return cb(new Error(`Expected 200 from Jenkins, got ${response.statusCode}`))
9782
}
9883

99-
cb(null, response.headers.location)
84+
cb(null, response.body._links.self.href)
10085
})
10186
}
10287

@@ -163,20 +148,22 @@ module.exports = (app) => {
163148
logger
164149
}
165150

166-
function logBuildStarted (err) {
151+
function replyToCollabWithBuildStarted (err, buildUrl) {
167152
if (err) {
168153
logger.error(err, 'Error while triggering Jenkins build')
169-
} else {
170-
logger.info('Jenkins build started')
154+
return createPrComment(options, `@${pullRequestAuthor} sadly an error occured when I tried to trigger a build :(`)
171155
}
156+
157+
createPrComment(options, `@${pullRequestAuthor} build started: https://ci.nodejs.org${buildUrl}`)
158+
logger.info({ buildUrl }, 'Jenkins build started')
172159
}
173160

174161
function triggerBuildWhenCollaborator (err) {
175162
if (err) {
176163
return logger.debug(`Ignoring comment to me by @${pullRequestAuthor} because they are not a repo collaborator`)
177164
}
178165

179-
triggerBuild(options, logBuildStarted)
166+
triggerBuild(options, replyToCollabWithBuildStarted)
180167
}
181168

182169
githubClient.repos.checkCollaborator({ owner, repo, username: pullRequestAuthor }, triggerBuildWhenCollaborator)

test/integration/trigger-jenkins-build.test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,28 @@ tap.test('Sends POST request to https://ci.nodejs.org', (t) => {
2323

2424
const originalJobUrlValue = process.env.JENKINS_JOB_URL_NODE
2525
const originalTokenValue = process.env.JENKINS_BUILD_TOKEN_NODE
26-
process.env.JENKINS_JOB_URL_NODE = 'https://ci.nodejs.org/job/node-test-pull-request'
26+
process.env.JENKINS_JOB_URL_NODE = 'https://ci.nodejs.org/blue/rest/organizations/jenkins/pipelines/node-test-pull-request-lite-pipeline/runs/'
2727
process.env.JENKINS_BUILD_TOKEN_NODE = 'myToken'
2828

2929
const webhookPayload = readFixture('pull-request-opened.json')
30+
const pipelineUrl = '/blue/organizations/jenkins/node-test-pull-request-lite-pipeline/detail/node-test-pull-request-lite-pipeline/23/pipeline/'
3031

3132
const collaboratorsScope = nock('https://api.github.com')
3233
.filteringPath(ignoreQueryParams)
3334
.get('/repos/nodejs/node/collaborators/phillipj')
3435
.reply(200, { permission: 'admin' })
3536
const ciJobScope = nock('https://ci.nodejs.org')
3637
.filteringPath(ignoreQueryParams)
37-
.post('/job/node-test-pull-request/build')
38-
.reply(201, '', {
39-
'Location': 'https://ci.nodejs.org/job/node-test-pull-request/1'
40-
})
38+
.post('/blue/rest/organizations/jenkins/pipelines/node-test-pull-request-lite-pipeline/runs/')
39+
.reply(200, { _links: { self: { href: pipelineUrl } } }, {})
40+
41+
const commentScope = nock('https://api.github.com')
42+
.filteringPath(ignoreQueryParams)
43+
.post('/repos/nodejs/node/issues/19/comments', { body: `@phillipj build started: https://ci.nodejs.org/${pipelineUrl}` })
44+
.reply(200)
4145

4246
t.plan(1)
43-
t.tearDown(() => collaboratorsScope.done() && ciJobScope.done() && clock.uninstall())
47+
t.tearDown(() => collaboratorsScope.done() && ciJobScope.done() && commentScope.done() && clock.uninstall())
4448

4549
supertest(app)
4650
.post('/hooks/github')

0 commit comments

Comments
 (0)