2828 required : true
2929 type : string
3030 description : Docker tag
31+ secrets :
32+ slack-webhook :
33+ required : true
3134
3235# Wait for other deploys to finish
3336concurrency :
3437 cancel-in-progress : false
35- group : api-deploy
38+ group : api-deploy-${{ inputs.env-name }}
3639
3740permissions :
3841 id-token : write
3942 contents : read
4043 issues : write
4144
42- env :
43- ECR_REPO : 917902836630.dkr.ecr.us-east-1.amazonaws.com/api-server
44- ECR_ADDRESS : 917902836630.dkr.ecr.us-east-1.amazonaws.com
45-
4645jobs :
4746 setup-vars :
48- runs-on : ubuntu-latest
49-
50- steps :
51- - name : Map env to AWS Role
52- id : role
53- run : |
54- ENV_NAME="${{ inputs.env-name }}"
55- case "$ENV_NAME" in
56- "dev")
57- echo 'role=arn:aws:iam::017925157769:role/cmiml-dev-oidc-github-role' >> "$GITHUB_OUTPUT"
58- ;;
59- "test")
60- echo 'role=arn:aws:iam::641513112151:role/cmiml-test-oidc-github-role' >> "$GITHUB_OUTPUT"
61- ;;
62- "uat")
63- echo 'role=arn:aws:iam::641513112151:role/cmiml-uat-oidc-github-role' >> "$GITHUB_OUTPUT"
64- ;;
65- "stage")
66- echo 'role=arn:aws:iam::641513112151:role/cmiml-stage-oidc-github-role' >> "$GITHUB_OUTPUT"
67- ;;
68- "prod")
69- echo 'role=arn:aws:iam::410431445687:role/cmiml-prod-oidc-github-role' >> "$GITHUB_OUTPUT"
70- ;;
71- "prod-dr")
72- echo 'role=arn:aws:iam::973422231492:role/cmiml-dr-oidc-github-role' >> "$GITHUB_OUTPUT"
73- ;;
74- *)
75- echo "Bad environment name"
76- exit 1;
77- esac
78-
79- - name : Map env to AWS Region
80- id : region
81- run : |
82- ENV_NAME="${{ inputs.env-name }}"
83- case "$ENV_NAME" in
84- "prod-dr")
85- echo "region=us-west-2" >> "$GITHUB_OUTPUT"
86- ;;
87- *)
88- echo "region=us-east-1" >> "$GITHUB_OUTPUT"
89- ;;
90- esac
91- - name : Map env to ECS cluster
92- id : cluster
93- run : |
94- ENV_NAME="${{ inputs.env-name }}"
95- case "$ENV_NAME" in
96- "prod-dr")
97- echo "cluster=prod-dr-us-west-2" >> "$GITHUB_OUTPUT"
98- ;;
99- *)
100- echo "cluster=cmiml-${ENV_NAME}" >> "$GITHUB_OUTPUT"
101- ;;
102- esac
103-
104- outputs :
105- role : ${{ steps.role.outputs.role }}
106- region : ${{ steps.region.outputs.region }}
107- cluster : ${{ steps.cluster.outputs.cluster }}
108-
109-
110- run-migration :
111- runs-on : ubuntu-latest
112- needs : [setup-vars]
113- env :
114- AWS_REGION : ${{ needs.setup-vars.outputs.region }}
115- steps :
116- - name : configure aws credentials
117- uses : aws-actions/configure-aws-credentials@v4
118- with :
119- role-to-assume : ${{ needs.setup-vars.outputs.role }}
120- role-session-name : OIDC-GHA-migration-session
121- aws-region : ${{ needs.setup-vars.outputs.region }}
122- - name : Download task definition
123- run : |
124- aws ecs describe-task-definition --task-definition migration --query taskDefinition > task-definition.json
125-
126- - name : Render Amazon ECS task definition
127- id : task-def
128- uses : aws-actions/amazon-ecs-render-task-definition@v1
129- with :
130- task-definition : task-definition.json
131- container-name : migrate
132- image : ${{ env.ECR_REPO }}:${{ inputs.image-tag }}
133- command : /fastapi-migrate
134- environment-variables : |
135- DD_VERSION=${{ inputs.image-tag }}
136- docker-labels : |
137- com.datadoghq.tags.version=${{ inputs.image-tag }}
138- - name : Update Task Definition
139- id : taskdef
140- uses : aws-actions/amazon-ecs-deploy-task-definition@v2
141- with :
142- task-definition : ${{ steps.task-def.outputs.task-definition }}
143- - name : Run migration
144- run : |-
145- CONTAINER_NAME=migrate
146- start=`date +%s`
147-
148- echo "Starting migration task"
149- TASK_ID=$(aws ecs run-task --task-definition "${{ steps.taskdef.outputs.task-definition-arn }}" --cluster ${{ needs.setup-vars.outputs.cluster }} | jq -r '.tasks[0].taskArn')
150-
151- echo "Waiting for task ${{ steps.taskdef.outputs.task-definition-arn }} to finish..."
152- aws ecs wait tasks-stopped --cluster ${{ needs.setup-vars.outputs.cluster }} --tasks "$TASK_ID"
153-
154- TASK_EXIT_CODE=$(aws ecs describe-tasks --cluster ${{ needs.setup-vars.outputs.cluster }} --tasks "$TASK_ID" --query "tasks[0].containers[?name=='$CONTAINER_NAME'].exitCode" --output text)
155-
156- end=`date +%s`
157- runtime=$((end-start))
158-
159- echo "Task finished with exit code $TASK_EXIT_CODE after $runtime"
160-
161- echo "exit-code=${TASK_EXIT_CODE}" >> $GITHUB_OUTPUT
162- echo "task-time=${runtime}" >> $GITHUB_OUTPUT
163-
164- if [ $TASK_EXIT_CODE -gt 0 ]; then
165- exit 1;
166- fi
167-
168- on-migration-failure :
169- runs-on : ubuntu-latest
170- if : ${{ !cancelled() && (needs.run-migration.result == 'failure' || needs.run-migration.result == 'timed_out') }}
171- needs :
172- - run-migration
173- steps :
174- - uses : actions/checkout@v4
175- - name : " Send Slack message on failure"
176- uses : rtCamp/action-slack-notify@v2
177- env :
178- SLACK_COLOR : failure
179- SLACK_WEBHOOK : ${{ secrets.SLACK_WEBHOOK_DEPLOY_TO_DEV }}
180- MSG_MINIMAL : actions url
181- SLACK_TITLE : Database Migration in DEV environment
182- SLACK_MESSAGE : ' 🚨 Error when executing migration!'
183-
184- on-migration-success :
185- runs-on : ubuntu-latest
186- if : ${{ !cancelled() && (needs.run-migration.result == 'success') }}
187- needs :
188- - run-migration
189- steps :
190- - uses : actions/checkout@v4
191- - name : " Send Slack message on success"
192- uses : rtCamp/action-slack-notify@v2
193- env :
194- SLACK_COLOR : success
195- SLACK_WEBHOOK : ${{ secrets.SLACK_WEBHOOK_DEPLOY_TO_DEV }}
196- MSG_MINIMAL : actions url
197- SLACK_TITLE : Database Migration in DEV environment
198- SLACK_MESSAGE : ' 🎉 Migration was successful!'
47+ uses : ./.github/workflows/_setup.yaml
48+ with :
49+ env-name : ${{ inputs.env-name }}
19950
20051 deploy :
20152 runs-on : ubuntu-latest
202- needs : [setup-vars, run-migration ]
53+ needs : [setup-vars]
20354
20455 name : Deploy ${{ matrix.apps.name }}
20556 strategy :
23182 with :
23283 task-definition : task-definition.json
23384 container-name : ${{ matrix.apps.container }}
234- image : ${{ env.ECR_REPO }}:${{ inputs.image-tag }}
85+ image : ${{ needs.setup-vars.outputs.ecr-repo }}/api-server :${{ inputs.image-tag }}
23586
23687 - name : Update Task Definition
23788 uses : aws-actions/amazon-ecs-deploy-task-definition@v2
@@ -253,9 +104,9 @@ jobs:
253104 uses : rtCamp/action-slack-notify@v2
254105 env :
255106 SLACK_COLOR : failure
256- SLACK_WEBHOOK : ${{ secrets.SLACK_WEBHOOK_DEPLOY_TO_DEV }}
107+ SLACK_WEBHOOK : ${{ secrets.slack-webhook }}
257108 MSG_MINIMAL : actions url
258- SLACK_TITLE : Deployment to DEV environment
109+ SLACK_TITLE : Deployment to ${{ inputs.env-name }} environment
259110 SLACK_MESSAGE : ' 🚨 Error when executing deployment!'
260111
261112 on-deploy-success :
@@ -269,7 +120,7 @@ jobs:
269120 uses : rtCamp/action-slack-notify@v2
270121 env :
271122 SLACK_COLOR : success
272- SLACK_WEBHOOK : ${{ secrets.SLACK_WEBHOOK_DEPLOY_TO_DEV }}
123+ SLACK_WEBHOOK : ${{ secrets.slack-webhook }}
273124 MSG_MINIMAL : actions url
274- SLACK_TITLE : Deployment to DEV environment
125+ SLACK_TITLE : Deployment to ${{ inputs.env-name }} environment
275126 SLACK_MESSAGE : ' 🚀 Deployment was successful!'
0 commit comments