-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaws.ts
169 lines (146 loc) · 4.28 KB
/
aws.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import * as semver from 'semver';
import {info, setSecret, exportVariable} from '@actions/core';
import {which} from '@actions/io';
import {getExecOutput} from '@actions/exec';
import ecrIAMPolicy from './resources/ecr-iam-policy.json';
import ecrLifecyclePolicy from './resources/ecr-lifecycle-policy.json';
const ecrRepositoryRegex =
/^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
export function isECRRepository(repository: string): boolean {
return ecrRepositoryRegex.test(repository) || isPubECRRepository(repository);
}
function isPubECRRepository(repository: string): boolean {
return repository.startsWith('public.ecr.aws');
}
async function getCLI(): Promise<string> {
return which('aws', true);
}
async function getCLIVersion(): Promise<string> {
return parseCLIVersion(await execCLI(['--version']));
}
async function parseCLIVersion(stdout: string): Promise<string> {
const matches = /aws-cli\/([0-9.]+)/.exec(stdout);
if (matches === null) {
throw new Error(`Cannot parse AWS CLI version`);
}
const version = semver.clean(matches[1]);
if (version === null) {
throw new Error('Cannot semver parse version');
}
return version;
}
async function execCLI(args: string[]): Promise<string> {
const cli = await getCLI();
const res = await getExecOutput(cli, args, {
silent: true,
ignoreReturnCode: true
});
if (res.stderr !== '' && res.exitCode) {
throw new Error(res.stderr);
} else if (res.stderr !== '') {
return res.stderr.trim();
}
return res.stdout.trim();
}
export function getRegion(registry: string): string {
if (isPubECRRepository(registry)) {
return 'us-east-1';
}
const matches = registry.match(ecrRepositoryRegex);
if (matches === null) {
return '';
}
return matches[3];
}
async function getDockerLoginPWD(
repository: string,
region: string
): Promise<string> {
const ecrCmd = isPubECRRepository(repository) ? 'ecr-public' : 'ecr';
return execCLI([ecrCmd, 'get-login-password', '--region', region]);
}
async function ensureEcrRepositoryExists(
repository: string,
region: string
): Promise<void> {
const ecrCmd = isPubECRRepository(repository) ? 'ecr-public' : 'ecr';
const matches = ecrRepositoryRegex.exec(repository);
if (matches === null) {
throw new Error(
`${repository} seems to be malformed. Please correct it and try again...`
);
}
const res = await getExecOutput(
await getCLI(),
[
ecrCmd,
'describe-repositories',
'--region',
region,
'--repository-names',
matches[6]
],
{silent: true, ignoreReturnCode: true}
);
if (res.exitCode === 254) {
info(`⚒️ ${matches[6]} does not exist, creating...`);
await execCLI([
ecrCmd,
'create-repository',
'--region',
region,
'--repository-name',
matches[6]
]);
await execCLI([
ecrCmd,
'set-repository-policy',
'--region',
region,
'--repository-name',
matches[6],
'--policy-text',
JSON.stringify(ecrIAMPolicy)
]);
await execCLI([
ecrCmd,
'put-lifecycle-policy',
'--region',
region,
'--repository-name',
matches[6],
'--lifecycle-policy-text',
JSON.stringify(ecrLifecyclePolicy)
]);
}
}
export async function getECRPassword(repository: string): Promise<string> {
const cliPath = await getCLI();
const cliVersion = await getCLIVersion();
const region = getRegion(repository);
if (isPubECRRepository(repository)) {
info(`💡 AWS Public ECR detected with ${region} region`);
} else {
info(`💡 AWS ECR detected with ${region} region`);
info(
`✔️ Checking if repository exists through AWS CLI ${cliVersion} (${cliPath})...`
);
await ensureEcrRepositoryExists(repository, region);
}
info(
`⬇️ Retrieving docker login password through AWS CLI ${cliVersion} (${cliPath})...`
);
return getDockerLoginPWD(repository, region);
}
export function exportCredentials(
accessKeyId: string,
secretAccessKey: string
): void {
if (accessKeyId && secretAccessKey) {
info('Use AWS credentials.');
setSecret(accessKeyId);
exportVariable('AWS_ACCESS_KEY_ID', accessKeyId);
setSecret(secretAccessKey);
exportVariable('AWS_SECRET_ACCESS_KEY', secretAccessKey);
}
}