-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·101 lines (94 loc) · 3.58 KB
/
index.js
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
const core = require("@actions/core")
const axios = require("axios")
// External input
const username = process.env.JENKINS_USER
const apitoken = process.env.JENKINS_TOKEN
const parameters = `${core.getInput("parameters")}`
const waitForCompletion = `${core.getInput("waitForCompletion")}`
var jobBuildUrl = `${core.getInput("jobBuildUrl")}`
const poll = async ({ fn, validate, interval, maxAttempts }) => {
let attempts = 0
const executePoll = async (resolve, reject) => {
const result = await fn()
attempts++
if (validate(result)) {
return resolve(result)
} else if (maxAttempts && attempts === maxAttempts) {
return reject(new Error("Exceeded max attempts"))
} else {
setTimeout(executePoll, interval, resolve, reject)
}
}
return new Promise(executePoll)
}
const pollForBuildStart = async (queuedItemUrl) => {
return poll({
fn: async () => {
return axios.post(queuedItemUrl, {}, { auth: basicAuth })
},
validate: response => response.data.executable !== undefined,
/* Poll every second */
interval: 1000,
/* Poll for 10 minutes */
maxAttempts: 60 * 10
})
}
const pollForBuildCompletion = async (buildUrl) => {
return poll({
fn: async () => {
return axios.post(buildUrl, {}, { auth: basicAuth })
},
validate: response => !!response.data.result,
/* Poll every second */
interval: 1000,
/* Poll for 30 minutes */
maxAttempts: 60 * 30
})
}
if (parameters !== undefined && parameters !== "") {
const queryParam = parameters.trim().replace(/\n+/g, "&")
core.info(`Using query params: ${queryParam}`)
jobBuildUrl = `${jobBuildUrl}?${queryParam}`
}
const basicAuth = {
username: username,
password: apitoken
}
core.info(`Triggering job: ${jobBuildUrl}`)
axios.post(jobBuildUrl, {}, { auth: basicAuth })
.then((response) => {
core.info("Job triggered")
if (response.status != 201 || response.headers.location === undefined ) {
core.error(`Status: ${response.status}`)
core.error(`Location: ${response.headers.location}`)
core.setFailed("Prerequisite for triggered job failed")
return
}
const queuedItemUrl = `${response.headers.location}api/json`
core.info(`Polling startup of job via ${queuedItemUrl}`)
pollForBuildStart(queuedItemUrl)
.then((response) => {
core.info("Job successfully started")
core.info(`Build URL is ${response.data.executable.url}`)
if (waitForCompletion == "true") {
const buildUrl = `${response.data.executable.url}api/json?tree=result`
core.info(`Waiting for job completion, polling via ${buildUrl}`)
pollForBuildCompletion(buildUrl)
.then((response) => {
core.info(`Job finished with result ${response.data.result} (${response.data.url})`)
if (response.data.result != "SUCCESS") {
core.setFailed(`Unsuccessful job state: ${response.data.url}`)
}
})
.catch((error) => {
core.setFailed(error.message)
})
}
})
.catch((error) => {
core.setFailed(error.message)
})
})
.catch((error) => {
core.setFailed(error.message)
})