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

Enable run_task command to wait until the executions ends and return the exit code #230

Open
gonrial opened this issue Dec 9, 2024 · 2 comments · May be fixed by #231
Open

Enable run_task command to wait until the executions ends and return the exit code #230

gonrial opened this issue Dec 9, 2024 · 2 comments · May be fixed by #231

Comments

@gonrial
Copy link

gonrial commented Dec 9, 2024

Describe Request:

Add the capability to wait until the end of an ECS task execution. Additionally, add the option to use the exit code as a way to track success or failure of the step.

The expected behaviors should be similar to the verify_revision_is_deployed parameter in update_service command. The difference with a service is that we know this task will end, and there is a different way to request the task status.

Default behavior (current one): no wait. Similar to fire-and-forget.

Examples:

description: Start the run of an ECS task on Fargate.
usage:
  version: 2.1
  orbs:
    aws-ecs: circleci/[email protected]
    aws-cli: circleci/[email protected]
  jobs:
    run_task:
      docker:
        - image: cimg/python:3.10
      steps:
        - aws-cli/setup:
            profile_name: "OIDC-USER"
            role_arn: "arn:aws:iam::123456789012:role/VALID_OIDC_ECS_ROLE"
        - aws-ecs/run_task:
            cluster: cluster1
            region: us-east-1
            task_definition: myapp
            subnet_ids: '$SUBNET_ONE, $SUBNET_TWO'
            security_group_ids: $SECURITY_GROUP_IDS
            wait_for_task_stopped: true
  workflows:
    run_task:
      jobs:
        - run_task

Possible implementation using bash:

#!/bin/sh
AWS_PROFILE=default
ECS_CLUSTER=cluster_name
CONTAINER_NAME=migrate

# Run task and get its arn
# NOTE: many of the necessary cli inputs have been omitted here
TASK_ARN=$(aws ecs run-task \
  --cluster $ECS_CLUSTER \
  --query 'tasks[].taskArn' \
  --output text)

# Wait for ecs task to stop
aws ecs wait tasks-stopped \
  --cluster $ECS_CLUSTER \
  --tasks $TASK_ARN

# Get exit code
TASK_EXIT_CODE=$(aws ecs describe-tasks \
  --cluster $ECS_CLUSTER \
  --tasks $TASK_ARN \
  --query "tasks[0].containers[?name=='$CONTAINER_NAME'].exitCode" \
  --output text)

echo "The $TASK_ARN ran in ECS cluster $ECS_CLUSTER and its $CONTAINER_NAME returned exit code $TASK_EXIT_CODE"

# exit with the same code
exit $TASK_EXIT_CODE

Warning

From the AWS ecs wait tasks-stopped command documentation:

it will poll every 6 seconds until a successful state has been reached. This will exit with a return code of 255 after 100 failed checks.

The AWS CLI do not offer parameters to control the poll interval nor the number of poll attempts.

Supporting Documentation Links:

@marboledacci
Copy link
Contributor

This feature is something to be on the run_task command or in the update_service?

@gonrial
Copy link
Author

gonrial commented Dec 10, 2024

run_task command

gonrial added a commit to gonrial/aws-ecs-orb that referenced this issue Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants