forked from noelzubin/aws-ecs-run-task
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
101 lines (83 loc) · 2.81 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
const core = require("@actions/core");
const { ECS, waitUntilTasksStopped } = require("@aws-sdk/client-ecs");
const ecs = new ECS();
const main = async () => {
const cluster = core.getInput("cluster", { required: true });
const service = core.getInput("service", { required: true });
const overrideContainer = core.getInput("override-container", {
required: false,
});
try {
// Get network configuration from aws directly from describe services
core.debug("Getting information from service...");
const info = await ecs.describeServices({ cluster, services: [service] });
if (!info || !info.services[0]) {
throw new Error(`Could not find service ${service} in cluster ${cluster}`);
}
if (!info.services[0].networkConfiguration) {
throw new Error(`Service ${service} in cluster ${cluster} does not have a network configuration`);
}
const taskDefinition = info.services[0].taskDefinition;
const networkConfiguration = info.services[0].networkConfiguration;
core.setOutput("task-definition", taskDefinition);
const overrideContainerCommand = core.getMultilineInput(
"override-container-command",
{
required: false,
}
);
const taskParams = {
taskDefinition,
cluster,
launchType: "FARGATE",
networkConfiguration,
};
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);
const taskArn = task.tasks[0].taskArn;
core.setOutput("task-arn", taskArn);
core.info(
`Task logs on Amazon ECS console: https://console.aws.amazon.com/ecs/v2/clusters/${cluster}/tasks/${taskArn.split("/").pop()}/logs?region=${process.env.AWS_REGION}`
);
core.debug("Waiting for task to finish...");
await waitUntilTasksStopped({
client: ecs,
maxWaitTime: 6000,
}, {
cluster,
tasks: [taskArn],
});
core.debug("Checking status of task");
task = await ecs.describeTasks({ cluster, tasks: [taskArn] });
const exitCode = task.tasks[0].containers[0].exitCode;
if (exitCode === 0) {
core.setOutput("status", "success");
} else {
core.setFailed(task.tasks[0].stoppedReason);
}
} catch (error) {
core.setFailed(error);
}
};
main();