-
Notifications
You must be signed in to change notification settings - Fork 0
139 lines (118 loc) · 5.77 KB
/
deploy.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
name: Deploy
on:
push:
branches: main
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: tester
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout del repository
uses: actions/checkout@v2
- 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 }}/confing/.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 "DOMAIN=${{ secrets.DOMAIN }}" >> "/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.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/server_name localhost/server_name ${{ secrets.DOMAIN }}/g" /opt/${{ env.REPO_NAME }}/nginx/nginx.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-plugin -y
- name: set lower case owner name
run: |
echo "OWNER_LC=${OWNER,,}" >>${GITHUB_ENV}
env:
OWNER: '${{ github.repository_owner }}'
# - name: LOG IN to container registry
# run: |
# echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# - name: Pull image from GitHub Container Registry
# run: |
# echo ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
# docker pull ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
# docker tag ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest ${{ env.IMAGE_NAME }}
# docker images
- 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
echo ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
echo ${REGISTRY}
echo ${OWNER_LC}
docker pull ghcr.io/leobitto/${{ env.IMAGE_NAME }}:latest
docker tag ghcr.io/leobitto/${{ env.IMAGE_NAME }}:latest ${{ env.IMAGE_NAME }}
docker images
docker tag ${REGISTRY}/${OWNER_LC}/${{ 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
# docker-compose -f /opt/${{ env.REPO_NAME }}/docker-compose-prod.yml exec -T web python manage.py migrate --noinput
# docker-compose -f /opt/${{ env.REPO_NAME }}/docker-compose-prod.yml exec -T web python manage.py collectstatic --noinput --clear