-
Notifications
You must be signed in to change notification settings - Fork 0
144 lines (125 loc) · 3.82 KB
/
cicd.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
name: Django CI/CD workflow
on:
push:
branches:
- main
- develop
paths-ignore:
- '.gitignore'
- '.dockerignore'
- 'README.md'
pull_request:
branches:
- main
- develop
jobs:
test:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.9]
services:
mysql:
image: mysql:5.7
env:
MYSQL_DATABASE: django-test
MYSQL_USER: user
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
options:
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
steps:
- name: checkout
uses: actions/checkout@v2
- name: set up python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: run migrations
run: |
export DEBUG=1
export SECRET_KEY=test-secret-key
export ALLOWED_HOSTS=*
python manage.py migrate
env:
DB_ENGINE: django.db.backends.mysql
DB_NAME: django-test
DB_USER: root
DB_PASSWORD: root
DB_HOST: 127.0.0.1
DB_PORT: 3306
- name: run tests
run: |
export DEBUG=1
export SECRET_KEY=test-secret-key
export ALLOWED_HOSTS=*
coverage run manage.py test
coverage report
env:
DB_ENGINE: django.db.backends.mysql
DB_NAME: django-test
DB_USER: root
DB_PASSWORD: root
DB_HOST: 127.0.0.1
DB_PORT: 3306
build-deploy:
runs-on: ubuntu-latest
needs: [test]
if: ${{ github.event_name == 'push' }}
steps:
- name: checkout
uses: actions/checkout@v2
- name: create env file
run: |
touch .env
echo "${{ secrets.ENV }}" >> .env
- name: get github actions runner's ip
id: ip
uses: haythem/[email protected]
- name: add github actions runner's ip to aws ec2 security group
run: |
aws ec2 authorize-security-group-ingress --group-name ${{ secrets.SG_NAME }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
- name: create remote directory
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
script: mkdir -p /home/ubuntu/srv/deploy
- name: copy source via ssh key
uses: burnett01/[email protected]
with:
switches: -avzr --delete
remote_path: /home/ubuntu/srv/deploy/
remote_host: ${{ secrets.HOST }}
remote_user: ${{ secrets.USERNAME }}
remote_key: ${{ secrets.KEY }}
- name: executing remote ssh commands
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
script: |
sh /home/ubuntu/srv/deploy/config/scripts/deploy.sh
- name: remove github actions runner's ip from aws ec2 security group
run: |
aws ec2 revoke-security-group-ingress --group-name ${{ secrets.SG_NAME }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
if: ${{ always() }}