-
-
Notifications
You must be signed in to change notification settings - Fork 5
197 lines (182 loc) · 6.83 KB
/
container-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
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
193
194
195
196
197
name: Docker Compose Deployment CI
on:
push:
branches:
- main
- deploy-*
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: checkout repo on current branch
uses: actions/checkout@v3
with:
# there seem to be no choice but fetch-depth 0 to be able to checkout main
fetch-depth: 0
- name: Set env variables
run: |
# direct container access
# deploy target
echo "SSH_HOST=10.1.0.203" >> $GITHUB_ENV
echo "SSH_PROXY_HOST=ovh1.openfoodfacts.org" >> $GITHUB_ENV
echo "SSH_USERNAME=off" >> $GITHUB_ENV
# did only config change ? (in this case we only need a restart)
declare current_branch=$(git rev-parse --abbrev-ref HEAD)
echo $current_branch
declare before=${{ github.event.before }}
if [[ $before =~ ^00000 ]]
then
# use main as reference
git checkout -b main --track origin/main
git checkout $current_branch
declare before=main
fi
echo before is: $before
echo head is: ${{ github.event.head }}
if ( git diff --name-only $before ${{ github.event.after }} |grep -v configs/ )
then
echo "RESTART_ONLY=no" >> $GITHUB_ENV
else
echo "RESTART_ONLY=yes" >> $GITHUB_ENV
fi
echo "Changed file:"
git diff --name-only $before ${{ github.event.after }}
- name: Checkout git repository
uses: appleboy/ssh-action@master
with:
host: ${{ env.SSH_HOST }}
username: ${{ env.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
proxy_host: ${{ env.SSH_PROXY_HOST }}
proxy_username: ${{ env.SSH_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
echo "RESTART_ONLY is ${{ env.RESTART_ONLY }}"
# Clone Git repository if not already there
[ ! -d 'monitoring' ] && git clone --depth 1 https://github.com/${{ github.repository }} monitoring --no-single-branch 2>&1
# Go to repository directory
cd monitoring/
# Fetch newest commits (in case it wasn't freshly cloned)
# and Checkout current commit SHA
git fetch --depth 1 && \
git checkout -qf ${{ github.sha }}
- name: Set environment variables in .env
uses: appleboy/ssh-action@master
with:
host: ${{ env.SSH_HOST }}
username: ${{ env.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
proxy_host: ${{ env.SSH_PROXY_HOST }}
proxy_username: ${{ env.SSH_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
# Go to repository directory
cd monitoring/
# Set Docker Compose variables
echo "DOCKER_CLIENT_TIMEOUT=120" > .env
echo "COMPOSE_HTTP_TIMEOUT=120" >> .env
echo "COMPOSE_PROJECT_NAME=monitoring" >> .env
echo "COMPOSE_PATH_SEPARATOR=;" >> .env
echo "COMPOSE_FILE=docker-compose.yml;docker/prod.yml" >> .env
# Grafana
echo "GF_SERVER_ROUTER_LOGGING=true" >> .env
echo "GF_SECURITY_ADMIN_USER=off" >> .env
echo "GF_SECURITY_ADMIN_PASSWORD=${{ secrets.GF_SECURITY_ADMIN_PASSWORD }}" >> .env
echo "GF_AUTH_GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_OAUTH2_CLIENT_ID }}" >> .env
echo "GF_AUTH_GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_OAUTH2_CLIENT_SECRET }}" >> .env
# Alertmanager
echo "SLACK_WEBHOOK_URL_INFRASTRUCTURE_ALERTS_0=${{ secrets.SLACK_WEBHOOK_URL_INFRASTRUCTURE_ALERTS_0 }}" >> .env
# InfluxDB
echo "INFLUXDB_ADMIN_USER=off" >> .env
echo "INFLUXDB_ADMIN_PASSWORD=${{ secrets.INFLUXDB_ADMIN_PASSWORD }}" >> .env
echo "INFLUXDB_DB=db0" >> .env
# set secrets
mkdir -p configs/prometheus/secrets
echo "${{ secrets.FREE_EXPORTERS_PASSWORD }}" > configs/prometheus/secrets/free-exporters.txt
- name: Run envsubst on Prometheus configs
uses: appleboy/ssh-action@master
with:
host: ${{ env.SSH_HOST }}
username: ${{ env.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
proxy_host: ${{ env.SSH_PROXY_HOST }}
proxy_username: ${{ env.SSH_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
cd monitoring/
make replace_env
- name: Create external Docker volumes
uses: appleboy/ssh-action@master
if: env.RESTART_ONLY == 'no'
with:
host: ${{ env.SSH_HOST }}
username: ${{ env.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
proxy_host: ${{ env.SSH_PROXY_HOST }}
proxy_username: ${{ env.SSH_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
cd monitoring/
make create_external_volumes
- name: Recreate & start services
uses: appleboy/ssh-action@master
if: env.RESTART_ONLY == 'no'
with:
host: ${{ env.SSH_HOST }}
username: ${{ env.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
proxy_host: ${{ env.SSH_PROXY_HOST }}
proxy_username: ${{ env.SSH_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
cd monitoring/
make create_backups_dir && \
make down && \
make up
- name: re-start services only
uses: appleboy/ssh-action@master
if: env.RESTART_ONLY == 'yes'
with:
host: ${{ env.SSH_HOST }}
username: ${{ env.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
proxy_host: ${{ env.SSH_PROXY_HOST }}
proxy_username: ${{ env.SSH_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
cd monitoring/
make restart
- name: Check services are up
uses: appleboy/ssh-action@master
if: ${{ always() }}
with:
host: ${{ env.SSH_HOST }}
username: ${{ env.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
proxy_host: ${{ env.SSH_PROXY_HOST }}
proxy_username: ${{ env.SSH_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
cd monitoring/
make livecheck
- name: Cleanup obsolete Docker objects
uses: appleboy/ssh-action@master
if: env.RESTART_ONLY == 'no'
with:
host: ${{ env.SSH_HOST }}
username: ${{ env.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
proxy_host: ${{ env.SSH_PROXY_HOST }}
proxy_username: ${{ env.SSH_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
cd monitoring/
make prune