-
Notifications
You must be signed in to change notification settings - Fork 7
252 lines (230 loc) · 10.5 KB
/
aws_deploy_hydroserver.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
name: Deploy HydroServer to AWS Cloud Deployment
on:
workflow_dispatch:
inputs:
environment:
description: 'Enter a deployment environment name.'
required: true
hydroserver-version:
description: 'Enter a version of HydroServer to use. Leave blank to use the latest version.'
required: false
permissions:
id-token: write
contents: read
jobs:
deploy-backend:
name: Deploy Backend
runs-on: ubuntu-latest
environment: ${{ github.event.inputs.environment }}
steps:
- name: configureawscredentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/${{ vars.AWS_IAM_ROLE }}
role-session-name: create-hydroserver-resources
aws-region: ${{ vars.AWS_REGION }}
- name: Get Latest HydroServer Version
id: get_latest_tag
run: echo "tag=$(curl -sL https://api.github.com/repos/hydroserver2/hydroserver-api-services/releases/latest | jq -r '.tag_name')" >> $GITHUB_OUTPUT
- name: Checkout Backend Repo
uses: actions/checkout@v4
with:
repository: hydroserver2/hydroserver-api-services
ref: refs/tags/${{ github.event.inputs.hydroserver-version || steps.get_latest_tag.outputs.tag }}
path: backend
- name: Create Backend Deployment Package
working-directory: ./backend
run: zip -r deploy_package.zip ./
- name: Upload Deployment Package to S3
working-directory: ./backend
run: aws s3 cp deploy_package.zip s3://hydroserver-django-${{ github.event.inputs.environment }}-${{ vars.AWS_ACCOUNT_ID }}/deploy_package_${{ github.event.inputs.hydroserver-version || steps.get_latest_tag.outputs.tag }}.zip
- name: Configure Environment Variables
working-directory: ./backend
run: |
cat << EOF > environment.json
[
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "ADMIN_EMAIL",
"Value": "${{ vars.ADMIN_EMAIL }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "ALLOWED_HOSTS",
"Value": "${{ vars.ALLOWED_HOSTS }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "AWS_STORAGE_BUCKET_NAME",
"Value": "hydroserver-storage-${{ github.event.inputs.environment }}-${{ vars.AWS_ACCOUNT_ID }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "DATABASE_URL",
"Value": "${{ secrets.DATABASE_URL }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "DEBUG",
"Value": "${{ vars.DEBUG }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "DEPLOYMENT_BACKEND",
"Value": "aws"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "DISABLE_ACCOUNT_CREATION",
"Value": "${{ vars.DISABLE_ACCOUNT_CREATION }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "EMAIL_HOST",
"Value": "${{ vars.EMAIL_HOST }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "EMAIL_PORT",
"Value": "${{ vars.EMAIL_PORT }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "EMAIL_HOST_USER",
"Value": "${{ secrets.EMAIL_HOST_USER }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "EMAIL_HOST_PASSWORD",
"Value": "${{ secrets.EMAIL_HOST_PASSWORD }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "OAUTH_GOOGLE_CLIENT",
"Value": "${{ secrets.OAUTH_GOOGLE_CLIENT }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "OAUTH_GOOGLE_SECRET",
"Value": "${{ secrets.OAUTH_GOOGLE_SECRET }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "OAUTH_HYDROSHARE_CLIENT",
"Value": "${{ secrets.OAUTH_HYDROSHARE_CLIENT }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "OAUTH_HYDROSHARE_SECRET",
"Value": "${{ secrets.OAUTH_HYDROSHARE_SECRET }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "OAUTH_ORCID_CLIENT",
"Value": "${{ secrets.OAUTH_ORCID_CLIENT }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "OAUTH_ORCID_SECRET",
"Value": "${{ secrets.OAUTH_ORCID_SECRET }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "PROXY_BASE_URL",
"Value": "${{ vars.PROXY_BASE_URL }}"
},
{
"Namespace": "aws:elasticbeanstalk:application:environment",
"OptionName": "SECRET_KEY",
"Value": "${{ secrets.DJANGO_SECRET_KEY }}"
}
]
EOF
- name: Create ElasticBeanstalk Application Version
working-directory: ./backend
run: |
aws elasticbeanstalk create-application-version \
--application-name hydroserver-${{ github.event.inputs.environment }} \
--source-bundle S3Bucket="hydroserver-django-${{ github.event.inputs.environment }}-${{ vars.AWS_ACCOUNT_ID }}",S3Key="deploy_package_${{ github.event.inputs.hydroserver-version || steps.get_latest_tag.outputs.tag }}.zip" \
--version-label "${{ github.event.inputs.hydroserver-version || steps.get_latest_tag.outputs.tag }}" \
--description "HydroServer Version ${{ github.event.inputs.hydroserver-version || steps.get_latest_tag.outputs.tag }}" \
--auto-create-application
- name: Update ElasticBeanstalk Environment
working-directory: ./backend
run: |
aws elasticbeanstalk update-environment \
--environment-name hydroserver-${{ github.event.inputs.environment }}-env \
--version-label "${{ github.event.inputs.hydroserver-version || steps.get_latest_tag.outputs.tag }}" \
--option-settings file://environment.json
- name: Install Django Dependencies
working-directory: ./backend
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pyopenssl --upgrade
- name: Configure Environment Variables
working-directory: ./backend
run: |
cat << EOF > .env
PROXY_BASE_URL=http://127.0.0.1:8000
DATABASE_URL=${{ secrets.DATABASE_URL }}
DEPLOYED=True
AWS_STORAGE_BUCKET_NAME=hydroserver-storage-${{ github.event.inputs.environment }}-${{ vars.AWS_ACCOUNT_ID }}
EOF
- name: Run Django Update Commands
working-directory: ./backend
env:
DJANGO_SETTINGS_MODULE: hydroserver.settings
run: |
python manage.py migrate
deploy-frontend:
name: Build and Deploy Frontend
runs-on: ubuntu-latest
environment: ${{ github.event.inputs.environment }}
steps:
- name: configureawscredentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/${{ vars.AWS_IAM_ROLE }}
role-session-name: create-hydroserver-resources
aws-region: ${{ vars.AWS_REGION }}
- name: Get Latest HydroServer Version
id: get_latest_tag
run: echo "tag=$(curl -sL https://api.github.com/repos/hydroserver2/hydroserver-data-management-app/releases/latest | jq -r '.tag_name')" >> $GITHUB_OUTPUT
- name: Checkout Frontend Repo
uses: actions/checkout@v4
with:
repository: hydroserver2/hydroserver-data-management-app
ref: refs/tags/${{ github.event.inputs.hydroserver-version || steps.get_latest_tag.outputs.tag }}
path: frontend
- name: Setup Node 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: npm
cache-dependency-path: frontend/package-lock.json
- name: Install Dependencies
working-directory: ./frontend
run: npm ci
- name: Configure Environment Variables
working-directory: ./frontend
run: |
cat << EOF > .env
VITE_APP_VERSION=${{ github.event.inputs.hydroserver-version || steps.get_latest_tag.outputs.tag }}
VITE_APP_GOOGLE_MAPS_API_KEY=${{ secrets.GOOGLE_MAPS_API_KEY }}
VITE_APP_GOOGLE_MAPS_MAP_ID=${{ secrets.GOOGLE_MAPS_MAP_ID }}
VITE_APP_PROXY_BASE_URL=${{ vars.PROXY_BASE_URL }}
VITE_APP_DISABLE_ACCOUNT_CREATION=${{ vars.DISABLE_ACCOUNT_CREATION == 'True' && 'true' || 'false' }}
VITE_APP_GOOGLE_OAUTH_ENABLED=${{ secrets.OAUTH_GOOGLE_CLIENT && 'true' || 'false' }}
VITE_APP_ORCID_OAUTH_ENABLED=${{ secrets.OAUTH_ORCID_CLIENT && 'true' || 'false' }}
VITE_APP_HYDROSHARE_OAUTH_ENABLED=${{ secrets.OAUTH_HYDROSHARE_CLIENT && 'true' || 'false' }}
- name: Build Frontend
working-directory: ./frontend
run: npm run build
- name: Deploy to S3
working-directory: ./frontend
run: |
aws s3 sync ./dist s3://hydroserver-web-${{ github.event.inputs.environment }}-${{ vars.AWS_ACCOUNT_ID }}/ --delete
- name: Invalidate CloudFront Distribution Cache
run: |
aws cloudfront create-invalidation --distribution-id ${{ vars.CLOUDFRONT_ID }} --paths "/*"