forked from immich-app/immich
-
Notifications
You must be signed in to change notification settings - Fork 0
298 lines (281 loc) · 11.1 KB
/
docker.yaml
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
name: Docker Image CI
# To run this locally use act (https://github.com/nektos/act)
# To use act:
# Create a .secrets file and set all the needed variables
# act -j <job>
# act <event_name>
# act -e .github/act/<event>.json // For additinal event params
# act -P self-hosted=ghcr.io/catthehacker/ubuntu:act-latest // To make it wokr with self hosted
on:
push:
branches: [main]
pull_request:
types: [opened, closed, reopened, synchronize]
release:
types: [released, prereleased]
env:
REGISTRY: registry.esek.se
DEFAULT_BRANCH: main
env: production
ONLY_PROD: true
jobs:
# This can only contain env vars and not secrets since actions is a fussy about it
env-vars:
runs-on: self-hosted
container: ghcr.io/catthehacker/ubuntu:act-latest
steps:
- name: set variables
id: vars
run: |
slugify () {
iconv -t ascii//TRANSLIT \
| tr -d "'" \
| sed -E 's/[^a-zA-Z0-9]+/-/g' \
| sed -E 's/^-+|-+$//g' \
| tr "[:upper:]" "[:lower:]"
}
echo ${{ github.event }}
export BRANCH=
if [ -z $GITHUB_HEAD_REF ]; then
BRANCH=$(basename $GITHUB_REF)
else
BRANCH=$GITHUB_HEAD_REF
fi
echo "Running on $BRANCH"
export TAG=ephermal
if [ "$BRANCH" = "${{ env.DEFAULT_BRANCH }}" ]; then
TAG=latest
fi
if ${{ github.ref_type == 'tag' }}; then
TAG=$BRANCH
BRANCH=${{ env.DEFAULT_BRANCH }}
fi
TAG=$(echo "$TAG" | slugify)
export IMAGE_NAME="${{ env.REGISTRY }}/${{ github.repository }}:$TAG"
echo "::echo::on"
echo "::set-output name=image_name::$IMAGE_NAME"
echo "::set-output name=tag::$TAG"
echo "::set-output name=basename::$(basename ${{ github.repository }} | slugify)"
echo "::set-output name=branch::$( echo $BRANCH | slugify)"
echo "::echo::off"
- name: select environment
id: env
run: |
export ENV=testing
export DEPLOY_ENV=testing
export PROJ_BASE=${{ steps.vars.outputs.basename }}
export TAG=${{ steps.vars.outputs.tag }}
export BRANCH=${{ steps.vars.outputs.branch }}
export PROJECT_NAME=$PROJ_BASE-testing
if ${{ (github.event_name == 'release' && github.event.action == 'released') || env.ONLY_PROD == 'true' }}; then
ENV=production
DEPLOY_ENV=$ENV
PROJECT_NAME=$PROJ_BASE-production
fi
if ${{ github.event_name == 'release' && github.event.action == 'prereleased' }}; then
ENV=staging
DEPLOY_ENV=$ENV
PROJECT_NAME=$PROJ_BASE-staging
fi
if ${{ github.event_name == 'pull_request' }}; then
ENV=review
DEPLOY_ENV=$BRANCH-review
PROJECT_NAME=$PROJ_BASE-$BRANCH
fi
echo "::echo::on"
echo "::set-output name=env::$ENV"
echo "::set-output name=project_name::$PROJECT_NAME"
echo "::set-output name=deploy_env::$DEPLOY_ENV"
echo "::echo::off"
outputs:
image_name: ${{ steps.vars.outputs.image_name }}
tag: ${{ steps.vars.outputs.tag }}
basename: ${{ steps.vars.outputs.basename }}
branch: ${{ steps.vars.outputs.branch }}
env: ${{ steps.env.outputs.env }}
project_name: ${{ steps.env.outputs.project_name }}
deploy_env: ${{ steps.env.outputs.deploy_env }}
build:
runs-on: self-hosted
needs: env-vars
if: ${{ !(github.event_name == 'pull_request' && github.event.action == 'closed') }}
container:
image: ghcr.io/catthehacker/ubuntu:act-latest
steps:
- name: import secrets
id: s
uses: hashicorp/[email protected]
with:
url: ${{ secrets.VAULT_URL }}
token: ${{ secrets.VAULT_TOKEN }}
method: token
secrets: |
kv/data/common registry_username;
kv/data/common registry_password;
- name: setup buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: checkout repo
uses: actions/checkout@v3
- name: login to registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ steps.s.outputs.registry_username }}
password: ${{ steps.s.outputs.registry_password }}
- name: build and push server image
uses: docker/build-push-action@v3
with:
context: .
file: ./server/Dockerfile
push: true
tags: ${{ needs.env-vars.outputs.image_name }}-server
# - name: build and push web image
# uses: docker/build-push-action@v3
# with:
# file: ./web/Dockerfile
# context: .
# push: true
# tags: ${{ needs.env-vars.outputs.image_name }}-web
deploy:
# To run this locally using act set act to false in event.json
if: ${{ !github.event.act || !(github.event_name == 'pull_request' && github.event.action == 'closed') }}
runs-on: self-hosted
needs:
- env-vars
- build
steps:
- name: import secrets
id: s
uses: hashicorp/[email protected]
with:
url: ${{ secrets.VAULT_URL }}
token: ${{ secrets.VAULT_TOKEN }}
method: token
exportEnv: true
secrets: |
kv/data/common registry_username;
kv/data/common registry_password;
kv/data/common docker_ca;
kv/data/common docker_cert;
kv/data/common docker_key;
kv/data/common docker_host;
kv/data/${{ needs.env-vars.outputs.basename }}/${{ needs.env-vars.outputs.env }} DEPLOY_URL;
kv/data/${{ needs.env-vars.outputs.basename }}/${{ needs.env-vars.outputs.env }} DB_DATA_LOCATION;
kv/data/${{ needs.env-vars.outputs.basename }}/${{ needs.env-vars.outputs.env }} DB_DATABASE_NAME;
kv/data/${{ needs.env-vars.outputs.basename }}/${{ needs.env-vars.outputs.env }} DB_PASSWORD;
kv/data/${{ needs.env-vars.outputs.basename }}/${{ needs.env-vars.outputs.env }} DB_USERNAME;
kv/data/${{ needs.env-vars.outputs.basename }}/${{ needs.env-vars.outputs.env }} IMMICH_VERSION;
kv/data/${{ needs.env-vars.outputs.basename }}/${{ needs.env-vars.outputs.env }} UPLOAD_LOCATION;
- name: set variables
run: |
echo "IMAGE_NAME=${{ needs.env-vars.outputs.image_name }}" >> $GITHUB_ENV
echo "TAG=${{ needs.env-vars.outputs.tag }}" >> $GITHUB_ENV
echo "DEPLOY_ENV=${{ needs.env-vars.outputs.deploy_env }}" >> $GITHUB_ENV
echo "ENV=${{ needs.env-vars.outputs.env }}" >> $GITHUB_ENV
echo "PROJECT_NAME=${{ needs.env-vars.outputs.project_name }}" >> $GITHUB_ENV
echo "COMPOSE_PROJECT_NAME=${{ needs.env-vars.outputs.project_name }}" >> $GITHUB_ENV
if [ ${{ needs.env-vars.outputs.env }} = 'review' ]; then
echo "DEPLOY_URL=${{ needs.env-vars.outputs.branch }}-${{ needs.env-vars.outputs.basename }}.review.esek.se" >> $GITHUB_ENV
fi
- name: checkout repo
uses: actions/checkout@v3
- name: start deployment
uses: bobheadxi/deployments@v1
id: deployment
if: ${{ !env.ACT && github.event_name != 'pull_request' && needs.env-vars.outputs.env != 'review' }}
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
override: true
env: ${{ env.DEPLOY_ENV }}
- name: login to host
uses: blennster/setup-docker-remote-tls@v4
with:
tls_key: ${{ steps.s.outputs.docker_key }}
tls_ca: ${{ steps.s.outputs.docker_ca }}
tls_cert: ${{ steps.s.outputs.docker_cert }}
tcp_host: ${{ steps.s.outputs.docker_host }}:2376
- name: check connection
run: |
docker info
docker compose version
- name: login to registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ steps.s.outputs.registry_username }}
password: ${{ steps.s.outputs.registry_password }}
- name: pull image(s)
run: echo $IMAGE_NAME; docker compose -f docker-compose.ci.yml pull
- name: Stop old container
run: |
docker compose -f docker-compose.ci.yml -p $PROJECT_NAME down
- name: deploy to host
shell: bash
run: |
if [[ "$ENV" = "production" || "$ENV" = "staging" ]]; then
docker compose -f docker-compose.ci.yml -f docker-compose.prod.yml -p $PROJECT_NAME up -d --force-recreate
fi
if [[ "$ENV" = "review" || "$ENV" = "testing" ]]; then
docker compose -f docker-compose.ci.yml -p $PROJECT_NAME up -d --force-recreate
fi
- name: prune system
run: docker system prune -f 2> /dev/null || true
- name: update deployment status
uses: bobheadxi/deployments@v1
if: ${{ always() && !env.ACT && github.event_name != 'pull_request' && needs.env-vars.outputs.env != 'review' }}
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
env: ${{ steps.deployment.outputs.env }}
env_url: https://${{ env.DEPLOY_URL }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
- if: ${{ github.event.pull_request && github.event.action == 'opened' && !env.ACT }}
name: comment deployment url to pr
uses: mshick/add-pr-comment@v1
with:
message: I have deployed this PR to [${{ env.DEPLOY_URL }}](https://${{ env.DEPLOY_URL }}) 🚀
repo-token: ${{ secrets.GITHUB_TOKEN }}
teardown:
runs-on: self-hosted
if: ${{ github.event_name == 'pull_request' && github.event.action == 'closed' }}
needs: env-vars
steps:
- name: import secrets
id: s
uses: hashicorp/[email protected]
with:
url: ${{ secrets.VAULT_URL }}
token: ${{ secrets.VAULT_TOKEN }}
method: token
exportEnv: true
secrets: |
kv/data/common docker_ca;
kv/data/common docker_cert;
kv/data/common docker_key;
kv/data/common docker_host;
- name: login to host
uses: blennster/setup-docker-remote-tls@v4
with:
tls_key: ${{ steps.s.outputs.docker_key }}
tls_ca: ${{ steps.s.outputs.docker_ca }}
tls_cert: ${{ steps.s.outputs.docker_cert }}
tcp_host: ${{ steps.s.outputs.docker_host }}:2376
- name: check connection
run: |
docker info
docker compose version
- name: checkout repo
uses: actions/checkout@v3
- name: stop container
run: |
docker compose -f docker-compose.ci.yml -p ${{ needs.env-vars.outputs.project_name }} down 2> /dev/null || true
- if: ${{ !env.ACT }}
name: comment deployment url to pr
uses: mshick/add-pr-comment@v1
with:
message: I have removed this deploy now 😇
repo-token: ${{ secrets.GITHUB_TOKEN }}