-
Notifications
You must be signed in to change notification settings - Fork 0
168 lines (140 loc) · 5.3 KB
/
deploy-backend.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
# GitHub action to deploy MartinBullmanApp backend.
name: deploy-backend
on:
# trigger workflow on pushes to the main branch or pull requests targeting the main branch.
push:
branches:
- main
pull_request:
branches:
- main
env:
# set environment variables for node.js version and github context values.
PYTHON_VERSION: 3.11.10
FILENAME: "${{ github.sha }}-${{ github.run_id }}"
GITHUB_SHA: "${{ github.sha }}"
GITHUB_REF: "${{ github.ref }}"
jobs:
lint:
runs-on: ubuntu-latest
steps:
# step 1: checkout the code from the repository.
- name: Checkout Code
uses: actions/checkout@v4
# step2: setup python.
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
# step 3: install project dependencies.
- name: Install Dependencies
working-directory: ./backend/martinbullman
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# step 4: run pylint to check for code issues.
- name: Run Pylint
working-directory: ./backend
run: |
pylint $(git ls-files '*.py')
test:
# test job depends on the lint job.
needs: lint
runs-on: ubuntu-latest
steps:
# step 1: checkout the code from the repository.
- name: Checkout Code
uses: actions/checkout@v4
# step 2: setup python with specific version.
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
# step 3: install project dependencies.
- name: Install Dependencies
working-directory: ./backend/martinbullman
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# step 4: run tests.
- name: Run Tests
working-directory: ./backend/martinbullman
env:
API_URL: ${{secrets.API_URL}}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
EMAIL_HOST: ${{ secrets.EMAIL_HOST }}
EMAIL_PORT: ${{ secrets.EMAIL_PORT }}
EMAIL_USERNAME: ${{ secrets.EMAIL_USERNAME }}
EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
EMAIL_FROM_ADDRESS: ${{ secrets.EMAIL_FROM_ADDRESS }}
EMAIL_FROM_NAME: ${{secrets.EMAIL_FROM_NAME}}
MAILJET_API_KEY: ${{secrets.MAILJET_API_KEY}}
MAILJET_API_SECRET: ${{secrets.MAILJET_API_SECRET}}
SPOTIFY_CLIENT_ID: ${{ secrets.SPOTIFY_CLIENT_ID }}
SPOTIFY_CLIENT_SECRET: ${{ secrets.SPOTIFY_CLIENT_SECRET }}
SPOTIFY_REFRESH_TOKEN: ${{ secrets.SPOTIFY_REFRESH_TOKEN }}
WEATHER_API_KEY: ${{ secrets.WEATHER_API_KEY }}
run: |
python manage.py test
build:
# build depends on the test job.
needs: test
runs-on: ubuntu-latest
steps:
# step 1: checkout the code from the repository.
- name: Checkout Code
uses: actions/checkout@v4
# step 2: archive the backend files into a tarball.
- name: Archive Artifacts
working-directory: ./backend
run: tar -czf ../${{ env.FILENAME }}.tar.gz ./*
# step 3: upload the archived artifacts for later use in deployment.
- name: Store Artifacts
uses: actions/upload-artifact@v4
with:
name: backend-artifacts
path: ${{ env.FILENAME }}.tar.gz
deploy:
# deployment depends on the test job.
needs: build
runs-on: ubuntu-latest
steps:
# step 1: download the build artifacts from the build job.
- name: Download Artifacts
uses: actions/download-artifact@v4
with:
name: backend-artifacts
# step 2: copy the backend artifacts to the remote server.
- name: Deploy Artifacts
uses: appleboy/scp-action@master
with:
host: ${{ secrets.LIGHTSAIL_HOST }}
username: ${{ secrets.LIGHTSAIL_USER }}
key: ${{ secrets.LIGHTSAIL_SSH_KEY }}
target: /home/ubuntu/
source: ${{ env.FILENAME }}.tar.gz
# step 3: extract the artifacts, run migrations, and restart the service.
- name: Extract and Restart Backend
uses: appleboy/ssh-action@v0.1.8
with:
host: ${{ secrets.LIGHTSAIL_HOST }}
username: ubuntu
key: ${{ secrets.LIGHTSAIL_SSH_KEY }}
script: |
# Navigate to backend directory
mkdir -p "backend_releases/${{ env.FILENAME }}"
tar -xzf ${{ env.FILENAME }}.tar.gz -C "backend_releases/${{ env.FILENAME }}"
cp -r "backend_releases/${{ env.FILENAME }}/"* "MartinBullmanApp/backend/"
rm -r ${{ env.FILENAME }}.tar.gz
# activate Python virtual environment.
source MartinBullmanApp/backend/venv/bin/activate
# install dependencies and run migrations.
pip install -r "MartinBullmanApp/backend/martinbullman/requirements.txt"
python "MartinBullmanApp/backend/martinbullman/manage.py" migrate
# restart backend application.
sudo systemctl restart gunicorn