-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.ts
145 lines (123 loc) · 4.18 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
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
import { resolve } from 'path';
import * as core from '@actions/core';
import { context } from '@actions/github';
import {
LocalProgramArgs,
LocalWorkspace,
LocalWorkspaceOptions,
} from '@pulumi/pulumi/automation';
import invariant from 'ts-invariant';
import {
Commands,
Config,
InstallationConfig,
makeConfig,
makeInstallationConfig,
} from './config';
import { environmentVariables } from './libs/envs';
import { handlePullRequestMessage } from './libs/pr';
import * as pulumiCli from './libs/pulumi-cli';
import { login } from './#';
const main = async () => {
const downloadConfig = makeInstallationConfig();
if (downloadConfig.success) {
await installOnly(downloadConfig.value);
core.info('Pulumi has been successfully installed. Exiting.');
return;
}
// If we get here, we're not in install-only mode.
// Attempt to parse the full configuration and run the action.
const config = await makeConfig();
core.debug('Configuration is loaded');
runAction(config);
};
// installOnly is the main entrypoint of the program when the user
// intends to install the Pulumi CLI without running additional commands.
const installOnly = async (config: InstallationConfig): Promise<void> => {
await pulumiCli.downloadCli(config.pulumiVersion);
};
const runAction = async (config: Config): Promise<void> => {
await pulumiCli.downloadCli(config.pulumiVersion);
await login(config.cloudUrl, environmentVariables.PULUMI_ACCESS_TOKEN);
const workDir = resolve(
environmentVariables.GITHUB_WORKSPACE,
config.workDir,
);
core.debug(`Working directory resolved at ${workDir}`);
const stackArgs: LocalProgramArgs = {
stackName: config.stackName,
workDir: workDir,
};
const stackOpts: LocalWorkspaceOptions = {};
if (config.secretsProvider != '') {
stackOpts.secretsProvider = config.secretsProvider;
}
const stack = await (config.upsert
? LocalWorkspace.createOrSelectStack(stackArgs, stackOpts)
: LocalWorkspace.selectStack(stackArgs, stackOpts));
const projectSettings = await stack.workspace.projectSettings();
const projectName = projectSettings.name;
const onOutput = (msg: string) => {
core.debug(msg);
core.info(msg);
};
if (config.configMap) {
// TODO: Remove `any` once pulumi/pulumi#12641 is fixed.
await stack.setAllConfig(config.configMap as any);
}
if (config.refresh) {
core.startGroup(`Refresh stack on ${config.stackName}`);
await stack.refresh({ onOutput });
core.endGroup();
}
core.startGroup(`pulumi ${config.command} on ${config.stackName}`);
const actions: Record<Commands, () => Promise<string>> = {
up: () => stack.up({ onOutput, ...config.options }).then((r) => r.stdout),
update: () =>
stack.up({ onOutput, ...config.options }).then((r) => r.stdout),
refresh: () =>
stack.refresh({ onOutput, ...config.options }).then((r) => r.stdout),
destroy: () =>
stack.destroy({ onOutput, ...config.options }).then((r) => r.stdout),
preview: async () => {
const { stdout, stderr } = await stack.preview(config.options);
onOutput(stdout);
onOutput(stderr);
return stdout;
},
};
core.debug(`Running action ${config.command}`);
const output = await actions[config.command]();
core.debug(`Done running action ${config.command}`);
core.setOutput('output', output);
const outputs = await stack.outputs();
for (const [outKey, outExport] of Object.entries(outputs)) {
core.setOutput(outKey, outExport.value);
if (outExport.secret) {
core.setSecret(outExport.value);
}
}
if (config.commentOnPrNumber || config.commentOnPr) {
const isPullRequest = context.payload.pull_request !== undefined;
if (isPullRequest) {
core.debug(`Commenting on pull request`);
invariant(config.githubToken, 'github-token is missing.');
handlePullRequestMessage(config, projectName, output);
}
}
if (config.remove && config.command === 'destroy') {
stack.workspace.removeStack(stack.name);
}
core.endGroup();
};
(async () => {
try {
await main();
} catch (err) {
if (err.message.stderr) {
core.setFailed(err.message.stderr);
} else {
core.setFailed(err.message);
}
}
})();