Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade and features #24

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- action-docs-description -->
## Description

Run an AWS ECS Fargate task and execute a custom command. See the log output of the command that is executed.
Run an AWS ECS Fargate task and execute a custom commands. See the log output of the commands.
<!-- action-docs-description -->

### Details
Expand Down Expand Up @@ -42,6 +42,10 @@ This action is great for executing migrations or other pre/post deployment steps
override-container-environment: |
AWS_REGION=us-east-1
FOO=baz

task-wait-until-stopped: true
task-start-max-wait-time: 120
task-stopped-max-wait-time: 300
```

#### Minimal example
Expand Down Expand Up @@ -97,7 +101,9 @@ Will pass the following command to the container on the AWS ECS Fargate task:
| override-container-command | The command to run on the container if `override-container` is passed. | `false` | |
| override-container-environment | Add or override existing environment variables if `override-container` is passed. Provide one per line in key=value format. | `false` | |
| tail-logs | If set to true, will try to extract the logConfiguration for the first container in the task definition. If `override-container` is passed, it will extract the logConfiguration from that container. Tailing logs is only possible if the provided container uses the `awslogs` logDriver. | `false` | true |
| task-stopped-wait-for-max-attempts | How many times to check if the task is stopped before failing the action. The delay between each check is 6 seconds. | `false` | 100 |
| task-wait-until-stopped | Whether to wait for the task to stop before finishing the action. If set to false, the action will finish immediately after the task reaches the `RUNNING` state (fire and forget). | `false` | true |
| task-start-max-wait-time | How long to wait for the task to start (i.e. reach the `RUNNING` state) in seconds. If the task does not start within this time, the pipeline will fail. | `false` | 120 |
| task-stopped-max-wait-time | How long to wait for the task to stop (i.e. reach the `STOPPED` state) in seconds. The task will not be canceled after this time, the pipeline will just be marked as failed. | `false` | 300 |
<!-- action-docs-inputs -->

<!-- action-docs-outputs -->
Expand All @@ -107,7 +113,7 @@ Will pass the following command to the container on the AWS ECS Fargate task:
| --- | --- |
| task-arn | The full ARN for the task that was ran. |
| task-id | The ID for the task that was ran. |
| log-output | The log output of the task that was ran, if `tail-logs` was set to true. |
| log-output | The log output of the task that was ran, if `tail-logs` and `task-wait-until-stopped` are set to true. |
<!-- action-docs-outputs -->

<!-- action-docs-runs -->
Expand Down
23 changes: 19 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,34 @@ inputs:
required: false
default: 'true'

task-stopped-wait-for-max-attempts:
task-wait-until-stopped:
description: >-
How many times to check if the task is stopped before failing the action. The delay between each check is 6 seconds.
Whether to wait for the task to stop before finishing the action. If set to false, the action will finish
immediately after the task reaches the `RUNNING` state (fire and forget).
required: false
default: 100
default: 'true'

task-start-max-wait-time:
description: >-
How long to wait for the task to start (i.e. reach the `RUNNING` state) in seconds. If the task does not start
within this time, the pipeline will fail.
required: false
default: 120

task-stopped-max-wait-time:
description: >-
How long to wait for the task to stop (i.e. reach the `STOPPED` state) in seconds. The task will not be canceled
after this time, the pipeline will just be marked as failed.
required: false
default: 300

outputs:
task-arn:
description: 'The full ARN for the task that was ran.'
task-id:
description: 'The ID for the task that was ran.'
log-output:
description: 'The log output of the task that was ran, if `tail-logs` was set to true.'
description: 'The log output of the task that was ran, if `tail-logs` and `task-wait-until-stopped` are set to true.'

runs:
using: 'node20'
Expand Down
48 changes: 38 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43562,6 +43562,14 @@ exports["default"] = _default;
}).call(this);


/***/ }),

/***/ 78072:
/***/ ((module) => {

module.exports = eval("require")("@aws-sdk/client-ecs");


/***/ }),

/***/ 39491:
Expand Down Expand Up @@ -50295,13 +50303,16 @@ var __webpack_exports__ = {};
(() => {
const core = __nccwpck_require__(42186);
const aws = __nccwpck_require__(71786);

const {ECS, waitUntilTasksRunning, waitUntilTasksStopped} = __nccwpck_require__(78072);

const smoketail = __nccwpck_require__(45601)

const main = async () => {
try {
// Setup AWS clients
const ecs = new aws.ECS({
customUserAgent: 'github-action-aws-ecs-run-task'
const ecs = new ECS({
customUserAgent: 'github-action-aws-ecs-run-task',
});

// Inputs: Required
Expand All @@ -50316,7 +50327,11 @@ const main = async () => {
const overrideContainer = core.getInput('override-container', {required: false});
const overrideContainerCommand = core.getMultilineInput('override-container-command', {required: false});
const overrideContainerEnvironment = core.getMultilineInput('override-container-environment', {required: false});
const taskStoppedWaitForMaxAttempts = parseInt(core.getInput('task-stopped-wait-for-max-attempts', {required: false}));

// Inputs: Waiters
const taskWaitUntilStopped = core.getBooleanInput('task-wait-until-stopped', {required: false});
const taskStartMaxWaitTime = parseInt(core.getInput('task-start-max-wait-time', {required: false}));
const taskStoppedMaxWaitTime = parseInt(core.getInput('task-stopped-max-wait-time', {required: false}));

// Build Task parameters
const taskRequestParams = {
Expand Down Expand Up @@ -50384,7 +50399,7 @@ const main = async () => {
// Start task
core.debug(JSON.stringify(taskRequestParams))
core.debug(`Starting task.`)
let task = await ecs.runTask(taskRequestParams).promise();
let task = await ecs.runTask(taskRequestParams);

// Get taskArn and taskId
const taskArn = task.tasks[0].taskArn;
Expand All @@ -50395,15 +50410,26 @@ const main = async () => {

// Wait for task to be in running state
core.debug(`Waiting for task to be in running state.`)
await ecs.waitFor('tasksRunning', {cluster, tasks: [taskArn]}).promise();
await waitUntilTasksRunning({
client: ecs,
maxWaitTime: taskStartMaxWaitTime,
}, {cluster, tasks: [taskArn]});

// If taskWaitUntilStopped is false, we can bail out here because we can not tail logs or have any
// information on the exitCodes or status of the task
if (!taskWaitUntilStopped) {
core.info(`Task is running. Exiting without waiting for task to stop.`);
return;
}

// Get logging configuration
let logFilterStream = null;
let logOutput = '';

// Only create logFilterStream if tailLogs is enabled, and we wait for the task to stop in the pipeline
if (tailLogs) {
core.debug(`Logging enabled. Getting logConfiguration from TaskDefinition.`)
let taskDef = await ecs.describeTaskDefinition({taskDefinition: taskDefinition}).promise();
let taskDef = await ecs.describeTaskDefinition({taskDefinition: taskDefinition});
taskDef = taskDef.taskDefinition

// Iterate all containers in TaskDef and search for given container with awslogs driver
Expand Down Expand Up @@ -50451,11 +50477,13 @@ const main = async () => {

// Wait for Task to finish
core.debug(`Waiting for task to finish.`);
await ecs.waitFor('tasksStopped', {
await waitUntilTasksStopped({
client: ecs,
maxWaitTime: taskStoppedMaxWaitTime,
}, {
cluster,
tasks: [taskArn],
$waiter: {delay: 6, maxAttempts: taskStoppedWaitForMaxAttempts}}
).promise();
});

// Close LogStream and store output
if (logFilterStream !== null) {
Expand All @@ -50468,7 +50496,7 @@ const main = async () => {

// Describe Task to get Exit Code and Exceptions
core.debug(`Process exit code and exception.`);
task = await ecs.describeTasks({cluster, tasks: [taskArn]}).promise();
task = await ecs.describeTasks({cluster, tasks: [taskArn]});

// Get exitCode
if (task.tasks[0].containers[0].exitCode !== 0) {
Expand Down
40 changes: 30 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const core = require('@actions/core');
const aws = require('aws-sdk');

const {ECS, waitUntilTasksRunning, waitUntilTasksStopped} = require('@aws-sdk/client-ecs');

const smoketail = require('smoketail')

const main = async () => {
try {
// Setup AWS clients
const ecs = new aws.ECS({
customUserAgent: 'github-action-aws-ecs-run-task'
const ecs = new ECS({
customUserAgent: 'github-action-aws-ecs-run-task',
});

// Inputs: Required
Expand All @@ -21,7 +24,11 @@ const main = async () => {
const overrideContainer = core.getInput('override-container', {required: false});
const overrideContainerCommand = core.getMultilineInput('override-container-command', {required: false});
const overrideContainerEnvironment = core.getMultilineInput('override-container-environment', {required: false});
const taskStoppedWaitForMaxAttempts = parseInt(core.getInput('task-stopped-wait-for-max-attempts', {required: false}));

// Inputs: Waiters
const taskWaitUntilStopped = core.getBooleanInput('task-wait-until-stopped', {required: false});
const taskStartMaxWaitTime = parseInt(core.getInput('task-start-max-wait-time', {required: false}));
const taskStoppedMaxWaitTime = parseInt(core.getInput('task-stopped-max-wait-time', {required: false}));

// Build Task parameters
const taskRequestParams = {
Expand Down Expand Up @@ -89,7 +96,7 @@ const main = async () => {
// Start task
core.debug(JSON.stringify(taskRequestParams))
core.debug(`Starting task.`)
let task = await ecs.runTask(taskRequestParams).promise();
let task = await ecs.runTask(taskRequestParams);

// Get taskArn and taskId
const taskArn = task.tasks[0].taskArn;
Expand All @@ -100,15 +107,26 @@ const main = async () => {

// Wait for task to be in running state
core.debug(`Waiting for task to be in running state.`)
await ecs.waitFor('tasksRunning', {cluster, tasks: [taskArn]}).promise();
await waitUntilTasksRunning({
client: ecs,
maxWaitTime: taskStartMaxWaitTime,
}, {cluster, tasks: [taskArn]});

// If taskWaitUntilStopped is false, we can bail out here because we can not tail logs or have any
// information on the exitCodes or status of the task
if (!taskWaitUntilStopped) {
core.info(`Task is running. Exiting without waiting for task to stop.`);
return;
}

// Get logging configuration
let logFilterStream = null;
let logOutput = '';

// Only create logFilterStream if tailLogs is enabled, and we wait for the task to stop in the pipeline
if (tailLogs) {
core.debug(`Logging enabled. Getting logConfiguration from TaskDefinition.`)
let taskDef = await ecs.describeTaskDefinition({taskDefinition: taskDefinition}).promise();
let taskDef = await ecs.describeTaskDefinition({taskDefinition: taskDefinition});
taskDef = taskDef.taskDefinition

// Iterate all containers in TaskDef and search for given container with awslogs driver
Expand Down Expand Up @@ -156,11 +174,13 @@ const main = async () => {

// Wait for Task to finish
core.debug(`Waiting for task to finish.`);
await ecs.waitFor('tasksStopped', {
await waitUntilTasksStopped({
client: ecs,
maxWaitTime: taskStoppedMaxWaitTime,
}, {
cluster,
tasks: [taskArn],
$waiter: {delay: 6, maxAttempts: taskStoppedWaitForMaxAttempts}}
).promise();
});

// Close LogStream and store output
if (logFilterStream !== null) {
Expand All @@ -173,7 +193,7 @@ const main = async () => {

// Describe Task to get Exit Code and Exceptions
core.debug(`Process exit code and exception.`);
task = await ecs.describeTasks({cluster, tasks: [taskArn]}).promise();
task = await ecs.describeTasks({cluster, tasks: [taskArn]});

// Get exitCode
if (task.tasks[0].containers[0].exitCode !== 0) {
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading