generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.mjs
93 lines (78 loc) · 2.05 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
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
import core from '@actions/core'
import AWS from './src/aws.mjs';
import { determineIp } from './src/ip.mjs'
import * as state from './src/state.mjs';
import * as io from './src/io.mjs';
/**
* Runs the main action
*
* It attempts to determine the ip if none provided, and then proceeds to create the relevant inbound rule
* in the specified security group.
*
* @returns {Promise<void>}
*/
async function main() {
try {
let ip = io.getIp();
if (ip === '' || !ip) {
core.info(`Determining the ip address...`);
ip = await determineIp();
}
io.setIp(ip);
state.saveIp(ip);
core.info(`Ip address to use: ${ip}`);
const client = new AWS({
accessKeyId: io.getAwsAccessKeyId(),
secretAccessKey: io.getAwsSecretAccessKey(),
region: io.getAwsRegion(),
});
const ruleId = await client.authorizeIngressRule(
io.getAwsSecurityGroupId(),
ip,
io.getProtocol(),
io.getPortNumber(),
);
core.info(`Inbound rule created successfully: ${ruleId}`);
// Save the rule to the output and the state
io.setRuleId(ruleId);
state.saveRuleId(ruleId);
} catch (error) {
core.setFailed(error.message);
}
}
/**
* Cleans up after the action
*
* This runs after the job ends and removes the created security group rule from AWS.
*
* @returns {Promise<void>}
*/
async function cleanup() {
const ruleId = state.getRuleId();
if (!ruleId) {
core.info('Nothing to clean because rule was not created');
return;
}
core.info('Removing created inbound rule');
const ip = state.getIp();
const client = new AWS({
accessKeyId: io.getAwsAccessKeyId(),
secretAccessKey: io.getAwsSecretAccessKey(),
region: io.getAwsRegion(),
});
await client.revokeIngressRule(
io.getAwsSecurityGroupId(),
ip,
io.getProtocol(),
io.getPortNumber(),
);
core.info('Inbound rule removed successfully');
}
// Self executing async function that runs everything
(async () => {
if (state.IsPost) {
await cleanup();
} else {
await main();
}
})();