-
Notifications
You must be signed in to change notification settings - Fork 1
97 lines (83 loc) · 2.92 KB
/
deploy.yml
File metadata and controls
97 lines (83 loc) · 2.92 KB
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
name: Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
env:
SECRET_KEY: ${{ secrets.SECRET_KEY }}
DB_NAME: ${{ secrets.DB_NAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_PORT: ${{ secrets.DB_PORT }}
CLIENT_ORIGIN: ${{ secrets.CLIENT_ORIGIN }}
CLIENT_HOST: ${{ secrets.CLIENT_HOST }}
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Load Pipenv cache
uses: actions/cache@v2
with:
path: ~/.local/share/virtualenvs
key: ${{ runner.os }}-pipenv-${{ hashFiles('Pipfile.lock') }}
restore-keys: |
${{ runner.os }}-pipenv-
- name: Install python dependencies
run: |
python -m pip install --upgrade pip
pip install pipenv
pipenv install -d
- name: Output requirements.txt for gcloud deploy
run: pipenv lock -r > requirements.txt
- name: Collect Django static assets
run: pipenv run python manage.py collectstatic
- name: Inject app.yml environment variables for gcloud App Engine deployment
uses: ikuanyshbekov/app-yaml-env-compiler@v1.0
- name: Setup Google Cloud SDK
uses: google-github-actions/setup-gcloud@v0.2.0
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}
- name: Deploy to App Engine
run: gcloud app deploy --quiet
- name: Backup database
env:
DB_INSTANCE_NAME: ${{ secrets.DB_INSTANCE_NAME }}
run: |
gcloud sql backups create --instance=$DB_INSTANCE_NAME
- name: Get Cloud SQL Proxy binary
run: |
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
chmod +x cloud_sql_proxy
- name: Migrate database
env:
DB_HOST: 127.0.0.1
DB_PORT: 3306
DB_INSTANCE_CONNECTION_NAME: ${{ secrets.DB_INSTANCE_CONNECTION_NAME }}
run: |
# Connect to Cloud SQL PRoxy in the background
# Push stdout and stderr to proxy.log file
./cloud_sql_proxy -instances=$DB_INSTANCE_CONNECTION_NAME=tcp:3306 >proxy.log 2>&1 &
# Save the process id
PID=$!
# Wait up to 20 seconds for cloud sql proxy to be ready
for attempt in $(seq 1 20); do
sleep 1
if grep -q "Ready for new connections" proxy.log; then
echo "Cloud SQL Proxy is ready for new connections"
break
fi
if [[ attempt -eq 20 ]]; then
echo "Error launching sql proxy"
exit
fi
done
# Run migrations
pipenv run python manage.py migrate
# Kill the process
kill $PID