Search code examples
continuous-integrationdevopsamazon-ecscontinuous-deployment

ECS sh script - update service


I have a service on ECS that has one task with 2 containers. The 2 containers have the same image but different entry points.

I'm trying to set up a pipeline that after tests, takes the image pushed on the registry and updates the service on ECS.

I created a script for that but it only updates one of the containers. So only one container is up-to-date with the changes. How do I make it so that the changes are taken from both containers?

Definition of the task:

register_definition() {
    TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition ${ECS_TASK_DEFINITION} | jq '.taskDefinition')
    current_revision=$(echo ${TASK_DEFINITION} | jq '.taskDefinitionArn')
    echo "Current revision: ${current_revision}"
    extra_keys=".compatibilities, .status, .taskDefinition, .revision, .requiresAttributes, .taskDefinitionArn"
    echo "Deleting extra keys... ${extra_keys}"
    TASK_DEFINITION=$(echo ${TASK_DEFINITION} | jq "del(${extra_keys})")
    prev_image=$(echo ${TASK_DEFINITION} | jq ".containerDefinitions[0].image")
    echo "Replacing ${prev_image} with ${IMAGE}"
    UPDATED_TASK_DEFINITION=$(echo ${TASK_DEFINITION} | jq ".containerDefinitions[0].image = \"${IMAGE}\"")
    new_revision=$(aws ecs register-task-definition --cli-input-json "${UPDATED_TASK_DEFINITION}")
    new_revision_arn=$(echo ${new_revision} | jq '.taskDefinition.taskDefinitionArn')
    export NEW_VERSION=$(echo ${new_revision} | jq '.taskDefinition.revision')
    echo "New revision ${NEW_VERSION} created"
}

the deploy to ecs:

deploy(){
    echo "Update service task revision"
    aws ecs update-service --cluster "${ECS_CLUSTER_NAME}" --service "${ECS_SERVICE_NAME}" --task-definition "${ECS_TASK_DEFINITION}:${NEW_VERSION}"

Any help is really appreciated.


Solution

  • I found the answer after some trial and error. So if someone happens to have the same issue here's the solution.

    prev_image=$(echo ${TASK_DEFINITION} | jq ".containerDefinitions[0].image | jq ".containerDefinitions[1].image")
    

    Needed only to get the image in the other container.