-
Notifications
You must be signed in to change notification settings - Fork 7
195 lines (167 loc) · 7.13 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# This workflow will build and push a new container image to Amazon ECR,
# and then will deploy a new task definition for each of the services to Amazon ECS.
# Expected vars:
# The following vars must be set at the repo level. Their values must be JSON and contain one key per
# environment (dev, prod, etc.) and the value for each key must be the value for that environment.
#
# DEPLOY_APP_NAME: {"dev": "app1", "prod": "app2"}
# DEPLOY_AWS_REGION: {"dev": "us-west-2", "prod": "us-east-1"}
# AWS_ACCOUNT: {"dev": "123456789012", "prod": "123456789012"}
# Note: The names of repository, cluster, services match what is configured in https://github.com/dimagi/ocs-deploy
name: Deploy to Amazon ECS
on:
workflow_dispatch:
inputs:
environment:
description: "Deploy environment"
required: true
type: choice
options:
- dev
- prod
workflow_run:
workflows: [ Lint and Test ]
types: [completed]
branches: [main]
permissions:
id-token: write
contents: read
deployments: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ inputs.environment }}
cancel-in-progress: true
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set Deploy Env
# Set the deploy env based on the input from the event or else from the branch
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
DEPLOY_ENV="${{ inputs.environment }}"
elif [[ "${{github.base_ref}}" == "main" || "${{github.ref}}" == "refs/heads/main" ]]; then
DEPLOY_ENV="prod"
else
DEPLOY_ENV="dev"
fi
echo "DEPLOY_ENV=$DEPLOY_ENV" >> "$GITHUB_ENV"
- name: Set variables
# Set other variables accordingly
run: |
# you can't reference the `env` context when defining other env vars to do it here
APP_NAME="${{ fromJSON(vars.DEPLOY_APP_NAME)[env.DEPLOY_ENV] }}"
echo "APP_NAME=$APP_NAME" >> "$GITHUB_ENV"
echo "ECR_REPOSITORY=$APP_NAME-${{ env.DEPLOY_ENV }}-ecr-repo" >> "$GITHUB_ENV"
echo "ECS_CLUSTER=$APP_NAME-${{ env.DEPLOY_ENV }}-Cluster" >> "$GITHUB_ENV"
echo "ECS_SERVICE_DJANGO=$APP_NAME-${{ env.DEPLOY_ENV }}-Django" >> "$GITHUB_ENV"
echo "ECS_SERVICE_CELERY=$APP_NAME-${{ env.DEPLOY_ENV }}-Celery" >> "$GITHUB_ENV"
echo "ECS_SERVICE_CELERY_BEAT=$APP_NAME-${{ env.DEPLOY_ENV }}-CeleryBeat" >> "$GITHUB_ENV"
- name: Create GitHub deployment
uses: chrnorm/deployment-action@v2
id: deployment
with:
token: '${{ github.token }}'
environment: "aws-${{ env.DEPLOY_ENV }}"
production-environment: ${{ env.DEPLOY_ENV == 'prod' }}
description: "Deploying ${{ github.head_ref || github.ref }} to AWS ${{ env.DEPLOY_ENV }}"
- name: configure aws credentials
uses: aws-actions/[email protected]
with:
role-to-assume: "arn:aws:iam::${{ fromJSON(vars.AWS_ACCOUNT)[env.DEPLOY_ENV] }}:role/github_deploy"
role-session-name: GithubDeploy
aws-region: ${{ fromJSON(vars.DEPLOY_AWS_REGION)[env.DEPLOY_ENV] }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/[email protected]
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Get image names
id: image-name
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
echo "image_latest=$ECR_REGISTRY/$ECR_REPOSITORY:latest" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v6
with:
push: true
tags: |
${{ steps.image-name.outputs.image }}
${{ steps.image-name.outputs.image_latest }}
file: Dockerfile
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Update ECS task def for Django web container
id: django-web-def
uses: aws-actions/[email protected]
with:
task-definition-family: ${{ env.APP_NAME }}-${{ env.DEPLOY_ENV }}-Django
container-name: web
image: ${{ steps.image-name.outputs.image }}
- name: Update ECS task def for Django migrations container
id: django-migrations-def
uses: aws-actions/[email protected]
with:
task-definition: ${{ steps.django-web-def.outputs.task-definition }}
container-name: migrate
image: ${{ steps.image-name.outputs.image }}
- name: Update ECS task def for Celery worker container
id: celery-worker-def
uses: aws-actions/[email protected]
with:
task-definition-family: ${{ env.APP_NAME }}-${{ env.DEPLOY_ENV }}-CeleryWorkerTask
container-name: celery-worker
image: ${{ steps.image-name.outputs.image }}
- name: Update ECS task def for Celery beat container
id: celery-beat-def
uses: aws-actions/[email protected]
with:
task-definition-family: ${{ env.APP_NAME }}-${{ env.DEPLOY_ENV }}-CeleryBeatTask
container-name: celery-beat
image: ${{ steps.image-name.outputs.image }}
- name: Deploy Django Web
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ steps.django-migrations-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE_DJANGO }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: false
- name: Deploy Celery Worker
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ steps.celery-worker-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE_CELERY }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: false
- name: Deploy Celery Beat
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ steps.celery-beat-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE_CELERY_BEAT }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: false
- name: Wait for service stability
run: |
aws ecs wait services-stable --cluster $ECS_CLUSTER --services $ECS_SERVICE_DJANGO $ECS_SERVICE_CELERY $ECS_SERVICE_CELERY_BEAT
- name: Update deployment status (success)
if: success()
uses: chrnorm/deployment-status@v2
with:
token: '${{ github.token }}'
environment-url: ${{ steps.deployment.outputs.environment_url }}
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
state: 'success'
- name: Update deployment status (failure)
if: failure()
uses: chrnorm/deployment-status@v2
with:
token: '${{ github.token }}'
environment-url: ${{ steps.deployment.outputs.environment_url }}
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
state: 'failure'