2022-12-08 22:00:11 +01:00
|
|
|
const core = require("@actions/core")
|
|
|
|
|
2023-08-22 17:50:06 +02:00
|
|
|
console.log('Preparing stack deployment')
|
|
|
|
|
2022-12-08 22:00:11 +01:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (environmentVariables !== undefined && environmentVariables !== "") {
|
|
|
|
postDataObject.env = JSON.parse(environmentVariables)
|
|
|
|
}
|
|
|
|
|
|
|
|
const postData = JSON.stringify(postDataObject)
|
|
|
|
|
2023-08-22 17:50:06 +02:00
|
|
|
console.log(`Deploying stack ${stackId} on portainer host ${portainerUrl} ${postDataObject.env ? 'With environment variables ' + JSON.stringify(postDataObject.env) : 'clearing all environment variables.'}`)
|
2023-08-17 19:17:58 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-08-22 17:50:06 +02:00
|
|
|
console.log('Stack deployed successfully')
|
2022-12-08 22:00:11 +01:00
|
|
|
})
|
|
|
|
.on("error", (error) => {
|
|
|
|
core.setFailed(error.message)
|
|
|
|
process.exit(3)
|
|
|
|
})
|
2023-08-17 19:17:58 +02:00
|
|
|
|
|
|
|
req.write(postData)
|
|
|
|
req.end()
|