forked from noelzubin/aws-ecs-run-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (79 loc) · 2.52 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
const core = require("@actions/core");
const AWS = require("aws-sdk");
const ecs = new AWS.ECS();
const main = async () => {
const cluster = core.getInput("cluster", { required: true });
const taskDefinition = core.getInput("task-definition", { required: true });
const subnets = core.getMultilineInput("subnets", { required: true });
const securityGroups = core.getMultilineInput("security-groups", {
required: true,
});
const assignPublicIp =
core.getInput("assign-public-ip", { required: false }) || "ENABLED";
const overrideContainer = core.getInput("override-container", {
required: false,
});
const overrideContainerCommand = core.getMultilineInput(
"override-container-command",
{
required: false,
}
);
const taskParams = {
taskDefinition,
cluster,
count: 1,
launchType: "FARGATE",
networkConfiguration: {
awsvpcConfiguration: {
subnets,
assignPublicIp,
securityGroups,
},
},
};
try {
if (overrideContainerCommand.length > 0 && !overrideContainer) {
throw new Error(
"override-container is required when override-container-command is set"
);
}
if (overrideContainer) {
if (overrideContainerCommand) {
taskParams.overrides = {
containerOverrides: [
{
name: overrideContainer,
command: overrideContainerCommand,
},
],
};
} else {
throw new Error(
"override-container-command is required when override-container is set"
);
}
}
core.debug("Running task...");
let task = await ecs.runTask(taskParams).promise();
const taskArn = task.tasks[0].taskArn;
core.setOutput("task-arn", taskArn);
core.debug("Waiting for task to finish...");
await ecs.waitFor("tasksStopped", { cluster, tasks: [taskArn] }).promise();
core.debug("Checking status of task");
task = await ecs.describeTasks({ cluster, tasks: [taskArn] }).promise();
const exitCode = task.tasks[0].containers[0].exitCode;
if (exitCode === 0) {
core.setOutput("status", "success");
} else {
core.setFailed(task.tasks[0].stoppedReason);
const taskHash = taskArn.split("/").pop();
core.info(
`task failed, you can check the error on Amazon ECS console: https://console.aws.amazon.com/ecs/home?region=${AWS.config.region}#/clusters/${cluster}/tasks/${taskHash}/details`
);
}
} catch (error) {
core.setFailed(error.message);
}
};
main();