-
-
Notifications
You must be signed in to change notification settings - Fork 0
156 lines (139 loc) · 5.68 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
name: Container Image Deployment CI
on:
push:
# Initially only doing production deploy
# branches:
# - main
# - deploy-*
tags:
- v*.*.*
jobs:
deploy:
runs-on: ubuntu-latest
strategy:
matrix:
env:
- ${{ startsWith(github.ref, 'refs/tags/v') && 'off-auth-org' || 'off-auth-net' }}
environment: ${{ matrix.env }}
concurrency: ${{ matrix.env }}
steps:
- name: Set various variable for production deployment
if: matrix.env == 'off-auth-org'
run: |
echo "SSH_PROXY_HOST=off1.openfoodfacts.org" >> $GITHUB_ENV
echo "SSH_USERNAME=off" >> $GITHUB_ENV
echo "SSH_PROXY_USERNAME=off" >> $GITHUB_ENV
echo "SSH_HOST=10.1.0.104" >> $GITHUB_ENV
echo "KEYCLOAK_BASE_URL=https://auth.openfoodfacts.org" >> $GITHUB_ENV
echo "KC_DB_URL_HOST=10.1.0.120" >> $GITHUB_ENV
echo "REDIS_URL=10.1.0.122:6379" >> $GITHUB_ENV
echo "SMTP_SERVER=pmg.openfoodfacts.org" >> $GITHUB_ENV
echo "CLIENTS=OFF,https://world.openfoodfacts.org NEW_OPFF,https://world.new.openpetfoordfacts.org" >> $GITHUB_ENV
- name: Wait for container build workflow
uses: tomchv/[email protected]
id: wait-build
with:
token: ${{ secrets.GITHUB_TOKEN }}
checkName: build
ref: ${{ github.event.pull_request.head.sha || github.sha }}
intervalSeconds: 10
timeoutSeconds: 600 # 10m
- name: Do something if build fail
if: steps.wait-build.outputs.conclusion == 'failure'
run: echo fail && false # fail if build fail
- name: Do something if build timeout
if: steps.wait-build.outputs.conclusion == 'timed_out'
run: echo Timeout && false # fail if build time out
- 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_PROXY_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
# Clone Git repository if not already there
[ ! -d '${{ matrix.env}}' ] && git clone --depth 1 https://github.com/${{ github.repository }} ${{ matrix.env }} --no-single-branch 2>&1
# Go to repository directory
cd ${{ matrix.env}}
# Fetch newest commits (in case it wasn't freshly cloned)
git fetch --depth 1
# Checkout current commit SHA
git checkout -qf ${{ github.sha }}
- name: Set environment variables
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_PROXY_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
# Go to repository directory
cd ${{ matrix.env}}
# Set environment variables
echo "KC_BOOTSTRAP_ADMIN_USERNAME=root" > .env
echo "KC_BOOTSTRAP_ADMIN_PASSWORD=${{ secrets.KC_BOOTSTRAP_ADMIN_PASSWORD }}" >> .env
echo "KEYCLOAK_BASE_URL=${{ env.KEYCLOAK_BASE_URL }}" >> .env
echo "KEYCLOAK_REALM_NAME=open-products-facts" >> .env
echo "COMPOSE_PROJECT_NAME=openfoodfacts-auth" >> .env
echo "COMPOSE_PATH_SEPARATOR=," >> .env
echo "COMPOSE_FILE=docker-compose.yml,docker/prod.yml" >> .env
echo "PG_BOOTSTRAP_USERNAME=bootstrap" >> .env
echo "PG_BOOTSTRAP_PASSWORD=${{ secrets.PG_BOOTSTRAP_PASSWORD }}" >> .env
echo "KC_DB_URL_HOST=${{ env.KC_DB_URL_HOST }}" >> .env
echo "KC_DB_USERNAME=keycloak" >> .env
echo "KC_DB_PASSWORD=${{ secrets.KC_DB_PASSWORD }}" >> .env
echo "CLIENTS=${{ env.CLIENTS }}" >> .env
echo "REDIS_URL=${{ env.REDIS_URL }}" >> .env
echo "SMTP_SERVER=${{ env.SMTP_SERVER }}" >> .env
echo "KEYCLOAK_HTTP_PORT=5600" >> .env
echo "KEYCLOAK_MANAGEMENT_PORT=5602" >> .env
echo "KEYCLOAK_STARTUP=prod" >> .env
echo "KEYCLOAK_TAG=sha-${{ github.sha }}" >> .env
echo "RESTART_POLICY=always" >> .env
- name: Create database and user
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_PROXY_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
cd ${{ matrix.env }}
make create_user_prod
- name: Start services
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_PROXY_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
cd ${{ matrix.env }}
docker compose up --wait --wait-timeout 300
- name: Cleanup obsolete Docker objects
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_PROXY_USERNAME }}
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: false
script: |
cd ${{ matrix.env }}
make prune