55# do not update the service
66# 2. Run the "db-migrate" command in the container as a new task
77#
8+ # Optional parameters:
9+ # --workspace <name> – Terraform workspace to select (for PR environments)
10+ #
811# Positional parameters:
912# app_name (required) – the name of subdirectory of /infra that holds the
1013# application's infrastructure code.
1518
1619set -euo pipefail
1720
21+ workspace=" "
22+ while : ; do
23+ case " ${1:- } " in
24+ --workspace)
25+ workspace=" $2 "
26+ shift 2
27+ ;;
28+ * )
29+ break
30+ ;;
31+ esac
32+ done
33+
1834app_name=" $1 "
1935image_tag=" $2 "
2036environment=" $3 "
4056db_migrator_user=$( terraform -chdir=" infra/${app_name} /app-config" output -json environment_configs | jq -r " .${environment} .database_config.migrator_username" )
4157
4258./bin/terraform-init " infra/${app_name} /service" " ${environment} "
59+
60+ if [ -n " ${workspace} " ]; then
61+ echo " Selecting Terraform workspace: ${workspace} "
62+ terraform -chdir=" infra/${app_name} /service" workspace select "${workspace}"
63+ fi
64+
4365migrator_role_arn=$( terraform -chdir=" infra/${app_name} /service" output -raw migrator_role_arn)
4466
4567echo
46- echo " ::group::Step 1. Update task definition without updating service"
4768
48- TF_CLI_ARGS_apply=" -input=false -auto-approve -var=image_tag=${image_tag}
49- -target=module.service.aws_ecs_task_definition.app
50- -target=module.service.aws_iam_role_policy.task_executor" \
51- make infra-update-app-service APP_NAME=" ${app_name} " ENVIRONMENT=" ${environment} "
69+ if [ -n " ${workspace} " ]; then
70+ # For PR environments, the task definition was already updated by the caller's
71+ # terraform apply, so skip the targeted apply (which would reinitialize terraform
72+ # and reset the workspace).
73+ echo " Skipping task definition update (already applied in workspace ${workspace} )"
74+ else
75+ echo " ::group::Step 1. Update task definition without updating service"
76+
77+ TF_CLI_ARGS_apply=" -input=false -auto-approve -var=image_tag=${image_tag}
78+ -target=module.service.aws_ecs_task_definition.app
79+ -target=module.service.aws_iam_role_policy.task_executor" \
80+ make infra-update-app-service APP_NAME=" ${app_name} " ENVIRONMENT=" ${environment} "
81+
82+ echo " ::endgroup::"
83+ fi
5284
53- echo " ::endgroup::"
5485echo
5586echo ' Step 2. Run "db-migrate" command'
5687
@@ -64,4 +95,4 @@ environment_variables=$(cat << EOF
6495EOF
6596)
6697
67- ./bin/run-command --task-role-arn " ${migrator_role_arn} " --environment-variables " ${environment_variables} " " ${app_name} " " ${environment} " " ${command} "
98+ ./bin/run-command ${workspace : +--workspace " ${workspace} " } --task-role-arn " ${migrator_role_arn} " --environment-variables " ${environment_variables} " " ${app_name} " " ${environment} " " ${command} "
0 commit comments