-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (180 loc) · 6.97 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const core = require('@actions/core')
const github = require('@actions/github')
const fs = require('fs')
const dotenv = require('dotenv')
const Portainer = require('./portainer')
;(async () => {
try {
// console.log('github.context:', github.context)
const { owner, repo } = github.context.repo
const token = core.getInput('token', { required: true })
// console.log('token:', token)
const url = core.getInput('url', { required: true })
console.log('url:', url)
const name = core.getInput('name', { required: true })
console.log('name:', name)
const composeFile = core.getInput('file', { required: true })
console.log('composeFile:', composeFile)
let endpointID = core.getInput('endpoint')
console.log('endpointID:', endpointID)
const repositoryReferenceName =
core.getInput('ref') || github.context.ref
console.log('repositoryReferenceName:', repositoryReferenceName)
const repositoryURL =
core.getInput('repo') || `https://github.com/${owner}/${repo}`
console.log('repositoryURL:', repositoryURL)
const tlsskipVerify = core.getBooleanInput('tlsskip')
console.log('tlsskipVerify:', tlsskipVerify)
const prune = core.getBooleanInput('prune')
console.log('prune:', prune)
const pullImage = core.getBooleanInput('pull')
console.log('pullImage:', pullImage)
const type = core.getInput('type')
console.log('type:', type)
if (!['repo', 'file'].includes(type)) {
core.setFailed(`Unknown type: ${type}. Must be repo or file.`)
}
const standalone = core.getBooleanInput('standalone')
console.log('standalone:', standalone)
const env_json = core.getInput('env_json')
// console.log('env_json:', env_json)
const env_file = core.getInput('env_file')
// console.log('env_file:', env_file)
const env = getEnv(env_json, env_file)
// console.log('env:', env)
let repositoryUsername = core.getInput('username')
// console.log('repositoryUsername:', repositoryUsername)
let repositoryPassword = core.getInput('password')
// console.log('repositoryPassword:', repositoryPassword)
const repositoryAuthentication = !!(
repositoryUsername || repositoryPassword
)
console.log('repositoryAuthentication:', repositoryAuthentication)
const portainer = new Portainer(url, token)
if (!endpointID) {
const endpoints = await portainer.getEndpoints()
// console.log('endpoints:', endpoints)
endpointID = endpoints[0]?.Id
console.log('endpointID:', endpointID)
if (!endpointID) {
return core.setFailed('No Endpoints Found!')
}
}
let swarmID = null
if (!standalone) {
const swarm = await portainer.getSwarm(endpointID)
// console.log('swarm:', swarm)
swarmID = swarm.ID
console.log('swarmID:', swarmID)
}
const stacks = await portainer.getStacks()
// console.log('stacks:', stacks)
let stack = stacks.find((item) => item.Name === name)
// console.log('stack:', stack)
let stackID = stack?.Id
console.log('stackID:', stackID)
if (type === 'repo') {
core.info('Performing Repository Deployment.')
if (stackID) {
core.info(`Stack Found - Updating Stack ID: ${stack.Id}`)
const body = {
env,
prune,
pullImage,
repositoryReferenceName,
repositoryAuthentication,
repositoryPassword,
repositoryUsername,
}
// console.log('body:', body)
stack = await portainer.updateStackRepo(
stackID,
endpointID,
body
)
// console.log('stack:', stack)
core.info(`Updated Stack ${stack.Id}: ${stack.Name}`)
} else {
core.info('Stack NOT Found - Deploying NEW Stack')
const body = {
name,
swarmID,
repositoryURL,
composeFile,
env,
tlsskipVerify,
repositoryReferenceName,
repositoryAuthentication,
repositoryPassword,
repositoryUsername,
}
// console.log('body:', body)
stack = await portainer.createStackRepo(endpointID, body)
// console.log('stack:', stack)
core.info(`Deployed Stack: ${stack.Id}: ${stack.Name}`)
}
} else if (type === 'file') {
core.info('Performing Stack File Deployment.')
const stackFileContent = fs.readFileSync(composeFile, 'utf-8')
if (stackID) {
core.info(`Stack Found - Updating Stack ID: ${stackID}`)
const body = {
env,
prune,
pullImage,
stackFileContent,
}
// console.log('body:', body)
stack = await portainer.updateStackString(
stackID,
endpointID,
body
)
// console.log('stack:', stack)
core.info(`Updated Stack ${stack.Id}: ${stack.Name}`)
} else {
core.info('Stack NOT Found - Deploying NEW Stack')
const body = {
name,
swarmID,
stackFileContent,
env,
}
// console.log('body:', body)
stack = await portainer.createStackString(endpointID, body)
// console.log('stack:', stack)
core.info(`Deployed Stack: ${stack.Id}: ${stack.Name}`)
}
}
core.setOutput('stackID', stack.Id)
core.setOutput('swarmID', swarmID)
core.setOutput('endpointID', endpointID)
core.info(`\u001b[32;1mFinished Success`)
} catch (e) {
core.debug(e)
console.log('response:', e.response?.data)
core.setFailed(e.message)
}
})()
/**
* @function getEnv
* @param {String} env_json
* @param {String} env_file
* @return {Object[]}
*/
function getEnv(env_json, env_file) {
const env = []
if (env_json) {
let data = JSON.parse(env_json)
for (const [name, value] of Object.entries(data)) {
env.push({ name, value })
}
}
if (env_file) {
let data = dotenv.config({ path: env_file })
for (const [name, value] of Object.entries(data.parsed)) {
env.push({ name, value })
}
}
return env
}