-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (107 loc) · 4.37 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
'use strict';
const util = require('node:util');
const { join } = require('node:path');
const fs = require('node:fs/promises');
const core = require('@actions/core');
const childProcess = require('node:child_process');
const exec = util.promisify(childProcess.exec);
const AWS_DEFAULT_PROFILE = 'default';
const DEFAULT_FILE_MODE = 0o600;
//////////
const INPUT_AWS_PROFILE = core.getInput('aws-profile');
const INPUT_AWS_ACCESS_KEY = core.getInput('aws-access-key-id');
const INPUT_AWS_SECRET_KEY = core.getInput('aws-secret-access-key');
const INPUT_AWS_REGION = core.getInput('aws-region');
const INPUT_ECR_PROFILE = core.getInput('ecr-profile');
const INPUT_ECR_REGION = core.getInput('ecr-region');
const INPUT_ECR_REGISTRY = core.getInput('ecr-registry');
const INPUT_NPM_TOKEN = core.getInput('npm-token');
const INPUT_SSH_KEY = core.getInput('ssh-key');
const INPUT_SSH_KEY_NAME = core.getInput('ssh-key-name');
const INPUT_SSH_KNOWN_HOSTS = core.getInput('ssh-known-hosts');
//////////
async function configureAwsCredentials () {
const directory = join(process.env.HOME, '.aws');
await fs.mkdir(directory, { recursive: true });
// ~/.aws/config
const profileName = INPUT_AWS_PROFILE === AWS_DEFAULT_PROFILE ? INPUT_AWS_PROFILE :
`profile ${ INPUT_AWS_PROFILE }`;
const configFile = join(directory, 'config');
const config = `[${ profileName }]\n` +
`region = ${ INPUT_AWS_REGION }\noutput = json\n`;
await fs.writeFile(configFile, config);
await fs.chmod(configFile, DEFAULT_FILE_MODE);
// ~/.aws/credentials
const credentialsFile = join(directory, 'credentials');
const credentials = `[${ INPUT_AWS_PROFILE }]\n` +
`aws_access_key_id = ${ INPUT_AWS_ACCESS_KEY }\n` +
`aws_secret_access_key = ${ INPUT_AWS_SECRET_KEY }\n`;
await fs.writeFile(credentialsFile, credentials);
await fs.chmod(credentialsFile, DEFAULT_FILE_MODE);
core.exportVariable('AWS_ACCESS_KEY_ID', INPUT_AWS_ACCESS_KEY);
core.exportVariable('AWS_SECRET_ACCESS_KEY', INPUT_AWS_SECRET_KEY);
core.exportVariable('AWS_DEFAULT_REGION', INPUT_AWS_REGION);
console.log(`Configured AWS credentials for [${ INPUT_AWS_PROFILE }]`);
}
async function loginToECR () {
let profile = INPUT_ECR_PROFILE || INPUT_AWS_PROFILE;
if (profile && profile !== AWS_DEFAULT_PROFILE) {
profile = `--profile ${ profile }`;
} else {
profile = '';
}
let region = INPUT_ECR_REGION || INPUT_AWS_REGION;
if (INPUT_ECR_REGISTRY.includes('.amazonaws.com')) {
region = INPUT_ECR_REGISTRY.replace(/^.*\.dkr\.ecr\.(.*?)\.amazonaws\.com$/, '$1');
}
const registry = INPUT_ECR_REGISTRY.includes('.amazonaws.com') ? INPUT_ECR_REGISTRY :
`${ INPUT_ECR_REGISTRY }.dkr.ecr.${ region }.amazonaws.com`;
await exec(`aws ecr get-login-password ${ profile } --region ${ region } | ` +
`docker login --username AWS --password-stdin ${ registry }`, { shell: '/bin/bash' });
console.log(`Successfully logged into ECR registry ${ registry } [${ profile }]`);
}
async function configureNpmToken () {
const file = join(process.env.HOME, '.npmrc');
await fs.writeFile(file, `//registry.npmjs.org/:_authToken=${ INPUT_NPM_TOKEN }\n`);
await fs.chmod(file, DEFAULT_FILE_MODE);
console.log(`Configured npm token in ${ file }`);
}
async function configureSshKey () {
const directory = join(process.env.HOME, '.ssh');
await fs.mkdir(directory, { recursive: true });
const file = join(directory, INPUT_SSH_KEY_NAME);
await fs.writeFile(file, INPUT_SSH_KEY);
await fs.chmod(file, DEFAULT_FILE_MODE);
console.log(`Configured SSH key "${ INPUT_SSH_KEY_NAME }"`);
}
async function configureSshKnownHosts () {
const directory = join(process.env.HOME, '.ssh');
await fs.mkdir(directory, { recursive: true });
const file = join(directory, 'known_hosts');
await fs.writeFile(file, INPUT_SSH_KNOWN_HOSTS);
await fs.chmod(file, DEFAULT_FILE_MODE);
console.log(`Configured SSH key "${ INPUT_SSH_KEY_NAME }"`);
}
//////////
async function main () {
try {
if (INPUT_AWS_ACCESS_KEY && INPUT_AWS_SECRET_KEY) {
await configureAwsCredentials();
}
if (INPUT_ECR_REGISTRY) {
await loginToECR();
}
if (INPUT_NPM_TOKEN) {
await configureNpmToken();
}
if (INPUT_SSH_KEY) {
await configureSshKey();
}
if (INPUT_SSH_KNOWN_HOSTS) {
await configureSshKnownHosts();
}
} catch (error) {
core.setFailed(error.message);
}
}
main();