-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·47 lines (41 loc) · 1.27 KB
/
index.mjs
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
#!/usr/bin/env node
import got from 'got';
import fs from 'fs-extra';
async function main() {
const { BASH_ENV, CIRCLE_OIDC_TOKEN, CFA_PROJECT_ID, CFA_SECRET } = process.env;
if (!BASH_ENV) {
throw new Error('Missing BASH_ENV environment variable');
}
if (!CIRCLE_OIDC_TOKEN) {
throw new Error('Missing CIRCLE_OIDC_TOKEN environment variable');
}
if (!CFA_PROJECT_ID) {
throw new Error('Missing CFA_PROJECT_ID environment variable');
}
if (!CFA_SECRET) {
throw new Error('Missing CFA_SECRET environment variable');
}
const resp = await got.post(`https://continuousauth.dev/api/request/${CFA_PROJECT_ID}/circleci/credentials`, {
body: JSON.stringify({
token: CIRCLE_OIDC_TOKEN,
}),
headers: {
'Content-Type': 'application/json',
'Authorization': `bearer ${CFA_SECRET}`,
},
});
if (resp.statusCode !== 200) {
console.error(resp.body);
throw new Error('Got error requesting credentials');
}
const { GITHUB_TOKEN } = JSON.parse(resp.body);
if (!await fs.pathExists(BASH_ENV)) {
await fs.writeFile(BASH_ENV, '');
}
await fs.appendFile(BASH_ENV, `export GITHUB_TOKEN="${GITHUB_TOKEN}"\nexport GITHUB_ACTION=1\n`);
}
main()
.catch((err) => {
console.error(err.message);
process.exit(1);
});