Skip to content

Commit d84c0af

Browse files
author
David Mang
committed
Refactor deployment process and split it into build and deploy
1 parent a2a726f commit d84c0af

File tree

4 files changed

+202
-1
lines changed

4 files changed

+202
-1
lines changed

.github/workflows/build_docker.yml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: Build Docker Image
2+
3+
on:
4+
workflow_call:
5+
outputs:
6+
server_image_tag:
7+
description: "The tag of the server image that was built"
8+
value: ${{ jobs.build.outputs.server_image_tag }}
9+
client_image_tag:
10+
description: "The tag of the client image that was built"
11+
value: ${{ jobs.build.outputs.client_image_tag }}
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
include:
20+
- dockerfile: .docker/client/Dockerfile
21+
image: ghcr.io/ls1intum/thaii/client
22+
context: ./client
23+
path: client
24+
- dockerfile: .docker/server/Dockerfile
25+
image: ghcr.io/ls1intum/thaii/server
26+
context: ./server
27+
path: server
28+
outputs:
29+
server_image_tag: "${{ steps.output-tag-server.outputs.server_image_tag }}"
30+
client_image_tag: "${{ steps.output-tag-client.outputs.client_image_tag }}"
31+
steps:
32+
- name: Checkout
33+
uses: actions/checkout@v4
34+
with:
35+
fetch-depth: 1
36+
37+
- name: Get changed files in the client folder
38+
id: changed-files-client-folder
39+
uses: tj-actions/changed-files@v44
40+
with:
41+
files: client/**
42+
43+
- name: Get changed files in the server folder
44+
id: changed-files-server-folder
45+
uses: tj-actions/changed-files@v44
46+
with:
47+
files: server/**
48+
49+
- name: Log in to the Container registry
50+
if: ${{ (steps.changed-files-client-folder.outputs.any_changed == 'true') || (steps.changed-files-server-folder.outputs.any_changed == 'true') }}
51+
uses: docker/login-action@v3
52+
with:
53+
registry: ghcr.io
54+
username: ${{ github.actor }}
55+
password: ${{ secrets.GITHUB_TOKEN }}
56+
57+
- name: Set up QEMU
58+
if: ${{ (steps.changed-files-client-folder.outputs.any_changed == 'true') || (steps.changed-files-server-folder.outputs.any_changed == 'true') }}
59+
uses: docker/setup-qemu-action@v3
60+
with:
61+
platforms: all
62+
63+
- name: Install Docker Buildx
64+
if: ${{ (steps.changed-files-client-folder.outputs.any_changed == 'true') || (steps.changed-files-server-folder.outputs.any_changed == 'true') }}
65+
id: buildx
66+
uses: docker/setup-buildx-action@v3
67+
68+
- name: Extract metadata (tags, labels) for Docker
69+
id: meta
70+
uses: docker/metadata-action@v5
71+
with:
72+
images: ${{ matrix.image }}
73+
tags: |
74+
type=raw,value=latest,enable={{is_default_branch}}
75+
type=ref,event=branch
76+
type=ref,event=pr
77+
78+
- name: Build and push Docker Image
79+
uses: docker/build-push-action@v5
80+
if: ${{ (steps.changed-files-client-folder.outputs.any_changed == 'true' && matrix.path == 'client') || (steps.changed-files-server-folder.outputs.any_changed == 'true' && matrix.path == 'server') }}
81+
with:
82+
context: ${{ matrix.context }}
83+
file: ${{ matrix.dockerfile }}
84+
platforms: linux/amd64,linux/arm64
85+
push: true
86+
tags: ${{ steps.meta.outputs.tags }}
87+
88+
- id: output-tag-client
89+
run: |
90+
if [[ "${{ matrix.path }}" == "client" ]] && [[ "${{ steps.changed-files-client-folder.outputs.any_changed }}" == "true" ]]; then
91+
echo "client_image_tag=${{ steps.meta.outputs.version }}" >> "$GITHUB_OUTPUT"
92+
elif [[ "${{ matrix.path }}" == "client" ]]; then
93+
echo "client_image_tag=latest" >> "$GITHUB_OUTPUT"
94+
fi
95+
96+
- id: output-tag-server
97+
run: |
98+
if [[ "${{ matrix.path }}" == "server" ]] && [[ "${{ steps.changed-files-server-folder.outputs.any_changed }}" == "true" ]]; then
99+
echo "server_image_tag=${{ steps.meta.outputs.version }}" >> "$GITHUB_OUTPUT"
100+
elif [[ "${{ matrix.path }}" == "server" ]]; then
101+
echo "server_image_tag=latest" >> "$GITHUB_OUTPUT"
102+
fi

.github/workflows/deploy.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Deploy
33
on:
44
push:
55
branches:
6-
- develop # or the branch you want to deploy from
6+
- main # or the branch you want to deploy from
77

88
jobs:
99
build:
@@ -58,6 +58,7 @@ jobs:
5858
scp -o StrictHostKeyChecking=no -r ./letsencrypt ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }}:~/letsencrypt
5959
6060
- name: Set Up Environment Variables
61+
uses: appleboy/[email protected]
6162
with:
6263
host: ${{ secrets.SERVER_DOMAIN }}
6364
username: ${{ secrets.SERVER_USER }}

.github/workflows/deploy_docker.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: Deploy Docker Image
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
environment:
7+
required: true
8+
type: string
9+
server_image_tag:
10+
default: "latest"
11+
type: string
12+
client_image_tag:
13+
default: "latest"
14+
type: string
15+
16+
jobs:
17+
deploy:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: SSH to VM and Execute Docker-Compose Down
21+
uses: appleboy/[email protected]
22+
with:
23+
host: ${{ secrets.SERVER_DOMAIN }}
24+
username: ${{ secrets.SERVER_USER }}
25+
key: ${{ secrets.SSH_KEY }}
26+
script: |
27+
docker compose -f compose.yml --env-file=.env down --remove-orphans --rmi all
28+
29+
- name: Checkout Code
30+
uses: actions/checkout@v3
31+
32+
- name: Copy Files to Server
33+
uses: appleboy/[email protected]
34+
with:
35+
host: ${{ secrets.SERVER_DOMAIN }}
36+
username: ${{ secrets.SERVER_USER }}
37+
key: ${{ secrets.SSH_KEY }}
38+
script: |
39+
scp -o StrictHostKeyChecking=no ./compose.yml ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }}:~/compose.yml
40+
scp -o StrictHostKeyChecking=no -r ./letsencrypt ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }}:~/letsencrypt
41+
42+
- name: Set Up Environment Variables
43+
uses: appleboy/[email protected]
44+
with:
45+
host: ${{ secrets.SERVER_DOMAIN }}
46+
username: ${{ secrets.SERVER_USER }}
47+
key: ${{ secrets.SSH_KEY }}
48+
script: |
49+
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} << 'EOF'
50+
touch .env
51+
echo "OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> .env
52+
echo "DEBUG=${{ secrets.DEBUG }}" >> .env
53+
echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env
54+
echo "POSTGRES_DB=${{ secrets.POSTGRES_DB }}" >> .env
55+
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> .env
56+
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> .env
57+
echo "POSTGRES_HOST=${{ secrets.POSTGRES_HOST }}" >> .env
58+
echo "EMAIL_USE_TLS=${{ secrets.EMAIL_USE_TLS }}" >> .env
59+
echo "EMAIL_HOST=${{ secrets.EMAIL_HOST }}" >> .env
60+
echo "EMAIL_HOST_USER=${{ secrets.EMAIL_HOST_USER }}" >> .env
61+
echo "EMAIL_HOST_PASSWORD=${{ secrets.EMAIL_HOST_PASSWORD }}" >> .env
62+
echo "DEFAULT_FROM_EMAIL=${{ secrets.DEFAULT_FROM_EMAIL }}" >> .env
63+
echo "EMAIL_PORT=${{ secrets.EMAIL_PORT }}" >> .env
64+
echo "DJANGO_SUPERUSER_USERNAME=${{ secrets.DJANGO_SUPERUSER_USERNAME }}" >> .env
65+
echo "DJANGO_SUPERUSER_PASSWORD=${{ secrets.DJANGO_SUPERUSER_PASSWORD }}" >> .env
66+
echo "DJANGO_SUPERUSER_EMAIL=${{ secrets.DJANGO_SUPERUSER_EMAIL }}" >> .env
67+
EOF
68+
69+
- name: SSH to VM and Execute Docker-Compose Up
70+
uses: appleboy/[email protected]
71+
with:
72+
host: ${{ secrets.SERVER_DOMAIN }}
73+
username: ${{ secrets.SERVER_USER }}
74+
key: ${{ secrets.SSH_KEY }}
75+
script: |
76+
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} "mkdir -p ~/"
77+
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} "touch ~/letsencrypt/acme.json && chmod 600 ~/letsencrypt/acme.json"
78+
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} "docker login ghcr.io -u ${{ github.actor }} --password-stdin <<< ${{ secrets.GITHUB_TOKEN }}"
79+
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} "docker compose pull && docker compose up -d && docker compose logs"

.github/workflows/prod.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Build and Deploy to Prod
2+
3+
on:
4+
push:
5+
branches: [develop]
6+
7+
jobs:
8+
build-prod-container:
9+
uses: ./.github/workflows/build_docker.yml
10+
secrets: inherit
11+
deploy-prod-container:
12+
needs: build-prod-container
13+
uses: ./.github/workflows/deploy_docker.yml
14+
secrets: inherit
15+
with:
16+
environment: Production
17+
server_image_tag: "latest"
18+
client_image_tag: "latest"
19+

0 commit comments

Comments
 (0)