Skip to content

Commit

Permalink
Update run-ecs-task not to fail if deployments are not returned in de…
Browse files Browse the repository at this point in the history
…scribe services response

Co-authored-by: Volodymyr Pihol <volodymyr.pihol@railsware.com>
  • Loading branch information
IgorDobryn and VladimirTaytor committed Nov 2, 2023
1 parent 083e2ba commit a4ef89c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ A collection of GitHub actions used at Railsware
- `yarn run_action --action name --param-1 value1 --param-2 value2 --param-3 value`
- `name` action name to run
- `param-x` - param like they specified in action.yml

- i.e when using credential profiles it could be like following:
```bash
ACTIONS_RUNNER_DEBUG=true AWS_SDK_LOAD_CONFIG=1 AWS_PROFILE=my-work-profile AWS_REGION=us-east-1 yarn run_action \
--action run-ecs-task \
--cluster my-cluster \
--service my-service \
--container my-container \
--command 'echo "Hello World"' \
--task-definition 'my_task_definition_arn' \
--wait-for-completion true \
--show-raw-output false
```
[Railsware](https://railsware.com)
2 changes: 1 addition & 1 deletion run-ecs-task/dist/index.js

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions run-ecs-task/runEcsTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const readTaskLogs = require("./readTaskLogs")
const waitTaskToComplete = require("./waitTaskToComplete")

async function runEcsTask({ ecs, cluster, serviceName, definedContainerName, command, givenTaskDefinition, waitForCompletion, showRawOutput }) {
core.debug("Describing ecs services");
const servicesResponse = await ecs
.describeServices({ cluster, services: [serviceName] })
.promise();
Expand All @@ -13,6 +14,7 @@ async function runEcsTask({ ecs, cluster, serviceName, definedContainerName, com

const service = servicesResponse.services[0];

core.debug("Describing task definition");
const {taskDefinition} = await ecs
.describeTaskDefinition({
taskDefinition: givenTaskDefinition || service.taskDefinition,
Expand All @@ -32,11 +34,12 @@ async function runEcsTask({ ecs, cluster, serviceName, definedContainerName, com
return taskDefinition.containerDefinitions[0].name;
}
})()

const networkConfiguration = service.deployments[0] ?
core.debug(`Using container name ${containerName}`);
const networkConfiguration = service.deployments && service.deployments[0] ?
service.deployments[0].networkConfiguration :
service.taskSets[0].networkConfiguration

core.debug(`Running ${command} command in ${taskDefinition.taskDefinitionArn}`);
const taskResponse = await ecs
.runTask({
cluster,
Expand All @@ -62,20 +65,19 @@ async function runEcsTask({ ecs, cluster, serviceName, definedContainerName, com

const outputURL = `https://${taskRegion}.console.aws.amazon.com/ecs/home?region=${taskRegion}#/clusters/${cluster}/tasks/${taskID}/details`;

console.log(`Task started. Track it online at ${outputURL}`);

core.info(`Task started. Track it online at ${outputURL}`);
core.setOutput("url", outputURL);

if (waitForCompletion === "true") {
const task = await waitTaskToComplete(ecs, cluster, taskID)
console.log("Task completed")
core.info("Task completed")

if (showRawOutput === "true") {
const logConfig = taskDefinition.containerDefinitions[0].logConfiguration
const logs = await readTaskLogs(logConfig, containerName, taskID)

const prettyOutput = JSON.stringify(logs, null, 2)
console.log(`Task output %o`, prettyOutput);
core.info(`Task output ${prettyOutput}`);
core.setOutput("raw_output", logs);
}

Expand All @@ -84,8 +86,8 @@ async function runEcsTask({ ecs, cluster, serviceName, definedContainerName, com
throw new Error(`Task exited abnormally. Exit code: ${container.exitCode}`)
}
} else {
console.log("The task is up and running but the action isn't going to wait for execution to complete");
core.info("The task is up and running but the action isn't going to wait for execution to complete");
}
}

module.exports = runEcsTask;
module.exports = runEcsTask;

0 comments on commit a4ef89c

Please sign in to comment.