1717
1818 - name : Set environment based on branch
1919 run : |
20- if [[ ${{ github.ref }} == 'refs/heads/main ' ]]; then
20+ if [[ ${{ github.ref }} == 'refs/heads/production ' ]]; then
2121 echo "ENVIRONMENT=prod" >> $GITHUB_ENV
2222 echo "STACK_NAME=neoapi-prod" >> $GITHUB_ENV
2323 else
@@ -35,15 +35,41 @@ jobs:
3535 - name : Login to Amazon ECR
3636 id : login-ecr
3737 uses : aws-actions/amazon-ecr-login@v2
38-
38+
39+ - name : Create ECR repository if it doesn't exist
40+ run : |
41+ aws ecr describe-repositories --repository-names neoapi-${{ env.ENVIRONMENT }} --region ${{ env.AWS_REGION }} 2>/dev/null || \
42+ aws ecr create-repository \
43+ --repository-name neoapi-${{ env.ENVIRONMENT }} \
44+ --region ${{ env.AWS_REGION }} \
45+ --image-scanning-configuration scanOnPush=true
46+
47+ - name : Build, tag, and push image to Amazon ECR
48+ run : |
49+ ECR_REPOSITORY=${{ steps.login-ecr.outputs.registry }}/neoapi-${{ env.ENVIRONMENT }}
50+ IMAGE_TAG=${{ github.sha }}
51+
52+ echo "Building Docker image..."
53+ docker build -t $ECR_REPOSITORY:$IMAGE_TAG .
54+ docker tag $ECR_REPOSITORY:$IMAGE_TAG $ECR_REPOSITORY:latest
55+
56+ echo "Pushing to ECR..."
57+ docker push $ECR_REPOSITORY:$IMAGE_TAG
58+ docker push $ECR_REPOSITORY:latest
59+
60+ echo "IMAGE_URI=$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_ENV
61+ echo "ECR_REPOSITORY=$ECR_REPOSITORY" >> $GITHUB_ENV
62+
3963 - name : Deploy CloudFormation stack
4064 run : |
65+ echo "Deploying infrastructure with image: ${{ env.IMAGE_URI }}"
66+
4167 aws cloudformation deploy \
4268 --template-file infrastructure/cloudformation-template.yaml \
4369 --stack-name ${{ env.STACK_NAME }} \
4470 --parameter-overrides \
4571 Environment=${{ env.ENVIRONMENT }} \
46- ImageUri="placeholder" \
72+ ImageUri=${{ env.IMAGE_URI }} \
4773 RDSHostname=${{ secrets.RDS_HOSTNAME }} \
4874 RDSDatabase=${{ secrets.RDS_DATABASE }} \
4975 RDSUsername=${{ secrets.RDS_USERNAME }} \
5278 PrivateSubnets=${{ secrets.PRIVATE_SUBNETS }} \
5379 --capabilities CAPABILITY_NAMED_IAM \
5480 --region ${{ env.AWS_REGION }}
81+
82+ - name : Wait for deployment to complete
83+ run : |
84+ echo "Waiting for CloudFormation stack to complete..."
85+ aws cloudformation wait stack-deploy-complete \
86+ --stack-name ${{ env.STACK_NAME }} \
87+ --region ${{ env.AWS_REGION }}
5588
5689 - name : Get ECR repository URI
5790 run : |
@@ -61,31 +94,37 @@ jobs:
6194 --output text \
6295 --region ${{ env.AWS_REGION }})
6396 echo "ECR_REPOSITORY=$ECR_URI" >> $GITHUB_ENV
64-
65- - name : Build, tag, and push image to Amazon ECR
97+
98+ - name : Get service URL
6699 run : |
67- IMAGE_TAG=${{ github.sha }}
68- docker build -t $ECR_REPOSITORY:$IMAGE_TAG .
69- docker tag $ECR_REPOSITORY:$IMAGE_TAG $ECR_REPOSITORY:latest
70- docker push $ECR_REPOSITORY:$IMAGE_TAG
71- docker push $ECR_REPOSITORY:latest
72- echo "IMAGE_URI=$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_ENV
100+ SERVICE_URL=$(aws cloudformation describe-stacks \
101+ --stack-name ${{ env.STACK_NAME }} \
102+ --query 'Stacks[0].Outputs[?OutputKey==`ServiceUrl`].OutputValue' \
103+ --output text \
104+ --region ${{ env.AWS_REGION }})
105+
106+ echo "🚀 Deployment complete!"
107+ echo "Service URL: $SERVICE_URL"
108+ echo "Environment: ${{ env.ENVIRONMENT }}"
109+ echo "Image: ${{ env.IMAGE_URI }}"
73110
74- - name : Update App Runner service with new image
111+ - name : Health check
75112 run : |
76- aws cloudformation update-stack \
113+ SERVICE_URL=$( aws cloudformation describe-stacks \
77114 --stack-name ${{ env.STACK_NAME }} \
78- --use-previous-template \
79- --parameters \
80- ParameterKey=Environment,UsePreviousValue=true \
81- ParameterKey=ImageUri,ParameterValue=${{ env.IMAGE_URI }} \
82- ParameterKey=RDSHostname,UsePreviousValue=true \
83- ParameterKey=RDSDatabase,UsePreviousValue=true \
84- ParameterKey=VPCId,UsePreviousValue=true \
85- ParameterKey=PrivateSubnets,UsePreviousValue=true \
86- --capabilities CAPABILITY_NAMED_IAM \
87- --region ${{ env.AWS_REGION }}
115+ --query 'Stacks[0].Outputs[?OutputKey==`ServiceUrl`].OutputValue' \
116+ --output text \
117+ --region ${{ env.AWS_REGION }})
88118
89- aws cloudformation wait stack-update-complete \
90- --stack-name ${{ env.STACK_NAME }} \
91- --region ${{ env.AWS_REGION }}
119+ echo "Performing health check..."
120+ sleep 30 # Give the service time to start
121+
122+ for i in {1..5}; do
123+ if curl -f -s "${SERVICE_URL}/v2.0/routes/healthwatch" > /dev/null; then
124+ echo "✅ Health check passed!"
125+ exit 0
126+ else
127+ echo "⏳ Health check attempt $i/5 failed, retrying in 30 seconds..."
128+ sleep 30
129+ fi
130+ done
0 commit comments