forked from wirgen/portainer-stack-redeploy-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (57 loc) · 2 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
const core = require("@actions/core")
console.log('Preparing stack deployment')
let portainerUrl = core.getInput("portainerUrl")
const accessToken = core.getInput("accessToken")
const stackId = parseInt(core.getInput("stackId"))
const endpointId = parseInt(core.getInput("endpointId"))
const repositoryAuthentication = core.getInput("repositoryAuthentication")
const environmentVariables = core.getInput("environment")
if (isNaN(stackId)) {
core.setFailed("Stack ID must be integer")
process.exit(1)
}
let client
if (portainerUrl.includes("http:")) {
client = require("http")
} else {
client = require("https")
if (!portainerUrl.includes("https:")) {
portainerUrl = `https://${portainerUrl}`
}
}
if (portainerUrl.substring(portainerUrl.length - 1) === "/") {
portainerUrl = portainerUrl.substring(0, portainerUrl.length - 1)
}
core.setSecret(portainerUrl)
core.setSecret(accessToken)
const postDataObject = {
pullImage: true,
}
if (repositoryAuthentication === true || repositoryAuthentication === 'true') {
postDataObject.repositoryAuthentication = true
}
if (environmentVariables !== undefined && environmentVariables !== "") {
postDataObject.env = JSON.parse(environmentVariables)
}
const postData = JSON.stringify(postDataObject)
console.log(`Deploying stack ${stackId} on portainer host ${portainerUrl} ${postDataObject.env ? 'With environment variables ' + JSON.stringify(postDataObject.env) : 'clearing all environment variables.'}`)
const req = client.request(`${portainerUrl}/api/stacks/${stackId}/git/redeploy` + (isNaN(endpointId) ? "" : `?endpointId=${endpointId}`), {
method: "PUT",
headers: {
"X-API-Key": accessToken,
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(postData)
}
}, (res) => {
if (res.statusCode !== 200) {
core.setFailed(res.statusMessage)
process.exit(2)
}
console.log('Stack deployed successfully')
})
.on("error", (error) => {
core.setFailed(error.message)
process.exit(3)
})
req.write(postData)
req.end()