Skip to content

Commit 5b8206f

Browse files
committed
Auto stash before rebase of "develop" onto "origin/search-dev"
Updated the github action to switch DBs between dev and prod.
1 parent 430b290 commit 5b8206f

File tree

1 file changed

+119
-117
lines changed

1 file changed

+119
-117
lines changed

.github/workflows/deploy.yml

Lines changed: 119 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -3,128 +3,130 @@ name: Deploy Neotoma API
33
on:
44
push:
55
branches: [production, develop]
6-
6+
77
env:
88
AWS_REGION: us-east-2
9-
9+
1010
jobs:
1111
deploy:
1212
runs-on: ubuntu-latest
13-
13+
1414
steps:
15-
- name: Checkout code
16-
uses: actions/checkout@v4
17-
18-
- name: Set environment based on branch
19-
run: |
20-
if [[ ${{ github.ref }} == 'refs/heads/production' ]]; then
21-
echo "ENVIRONMENT=prod" >> $GITHUB_ENV
22-
echo "STACK_NAME=neoapi-prod" >> $GITHUB_ENV
23-
else
24-
echo "ENVIRONMENT=dev" >> $GITHUB_ENV
25-
echo "STACK_NAME=neoapi-dev" >> $GITHUB_ENV
26-
fi
27-
28-
- name: Configure AWS credentials
29-
uses: aws-actions/configure-aws-credentials@v4
30-
with:
31-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
32-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
33-
aws-region: ${{ env.AWS_REGION }}
34-
35-
- name: Login to Amazon ECR
36-
id: login-ecr
37-
uses: aws-actions/amazon-ecr-login@v2
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-
63-
- name: Deploy CloudFormation stack
64-
run: |
65-
echo "Deploying infrastructure with image: ${{ env.IMAGE_URI }}"
66-
67-
aws cloudformation deploy \
68-
--template-file infrastructure/cloudformation-template.yaml \
69-
--stack-name ${{ env.STACK_NAME }} \
70-
--parameter-overrides \
71-
Environment=${{ env.ENVIRONMENT }} \
72-
ImageUri=${{ env.IMAGE_URI }} \
73-
RDSHostname=${{ secrets.RDS_HOSTNAME }} \
74-
RDSDatabase=${{ secrets.RDS_DATABASE }} \
75-
RDSUsername=${{ secrets.RDS_USERNAME }} \
76-
RDSPassword=${{ secrets.RDS_PASSWORD }} \
77-
VPCId=${{ secrets.VPC_ID }} \
78-
PrivateSubnets=${{ secrets.PRIVATE_SUBNETS }} \
79-
--capabilities CAPABILITY_NAMED_IAM \
80-
--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 }}
88-
89-
- name: Get ECR repository URI
90-
run: |
91-
ECR_URI=$(aws cloudformation describe-stacks \
92-
--stack-name ${{ env.STACK_NAME }} \
93-
--query 'Stacks[0].Outputs[?OutputKey==`ECRRepository`].OutputValue' \
94-
--output text \
95-
--region ${{ env.AWS_REGION }})
96-
echo "ECR_REPOSITORY=$ECR_URI" >> $GITHUB_ENV
97-
98-
- name: Get service URL
99-
run: |
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 }}"
110-
111-
- name: Health check
112-
run: |
113-
SERVICE_URL=$(aws cloudformation describe-stacks \
114-
--stack-name ${{ env.STACK_NAME }} \
115-
--query 'Stacks[0].Outputs[?OutputKey==`ServiceUrl`].OutputValue' \
116-
--output text \
117-
--region ${{ env.AWS_REGION }})
118-
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
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Set environment based on branch
19+
run: |
20+
if [[ ${{ github.ref }} == 'refs/heads/production' ]]; then
21+
echo "ENVIRONMENT=prod" >> $GITHUB_ENV
22+
echo "STACK_NAME=neoapi-prod" >> $GITHUB_ENV
23+
echo "RDSDB=neotoma" >> $GITHUB_ENV
12624
else
127-
echo "⏳ Health check attempt $i/5 failed, retrying in 30 seconds..."
128-
sleep 30
25+
echo "ENVIRONMENT=dev" >> $GITHUB_ENV
26+
echo "STACK_NAME=neoapi-dev" >> $GITHUB_ENV
27+
echo "RDSDB=neotomatank" >> $GITHUB_ENV
12928
fi
130-
done
29+
30+
- name: Configure AWS credentials
31+
uses: aws-actions/configure-aws-credentials@v4
32+
with:
33+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
34+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
35+
aws-region: ${{ env.AWS_REGION }}
36+
37+
- name: Login to Amazon ECR
38+
id: login-ecr
39+
uses: aws-actions/amazon-ecr-login@v2
40+
41+
- name: Create ECR repository if it doesn't exist
42+
run: |
43+
aws ecr describe-repositories --repository-names neoapi-${{ env.ENVIRONMENT }} --region ${{ env.AWS_REGION }} 2>/dev/null || \
44+
aws ecr create-repository \
45+
--repository-name neoapi-${{ env.ENVIRONMENT }} \
46+
--region ${{ env.AWS_REGION }} \
47+
--image-scanning-configuration scanOnPush=true
48+
49+
- name: Build, tag, and push image to Amazon ECR
50+
run: |
51+
ECR_REPOSITORY=${{ steps.login-ecr.outputs.registry }}/neoapi-${{ env.ENVIRONMENT }}
52+
IMAGE_TAG=${{ github.sha }}
53+
54+
echo "Building Docker image..."
55+
docker build -t $ECR_REPOSITORY:$IMAGE_TAG .
56+
docker tag $ECR_REPOSITORY:$IMAGE_TAG $ECR_REPOSITORY:latest
57+
58+
echo "Pushing to ECR..."
59+
docker push $ECR_REPOSITORY:$IMAGE_TAG
60+
docker push $ECR_REPOSITORY:latest
61+
62+
echo "IMAGE_URI=$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_ENV
63+
echo "ECR_REPOSITORY=$ECR_REPOSITORY" >> $GITHUB_ENV
64+
65+
- name: Deploy CloudFormation stack
66+
run: |
67+
echo "Deploying infrastructure with image: ${{ env.IMAGE_URI }}"
68+
69+
aws cloudformation deploy \
70+
--template-file infrastructure/cloudformation-template.yaml \
71+
--stack-name ${{ env.STACK_NAME }} \
72+
--parameter-overrides \
73+
Environment=${{ env.ENVIRONMENT }} \
74+
ImageUri=${{ env.IMAGE_URI }} \
75+
RDSHostname=${{ secrets.RDS_HOSTNAME }} \
76+
RDSDatabase=${{ env.RDSDB }} \
77+
RDSUsername=${{ secrets.RDS_USERNAME }} \
78+
RDSPassword=${{ secrets.RDS_PASSWORD }} \
79+
VPCId=${{ secrets.VPC_ID }} \
80+
PrivateSubnets=${{ secrets.PRIVATE_SUBNETS }} \
81+
--capabilities CAPABILITY_NAMED_IAM \
82+
--region ${{ env.AWS_REGION }}
83+
84+
- name: Wait for deployment to complete
85+
run: |
86+
echo "Waiting for CloudFormation stack to complete..."
87+
aws cloudformation wait stack-deploy-complete \
88+
--stack-name ${{ env.STACK_NAME }} \
89+
--region ${{ env.AWS_REGION }}
90+
91+
- name: Get ECR repository URI
92+
run: |
93+
ECR_URI=$(aws cloudformation describe-stacks \
94+
--stack-name ${{ env.STACK_NAME }} \
95+
--query 'Stacks[0].Outputs[?OutputKey==`ECRRepository`].OutputValue' \
96+
--output text \
97+
--region ${{ env.AWS_REGION }})
98+
echo "ECR_REPOSITORY=$ECR_URI" >> $GITHUB_ENV
99+
100+
- name: Get service URL
101+
run: |
102+
SERVICE_URL=$(aws cloudformation describe-stacks \
103+
--stack-name ${{ env.STACK_NAME }} \
104+
--query 'Stacks[0].Outputs[?OutputKey==`ServiceUrl`].OutputValue' \
105+
--output text \
106+
--region ${{ env.AWS_REGION }})
107+
108+
echo "🚀 Deployment complete!"
109+
echo "Service URL: $SERVICE_URL"
110+
echo "Environment: ${{ env.ENVIRONMENT }}"
111+
echo "Image: ${{ env.IMAGE_URI }}"
112+
113+
- name: Health check
114+
run: |
115+
SERVICE_URL=$(aws cloudformation describe-stacks \
116+
--stack-name ${{ env.STACK_NAME }} \
117+
--query 'Stacks[0].Outputs[?OutputKey==`ServiceUrl`].OutputValue' \
118+
--output text \
119+
--region ${{ env.AWS_REGION }})
120+
121+
echo "Performing health check..."
122+
sleep 30 # Give the service time to start
123+
124+
for i in {1..5}; do
125+
if curl -f -s "${SERVICE_URL}/v2.0/routes/healthwatch" > /dev/null; then
126+
echo "✅ Health check passed!"
127+
exit 0
128+
else
129+
echo "⏳ Health check attempt $i/5 failed, retrying in 30 seconds..."
130+
sleep 30
131+
fi
132+
done

0 commit comments

Comments
 (0)