Skip to content

Commit 83cd85e

Browse files
committed
Fix db-migrate task for ADOT-enabled task definitions
This adds a new db-migrate buildspec that correctly retrieves the application container from task definitions that include an ADOT sidecar. The original db-migrate task assumed the first container in the task definition was the application container. With ADOT enabled, the task definition contains multiple containers, so the buildspec now retrieves the container by name instead.
1 parent 2a13318 commit 83cd85e

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
version: 0.2
2+
phases:
3+
pre_build:
4+
commands:
5+
# Check Image URI is not the default pipeline value
6+
- |
7+
if [[ "${IMAGE_URI}" = "MUST_BE_SET" ]]; then
8+
echo "The IMAGE_URI has not been set by the caller. The value of IMAGE_URI is \"${IMAGE_URI}\""
9+
exit 1
10+
fi
11+
12+
# Get task definition
13+
- echo "Existing ECS task definition name is ${TASK_DEFINITION_NAME}"
14+
- ECS_TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "${TASK_DEFINITION_NAME}")
15+
16+
# Delete any reference to the old image from the task definition and add the new image uri
17+
- NEW_ECS_TASK_DEFINITION=$(echo "${ECS_TASK_DEFINITION}" | jq --arg "INPUT_IMAGE_URI" "${IMAGE_URI}" --arg "APP_NAME" "${APP_NAME}" '.taskDefinition | .containerDefinitions |= map(if .name == $APP_NAME then .image = $INPUT_IMAGE_URI else . end) | del(.taskDefinitionArn) | del(.revision) | del(.status) | del(.requiresAttributes) | del(.compatibilities) | del(.registeredAt) | del(.registeredBy)')
18+
- echo "Replaced image uri with ${IMAGE_URI} for container ${APP_NAME}"
19+
20+
# Register a new task definition
21+
- |
22+
NEW_ECS_TASK_DEFINITION_ARN=$(aws ecs register-task-definition --cli-input-json "$NEW_ECS_TASK_DEFINITION" | jq -r '.taskDefinition.taskDefinitionArn' )
23+
echo "New ECS task definition ARN ${NEW_ECS_TASK_DEFINITION_ARN}"
24+
25+
# Get the existing configuration
26+
- ECS_CLUSTER_ARN=$(aws ecs describe-clusters --cluster "${CLUSTER_NAME}" | jq -r '.clusters[].clusterArn')
27+
- ECS_TASK_CONTAINER_DEFINITION_NAME=$(aws ecs describe-task-definition --task-definition "${TASK_DEFINITION_NAME}" | jq --arg "APP_NAME" "${APP_NAME}" -r '.taskDefinition.containerDefinitions[] | select(.name == $APP_NAME) | .name')
28+
- ECS_TASK_NETWORK_CONFIGURATION=$(aws ecs describe-services --cluster "${CLUSTER_NAME}" --services "${APP_NAME}" | jq '.services[].networkConfiguration[]')
29+
30+
# Write new task definition overriding the command the container runs
31+
- |
32+
jq -nrM \
33+
--arg "ECS_CLUSTER_ARN" "${ECS_CLUSTER_ARN}" \
34+
--arg "CONTAINER_DEFINITION_NAME" "${ECS_TASK_CONTAINER_DEFINITION_NAME}" \
35+
--arg "ECS_TASK_DEFINITION_ARN" "${NEW_ECS_TASK_DEFINITION_ARN}" \
36+
--argjson "ECS_TASK_NETWORK_CONFIGURATION" "${ECS_TASK_NETWORK_CONFIGURATION}" \
37+
'{
38+
"cluster": $ECS_CLUSTER_ARN,
39+
"taskDefinition": $ECS_TASK_DEFINITION_ARN,
40+
"count": 1,
41+
"launchType": "FARGATE",
42+
"overrides": {
43+
"containerOverrides": [
44+
{
45+
"name": $CONTAINER_DEFINITION_NAME,
46+
"command": ["rake", "db:migrate"],
47+
"environment": [
48+
{ "name": "VERBOSE", "value": "true" }
49+
]
50+
}
51+
]
52+
},
53+
"networkConfiguration": {
54+
"awsvpcConfiguration": $ECS_TASK_NETWORK_CONFIGURATION
55+
}
56+
}' > db_migrate_task_definition.json
57+
58+
- echo -e "New task definition \n $(<db_migrate_task_definition.json )"
59+
build:
60+
commands:
61+
- echo "Running database migration for ${APP_NAME}"
62+
- RUNNING_TASK_ARN=$(aws ecs run-task --cli-input-json "file://db_migrate_task_definition.json" | jq -r '.tasks[].taskArn')
63+
- echo "Running task ARN ${RUNNING_TASK_ARN}"
64+
- echo "Waiting for the task to finish"
65+
- aws ecs wait tasks-stopped --tasks "${RUNNING_TASK_ARN}" --cluster "${ECS_CLUSTER_ARN}"
66+
# Determine the exit code for the running task
67+
# Determine if the running task or the CodeBuild build failed
68+
# No failures: 0
69+
# Any failures: 1
70+
- |
71+
RUNNING_TASK_EXIT_CODE=$(\
72+
aws ecs describe-tasks --tasks "${RUNNING_TASK_ARN}" --cluster "${ECS_CLUSTER_ARN}" \
73+
| jq --arg "CONTAINER_NAME" "${ECS_TASK_CONTAINER_DEFINITION_NAME}" -r '.tasks[0].containers[] | select(.name == $CONTAINER_NAME) | .exitCode'
74+
)
75+
- exit "${RUNNING_TASK_EXIT_CODE}"

infra/deployments/forms/pipelines/deploy-forms-runner-container.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ module "db_migrate_runner" {
328328
project_description = "Run database migrations"
329329
environment = var.environment_name
330330
artifact_store_arn = module.artifact_bucket.arn
331-
buildspec = file("${path.root}/buildspecs/db-migrate/db-migrate.yml")
331+
buildspec = file("${path.root}/buildspecs/db-migrate/db-migrate-adot.yml")
332332
log_group_name = "codebuild/db_migrate_runner_${var.environment_name}"
333333
codebuild_service_role_arn = data.aws_iam_role.deployer_role.arn
334334
}

0 commit comments

Comments
 (0)