Skip to content

Commit e69364f

Browse files
author
claudfuen
committed
fix: run migrations via CodeBuild instead of non-existent ECS task
- Add VPC access and DATABASE_URL to migration CodeBuild project - Modify buildspec-migration.yml to run migrations when RUN_MIGRATIONS=true - Update deploy.sh to use CodeBuild for migrations instead of ECS tasks - Update GitHub Actions workflow to use same CodeBuild approach - Fixes 'Invalid revision number. Number: latest' error
1 parent 9ad8eb8 commit e69364f

File tree

4 files changed

+45
-62
lines changed

4 files changed

+45
-62
lines changed

.github/workflows/deploy.yml

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,24 @@ jobs:
7878
7979
- name: Run database migrations
8080
run: |
81-
# Get subnet and security group for migration task
82-
PRIVATE_SUBNET=$(aws ec2 describe-subnets --filters "Name=tag:Type,Values=private" --query 'Subnets[0].SubnetId' --output text)
83-
CODEBUILD_SG=$(aws ec2 describe-security-groups --filters "Name=tag:Name,Values=pathfinder-codebuild-sg" --query 'SecurityGroups[0].GroupId' --output text)
84-
85-
echo "Using subnet: $PRIVATE_SUBNET"
86-
echo "Using security group: $CODEBUILD_SG"
87-
88-
TASK_ARN=$(aws ecs run-task \
89-
--cluster pathfinder \
90-
--task-definition pathfinder-migration:latest \
91-
--launch-type FARGATE \
92-
--network-configuration "awsvpcConfiguration={subnets=[$PRIVATE_SUBNET],securityGroups=[$CODEBUILD_SG],assignPublicIp=ENABLED}" \
93-
--query 'tasks[0].taskArn' --output text)
81+
# Run migrations via CodeBuild (which has VPC access to database)
82+
MIGRATION_RUN_ID=$(aws codebuild start-build \
83+
--project-name pathfinder-migration-build \
84+
--environment-variables-override name=RUN_MIGRATIONS,value=true \
85+
--query 'build.id' --output text)
9486
95-
echo "Migration task started: $TASK_ARN"
87+
echo "Migration run started: $MIGRATION_RUN_ID"
9688
97-
# Wait for migration task to complete
98-
aws ecs wait tasks-stopped --cluster pathfinder --tasks $TASK_ARN
89+
# Wait for migration run to complete
90+
while [ "$(aws codebuild batch-get-builds --ids $MIGRATION_RUN_ID --query 'builds[0].buildStatus' --output text)" = "IN_PROGRESS" ]; do
91+
echo "Waiting for migrations to complete..."
92+
sleep 30
93+
done
9994
10095
# Check if migration succeeded
101-
EXIT_CODE=$(aws ecs describe-tasks --cluster pathfinder --tasks $TASK_ARN \
102-
--query 'tasks[0].containers[0].exitCode' --output text)
103-
104-
if [ "$EXIT_CODE" != "0" ]; then
105-
echo "Migration failed with exit code: $EXIT_CODE"
96+
STATUS=$(aws codebuild batch-get-builds --ids $MIGRATION_RUN_ID --query 'builds[0].buildStatus' --output text)
97+
if [ "$STATUS" != "SUCCEEDED" ]; then
98+
echo "Migration failed with status: $STATUS"
10699
exit 1
107100
fi
108101
echo "Migrations completed successfully"

apps/infra/modules/build.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ export function createBuildSystem(config: CommonConfig, network: NetworkOutputs,
165165
},
166166
});
167167

168-
// CodeBuild project for building migration image (no database access needed)
168+
// CodeBuild project for building migration image (with database access for running migrations)
169169
const migrationProject = new aws.codebuild.Project("pathfinder-migration-build", {
170170
name: "pathfinder-migration-build",
171-
description: "Build migration Docker image (standalone)",
171+
description: "Build migration Docker image and run migrations",
172172
serviceRole: codebuildRole.arn,
173173
artifacts: {
174174
type: "NO_ARTIFACTS",
@@ -189,14 +189,24 @@ export function createBuildSystem(config: CommonConfig, network: NetworkOutputs,
189189
value: "pathfinder",
190190
type: "PLAINTEXT",
191191
},
192+
{
193+
name: "DATABASE_URL",
194+
value: database.connectionString,
195+
type: "PLAINTEXT",
196+
},
192197
{
193198
name: "AWS_DEFAULT_REGION",
194199
value: config.awsRegion,
195200
type: "PLAINTEXT",
196201
},
197202
],
198203
},
199-
// No VPC config - doesn't need database access
204+
// Add VPC config for database access
205+
vpcConfig: {
206+
vpcId: network.vpcId,
207+
subnets: network.privateSubnetIds,
208+
securityGroupIds: [network.securityGroups.codeBuild],
209+
},
200210
source: {
201211
type: "GITHUB",
202212
location: `https://github.com/${config.githubOrg}/${config.githubRepo}.git`,

apps/web/buildspec-migration.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ phases:
1717
- docker build -f Dockerfile.migration -t $IMAGE_REPO_NAME:migration-$IMAGE_TAG .
1818
- docker tag $IMAGE_REPO_NAME:migration-$IMAGE_TAG $REPOSITORY_URI:migration-$IMAGE_TAG
1919
- docker tag $IMAGE_REPO_NAME:migration-$IMAGE_TAG $REPOSITORY_URI:migration-latest
20+
- |
21+
if [ "$RUN_MIGRATIONS" = "true" ]; then
22+
echo "Running database migrations..."
23+
docker run --rm \
24+
-e DATABASE_URL="$DATABASE_URL" \
25+
-e NODE_ENV="production" \
26+
$IMAGE_REPO_NAME:migration-$IMAGE_TAG
27+
echo "Migrations completed successfully"
28+
fi
2029
2130
post_build:
2231
commands:

scripts/deploy.sh

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -67,46 +67,17 @@ migration_build_id=$(aws codebuild start-build \
6767
echo "Migration build ID: $migration_build_id"
6868
wait_for_build "$migration_build_id" "Migration"
6969

70-
# Step 3: Run Database Migrations
71-
echo -e "${YELLOW}🗃️ Step 3: Running database migrations...${NC}"
70+
# Step 3: Run Database Migrations via CodeBuild
71+
echo -e "${YELLOW}🗃️ Step 3: Running database migrations via CodeBuild...${NC}"
7272

73-
# Get subnet and security group for migration task
74-
private_subnet=$(aws ec2 describe-subnets \
75-
--filters "Name=tag:Type,Values=private" \
76-
--query 'Subnets[0].SubnetId' --output text)
77-
78-
codebuild_sg=$(aws ec2 describe-security-groups \
79-
--filters "Name=tag:Name,Values=pathfinder-codebuild-sg" \
80-
--query 'SecurityGroups[0].GroupId' --output text)
81-
82-
echo "Using subnet: $private_subnet"
83-
echo "Using security group: $codebuild_sg"
84-
85-
# Run migration task with correct syntax
86-
migration_task_arn=$(aws ecs run-task \
87-
--cluster "$CLUSTER_NAME" \
88-
--task-definition pathfinder-migration:latest \
89-
--launch-type FARGATE \
90-
--network-configuration "awsvpcConfiguration={subnets=[$private_subnet],securityGroups=[$codebuild_sg],assignPublicIp=ENABLED}" \
91-
--query 'tasks[0].taskArn' --output text)
92-
93-
echo "Migration task ARN: $migration_task_arn"
94-
95-
# Wait for migration to complete
96-
echo -e "${YELLOW}⏳ Waiting for migrations to complete...${NC}"
97-
aws ecs wait tasks-stopped --cluster "$CLUSTER_NAME" --tasks "$migration_task_arn"
98-
99-
# Check migration exit code
100-
exit_code=$(aws ecs describe-tasks \
101-
--cluster "$CLUSTER_NAME" \
102-
--tasks "$migration_task_arn" \
103-
--query 'tasks[0].containers[0].exitCode' --output text)
73+
# Run migration via CodeBuild (which has VPC access to database)
74+
migration_run_id=$(aws codebuild start-build \
75+
--project-name pathfinder-migration-build \
76+
--environment-variables-override name=RUN_MIGRATIONS,value=true \
77+
--query 'build.id' --output text)
10478

105-
if [ "$exit_code" != "0" ]; then
106-
echo -e "${RED}❌ Migration failed with exit code: $exit_code${NC}"
107-
exit 1
108-
fi
109-
echo -e "${GREEN}✅ Migrations completed successfully${NC}"
79+
echo "Migration run ID: $migration_run_id"
80+
wait_for_build "$migration_run_id" "Migration Run"
11081

11182
# Step 4: Build Application Image
11283
echo -e "${YELLOW}🔨 Step 4: Building application image...${NC}"

0 commit comments

Comments
 (0)