-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline-apply.ts
144 lines (125 loc) · 4.82 KB
/
pipeline-apply.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
import type { CdkManager } from './manager';
import type { Account, Instance } from './config';
import { BaseCliCommand } from './cli-utils';
import { execPromise, executeCommand, commandSetToString } from './exec-utils';
import type { CommandSet } from './exec-utils';
import * as cmdTs from 'cmd-ts';
export const pipelineApplyCliArgs = {
account: cmdTs.option({
type: cmdTs.optional(cmdTs.string),
long: 'account',
}),
noDefaultProfiles: cmdTs.flag({
type: cmdTs.boolean,
long: 'no-default-profiles',
}),
deploymentSuffix: cmdTs.option({
type: cmdTs.optional(cmdTs.string),
long: 'deployment-suffix',
}),
apply: cmdTs.flag({
type: cmdTs.boolean,
long: 'apply',
}),
};
export interface PipelineApplyCliCommandArgs {
account: string | undefined;
apply: boolean;
noDefaultProfiles: boolean;
deploymentSuffix: string | undefined;
}
export class PipelineApplyCliCommand<A, M extends CdkManager<A>> extends BaseCliCommand<A, M> {
getPipelineDeployShellCommand(account: Account, instance: Instance<A>, extraEnv?: Record<string, string>): { command: string; env: Record<string, string> } {
const env: Record<string, string> = {
TARGET_ACCOUNT: account.name,
...(extraEnv ?? {}),
};
if (instance.suffix) {
env['TARGET_ENVIRONMENT_SUFFIX'] = instance.suffix;
}
return {
command: `npm run -- cdk deploy --require-approval never -e ${this.manager.getPipelineStackName(account, instance)}`,
env,
};
}
getLocalBranchSwitchCommand(branchName: string): string {
return `git checkout ${branchName}`;
}
getLocalBranchSwitchUndoCommand(): string {
return `git checkout -`;
}
getPipelineBranchChangeAndDeployCommand(account: Account, instance: Instance<A>, extraEnv?: Record<string, string>): Array<{ command: string; env?: Record<string, string> }> {
const { command, env } = this.getPipelineDeployShellCommand(account, instance, extraEnv);
return [
{
command: this.getLocalBranchSwitchCommand(instance.branchName),
},
{
command,
env,
},
{
command: this.getLocalBranchSwitchUndoCommand(),
},
];
}
getPipelineApplyCommands(accountName: string, suffix: string | undefined, noDefaultProfiles: boolean): Array<CommandSet> {
const account = this.manager.getAccount(accountName);
const instances = this.manager.getInstancesForAccount(account.name);
const commandSets: Array<CommandSet> = [];
for (const instance of instances) {
if (suffix && suffix !== instance.suffix) {
continue;
}
const env: Record<string, string> = {};
if (!noDefaultProfiles) {
env['AWS_PROFILE'] = this.manager.getDefaultPipelineDeploymentProfile(account, instance);
}
const commands = this.getPipelineBranchChangeAndDeployCommand(account, instance, env);
commandSets.push(commands);
}
return commandSets;
}
async handler(args: PipelineApplyCliCommandArgs): Promise<void> {
const { account, apply, noDefaultProfiles, deploymentSuffix } = args;
const selectedAccountNames = account ? [account] : this.manager.getAccountNames();
const commands = [];
if (apply) {
try {
await execPromise('git diff --quiet');
} catch (err) {
console.error('Working directory not clean, aborting. Commit code, then try again.');
throw err;
}
}
for (const selectedAccountName of selectedAccountNames) {
commands.push(...this.getPipelineApplyCommands(selectedAccountName, deploymentSuffix, noDefaultProfiles));
}
if (apply) {
console.error('Appling pipelines:');
for (const command of commands) {
await executeCommand(command);
}
} else {
console.error('Not doing anything without --apply flag. Would run the following:');
if (commands.length) {
for (const command of commands) {
console.log(commandSetToString(command));
}
} else {
console.error('(no commands to run)');
}
}
}
}
export const pipelineApplyCliRun = <A, M extends CdkManager<A>>(manager: M, argv: Array<string>): void => {
const cls = new PipelineApplyCliCommand(manager);
cmdTs.run(
cmdTs.command({
name: 'pipeline-apply',
args: pipelineApplyCliArgs,
handler: cls.handler.bind(cls),
}),
argv,
);
};