-
Notifications
You must be signed in to change notification settings - Fork 0
192 lines (160 loc) · 7.39 KB
/
CI-CD.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
name: CI/CD
on:
push:
branches: main
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: djangoforge
jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v3
with:
submodules: recursive
token: ${{ secrets.GITHUB_TOKEN }}
- name: Create config directory and .env file
run: |
mkdir -p config
echo "DEBUG=False" >> config/.env
echo "SECRET_KEY=foo" >> config/.env
echo "DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] *" >> config/.env
echo "SQL_ENGINE=django.db.backends.postgresql" >> config/.env
echo "POSTGRES_DB=${{ secrets.POSTGRES_DB }}" >> config/.env
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> config/.env
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> config/.env
echo "SQL_HOST=db" >> config/.env
echo "SQL_PORT=5432" >> config/.env
echo "DATABASE=postgres" >> config/.env
echo "DOMAIN=${{ secrets.DOMAIN }}" >> config/.env
working-directory: .
- name: Display content of .env file
run: cat ./config/.env
- name: Update Nginx configuration file
run: |
sed -i "s/server_name localhost/server_name ${{ secrets.DOMAIN }}/g" ./nginx/nginx.conf
working-directory: .
- name: Build Docker image using Docker Compose
run: docker-compose -f docker-compose.dev.yml build
- name: verify that all the container have been built
run: docker images
- name: LOG IN to container registry
run: echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: set lower case owner name
run: |
echo "OWNER_LC=${OWNER,,}" >>${GITHUB_ENV}
env:
OWNER: '${{ github.repository_owner }}'
- name: Tag image
run: |
echo ${REGISTRY}/${OWNER_LC}/${IMAGE_NAME}:latest
docker tag webapp_django ${REGISTRY}/${OWNER_LC}/${IMAGE_NAME}:latest
- name: List Docker images
run: docker images
- name: Push image to GitHub Container Registry
run: |
echo ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
docker push ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
deploy:
runs-on: ubuntu-latest
needs: build_and_push
steps:
- name: Checkout del repository
uses: actions/checkout@v3
- name: Set ENV variables
run: |
echo "REPO_NAME=${GITHUB_REPOSITORY#$GITHUB_REPOSITORY_OWNER/}" >> $GITHUB_ENV
echo $GITHUB_ENV
- name: Create project directory and conf file
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
mkdir -p "/opt/${{ env.REPO_NAME }}"
mkdir -p "/opt/${{ env.REPO_NAME }}/config"
rm /opt/${{ env.REPO_NAME }}/config/.env
echo "DEBUG=False" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "SECRET_KEY=foo" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] *" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "SQL_ENGINE=django.db.backends.postgresql" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "POSTGRES_DB=${{ secrets.POSTGRES_DB }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "SQL_HOST=db" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "SQL_PORT=5432" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "DATABASE=postgres" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "EMAIL=${{ secrets.EMAIL }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "DOMAIN=${{ secrets.DOMAIN }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
cat "/opt/${{ env.REPO_NAME }}/config/.env"
- name: SCP Docker Compose file and Nginx conf
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
source: "./docker-compose.prod.yml"
target: "/opt/${{ env.REPO_NAME }}/"
- name: SCP Nginx conf file
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
source: "./nginx/nginx.prod.conf"
target: "/opt/${{ env.REPO_NAME }}/"
- name: Update Nginx configuration file
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
sed -i "s/yourdomain.com/${{ secrets.DOMAIN }}/g" /opt/${{ env.REPO_NAME }}/nginx/nginx.prod.conf
- name: Upgrade server and install Docker
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
sudo apt update -y
sudo apt upgrade -y
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose docker-compose-plugin -y
- name: set lower case owner name
run: |
echo "OWNER_LC=${OWNER,,}" >>${GITHUB_ENV}
env:
OWNER: '${{ github.repository_owner }}'
- name: Stop And Start containers
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
docker pull ghcr.io/leobitto/${{ env.IMAGE_NAME }}:latest
docker tag ghcr.io/leobitto/${{ env.IMAGE_NAME }}:latest ${{ env.IMAGE_NAME }}
docker-compose -f /opt/${{ env.REPO_NAME }}/docker-compose.prod.yml down
docker-compose -f /opt/${{ env.REPO_NAME }}/docker-compose.prod.yml up -d --force-recreate
echo "wait for it.."
sleep 5
echo "5 seconds has passed"
docker exec web python manage.py migrate
docker exec web python manage.py collectstatic --noinput --clear