@@ -5,10 +5,46 @@ phases:
55 commands :
66 - echo "Logging in to Amazon ECR..."
77 - aws --version
8+ - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
9+ - REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
10+ - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
11+ - IMAGE_TAG=${COMMIT_HASH:=latest}
12+ - echo "Repository URI is $REPOSITORY_URI"
13+ - echo "Image tag is $IMAGE_TAG"
14+ - echo "DATABASE_URL is configured for build-time access"
15+
816 build :
917 commands :
10- - echo "Build started"
11- - echo "Test build phase"
18+ - echo "Build started on $(date)"
19+ - echo "=== PHASE 1 : Building and pushing migration image ==="
20+ - cd apps/web
21+ - docker build -f Dockerfile.migration -t $IMAGE_REPO_NAME:migration-$IMAGE_TAG .
22+ - docker tag $IMAGE_REPO_NAME:migration-$IMAGE_TAG $REPOSITORY_URI:migration-$IMAGE_TAG
23+ - docker tag $IMAGE_REPO_NAME:migration-$IMAGE_TAG $REPOSITORY_URI:migration-latest
24+ - docker push $REPOSITORY_URI:migration-$IMAGE_TAG
25+ - docker push $REPOSITORY_URI:migration-latest
26+ - echo "=== PHASE 2 : Running database migrations ==="
27+ - TASK_ARN=$(aws ecs run-task --cluster $CLUSTER_NAME --task-definition pathfinder-migration --network-configuration "awsvpcConfiguration={subnets=[$SUBNET_ID],securityGroups=[$SECURITY_GROUP_ID],assignPublicIp=ENABLED}" --launch-type FARGATE --query 'tasks[0].taskArn' --output text)
28+ - echo "Migration task started : $TASK_ARN"
29+ - aws ecs wait tasks-stopped --cluster $CLUSTER_NAME --tasks $TASK_ARN
30+ - EXIT_CODE=$(aws ecs describe-tasks --cluster $CLUSTER_NAME --tasks $TASK_ARN --query 'tasks[0].containers[0].exitCode' --output text)
31+ - if [ "$EXIT_CODE" != "0" ]; then echo "Migration failed with exit code : $EXIT_CODE"; exit 1; fi
32+ - echo "Migrations completed successfully!"
33+ - echo "=== PHASE 3 : Building main app image with database access ==="
34+ - docker build --build-arg DATABASE_URL=$DATABASE_URL -t $IMAGE_REPO_NAME:$IMAGE_TAG .
35+ - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $REPOSITORY_URI:$IMAGE_TAG
36+ - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $REPOSITORY_URI:latest
37+
1238 post_build :
1339 commands :
14- - echo "Build completed"
40+ - echo "Build completed on $(date)"
41+ - echo "Pushing the Docker images..."
42+ - docker push $REPOSITORY_URI:$IMAGE_TAG
43+ - docker push $REPOSITORY_URI:latest
44+ - echo "Writing image definitions file..."
45+ - printf '[{"name":"pathfinder-app","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
46+
47+ artifacts :
48+ files :
49+ - imagedefinitions.json
50+ name : pathfinder-build-$(date +%Y-%m-%d)
0 commit comments