This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
78 lines (70 loc) · 2.04 KB
/
main.ts
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
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as io from '@actions/io'
import * as os from 'os'
import * as toolCache from '@actions/tool-cache'
import fs from 'fs'
const STATE_FLAG = 'cloudctlLogin'
const KUBE_CONFIG_BACKUP = `${os.tmpdir()}/kubeconfig.cloudctl.bak`
const KUBE_CONFIG_FILE = `${os.homedir()}/.kube/config`
export async function run(): Promise<void> {
if (isCleanupPhase()) {
await exec.exec('cloudctl', ['logout'])
await restoreKubeConfig()
} else {
if (core.getBooleanInput('installKubectl', {required: true})) {
await downloadTool('kubectl')
}
await downloadTool('cloudctl')
return cloudctlLogin()
}
}
async function restoreKubeConfig(): Promise<void> {
await io.rmRF(KUBE_CONFIG_FILE)
if (fs.existsSync(KUBE_CONFIG_BACKUP)) {
await io.mv(KUBE_CONFIG_BACKUP, KUBE_CONFIG_FILE)
}
}
async function downloadTool(tool: string): Promise<void> {
try {
const download = await toolCache.downloadTool(
`${core.getInput('apiEndpoint', {
required: true,
trimWhitespace: true
})}/api/cli/${tool}-linux-amd64`
)
fs.chmodSync(download, 0o555)
const cachedPath = await toolCache.cacheFile(download, tool, tool, 'latest')
core.addPath(cachedPath)
} catch (error) {
core.setFailed(error.message)
}
}
async function backupConfig(): Promise<void> {
if (fs.existsSync(KUBE_CONFIG_FILE)) {
await io.cp(KUBE_CONFIG_FILE, KUBE_CONFIG_BACKUP)
}
}
async function cloudctlLogin(): Promise<void> {
try {
await backupConfig()
await exec.exec('cloudctl', [
'login',
'-a',
core.getInput('apiEndpoint', {required: true}),
'-u',
core.getInput('username', {required: true}),
'-p',
core.getInput('password', {required: true}),
'-n',
core.getInput('namespace', {required: true})
])
core.saveState(STATE_FLAG, 'true')
} catch (error) {
core.setFailed(error.message)
}
}
function isCleanupPhase(): boolean {
return core.getState(STATE_FLAG).length > 0
}
run()