portainer-stack-git-redeplo.../index.js

66 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-12-08 22:00:11 +01:00
const core = require("@actions/core")
let portainerUrl = core.getInput("portainerUrl")
const accessToken = core.getInput("accessToken")
const stackId = parseInt(core.getInput("stackId"))
const endpointId = parseInt(core.getInput("endpointId"))
2023-08-17 19:17:58 +02:00
const environmentVariables = core.getInput("environment")
2022-12-08 22:00:11 +01:00
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)
2022-12-08 22:24:09 +01:00
core.setSecret(accessToken)
2022-12-08 22:00:11 +01:00
2023-08-17 19:17:58 +02:00
const postDataObject = {
pullImage: true,
}
console.dir(environmentVariables)
if (environmentVariables !== undefined && environmentVariables !== "") {
postDataObject.env = JSON.parse(environmentVariables)
}
const postData = JSON.stringify(postDataObject)
console.dir(postData)
const req = client.request(`${portainerUrl}/api/stacks/${stackId}/git/redeploy` + (isNaN(endpointId) ? "" : `?endpointId=${endpointId}`), {
method: "PUT",
2022-12-08 22:00:11 +01:00
headers: {
2023-08-17 19:17:58 +02:00
"X-API-Key": accessToken,
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(postData)
2022-12-08 22:00:11 +01:00
}
}, (res) => {
if (res.statusCode !== 200) {
core.setFailed(res.statusMessage)
process.exit(2)
}
})
.on("error", (error) => {
core.setFailed(error.message)
process.exit(3)
})
2023-08-17 19:17:58 +02:00
req.write(postData)
req.end()