Skip to content

Commit 4e380a8

Browse files
committed
refactor: update CI/CD workflows and Docker configurations for improved image building and deployment
1 parent 6b314e0 commit 4e380a8

5 files changed

Lines changed: 124 additions & 283 deletions

File tree

‎.github/workflows/backend-ci.yml‎

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 106 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,112 @@
1-
name: Build and Push Docker Images
1+
# Builds Docker images to GitHub Container Registry (GHCR) when a git tag is pushed
2+
# Triggered only on: git tag -a v1.0.0 && git push origin v1.0.0
3+
# Images appear at: ghcr.io/vuducle/nachweise-backend:1.0.0
4+
5+
name: Build and Push to GHCR
26

37
on:
4-
push:
5-
branches:
6-
- main
7-
- develop
8-
tags:
9-
- "v*"
10-
pull_request:
11-
branches:
12-
- main
13-
- develop
8+
push:
9+
tags:
10+
- "*" # Triggered on any git tag
1411

1512
env:
16-
REGISTRY: ghcr.io
17-
BACKEND_IMAGE_NAME: ${{ github.actor }}/nachweise-backend
18-
FRONTEND_IMAGE_NAME: ${{ github.actor }}/nachweise-frontend
13+
REGISTRY: ghcr.io
14+
BACKEND_IMAGE: ${{ github.actor }}/nachweise-backend
15+
FRONTEND_IMAGE: ${{ github.actor }}/nachweise-frontend
1916

2017
jobs:
21-
build-backend:
22-
runs-on: ubuntu-latest
23-
permissions:
24-
contents: read
25-
packages: write
26-
id-token: write
27-
28-
steps:
29-
- name: Checkout repository
30-
uses: actions/checkout@v4
31-
32-
- name: Set up Docker Buildx
33-
uses: docker/setup-buildx-action@v3
34-
35-
- name: Log in to Container Registry
36-
if: github.event_name != 'pull_request'
37-
uses: docker/login-action@v3
38-
with:
39-
registry: ${{ env.REGISTRY }}
40-
username: ${{ github.actor }}
41-
password: ${{ secrets.GHCR_TOKEN }}
42-
43-
- name: Extract metadata (tags, labels)
44-
id: meta-backend
45-
uses: docker/metadata-action@v5
46-
with:
47-
images: ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE_NAME }}
48-
tags: |
49-
type=ref,event=branch
50-
type=semver,pattern={{version}}
51-
type=semver,pattern={{major}}.{{minor}}
52-
type=sha,prefix={{branch}}-
53-
type=raw,value=latest,enable={{is_default_branch}}
54-
55-
- name: Build and push Backend Docker image
56-
uses: docker/build-push-action@v5
57-
with:
58-
context: ./backend
59-
push: ${{ github.event_name != 'pull_request' }}
60-
tags: ${{ steps.meta-backend.outputs.tags }}
61-
labels: ${{ steps.meta-backend.outputs.labels }}
62-
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE_NAME }}:buildcache
63-
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE_NAME }}:buildcache,mode=max
64-
65-
build-frontend:
66-
runs-on: ubuntu-latest
67-
permissions:
68-
contents: read
69-
packages: write
70-
id-token: write
71-
72-
steps:
73-
- name: Checkout repository
74-
uses: actions/checkout@v4
75-
76-
- name: Set up Docker Buildx
77-
uses: docker/setup-buildx-action@v3
78-
79-
- name: Log in to Container Registry
80-
if: github.event_name != 'pull_request'
81-
uses: docker/login-action@v3
82-
with:
83-
registry: ${{ env.REGISTRY }}
84-
username: ${{ github.actor }}
85-
password: ${{ secrets.GHCR_TOKEN }}
86-
87-
- name: Extract metadata (tags, labels)
88-
id: meta-frontend
89-
uses: docker/metadata-action@v5
90-
with:
91-
images: ${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE_NAME }}
92-
tags: |
93-
type=ref,event=branch
94-
type=semver,pattern={{version}}
95-
type=semver,pattern={{major}}.{{minor}}
96-
type=sha,prefix={{branch}}-
97-
type=raw,value=latest,enable={{is_default_branch}}
98-
99-
- name: Build and push Frontend Docker image
100-
uses: docker/build-push-action@v5
101-
with:
102-
context: ./client-nachweise
103-
push: ${{ github.event_name != 'pull_request' }}
104-
tags: ${{ steps.meta-frontend.outputs.tags }}
105-
labels: ${{ steps.meta-frontend.outputs.labels }}
106-
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE_NAME }}:buildcache
107-
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE_NAME }}:buildcache,mode=max
108-
109-
deploy:
110-
needs: [build-backend, build-frontend]
111-
runs-on: ubuntu-latest
112-
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
113-
114-
steps:
115-
- name: Checkout repository
116-
uses: actions/checkout@v4
117-
118-
- name: Deploy to VPS
119-
uses: appleboy/ssh-action@master
120-
with:
121-
host: ${{ secrets.VPS_HOST }}
122-
username: ${{ secrets.VPS_USER }}
123-
key: ${{ secrets.VPS_SSH_KEY }}
124-
port: ${{ secrets.VPS_PORT || 22 }}
125-
script: |
126-
cd ${{ secrets.VPS_DEPLOY_PATH }}
127-
git pull origin main
128-
docker-compose pull
129-
docker-compose up -d
130-
docker-compose exec -T backend sh -c "java -jar /app/app.jar" &
18+
build-backend:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
contents: read
22+
packages: write
23+
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v4
27+
28+
- name: Set up Docker Buildx
29+
uses: docker/setup-buildx-action@v3
30+
31+
- name: Login to GHCR
32+
uses: docker/login-action@v3
33+
with:
34+
registry: ${{ env.REGISTRY }}
35+
username: ${{ github.actor }}
36+
password: ${{ secrets.GHCR_TOKEN }}
37+
38+
- name: Extract tag version
39+
id: meta
40+
uses: docker/metadata-action@v5
41+
with:
42+
images: ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE }}
43+
tags: |
44+
type=semver,pattern={{version}}
45+
type=semver,pattern={{major}}.{{minor}}
46+
type=raw,value=latest
47+
48+
- name: Build and push backend image
49+
uses: docker/build-push-action@v5
50+
with:
51+
context: ./backend
52+
push: true
53+
tags: ${{ steps.meta.outputs.tags }}
54+
labels: ${{ steps.meta.outputs.labels }}
55+
56+
build-frontend:
57+
runs-on: ubuntu-latest
58+
permissions:
59+
contents: read
60+
packages: write
61+
62+
steps:
63+
- name: Checkout code
64+
uses: actions/checkout@v4
65+
66+
- name: Set up Docker Buildx
67+
uses: docker/setup-buildx-action@v3
68+
69+
- name: Login to GHCR
70+
uses: docker/login-action@v3
71+
with:
72+
registry: ${{ env.REGISTRY }}
73+
username: ${{ github.actor }}
74+
password: ${{ secrets.GHCR_TOKEN }}
75+
76+
- name: Extract tag version
77+
id: meta
78+
uses: docker/metadata-action@v5
79+
with:
80+
images: ${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE }}
81+
tags: |
82+
type=semver,pattern={{version}}
83+
type=semver,pattern={{major}}.{{minor}}
84+
type=raw,value=latest
85+
86+
- name: Build and push frontend image
87+
uses: docker/build-push-action@v5
88+
with:
89+
context: ./client-nachweise
90+
push: true
91+
tags: ${{ steps.meta.outputs.tags }}
92+
labels: ${{ steps.meta.outputs.labels }}
93+
94+
deploy:
95+
needs: [build-backend, build-frontend]
96+
runs-on: ubuntu-latest
97+
steps:
98+
- name: Checkout code
99+
uses: actions/checkout@v4
100+
101+
- name: Deploy to VPS
102+
uses: appleboy/ssh-action@master
103+
with:
104+
host: ${{ secrets.VPS_HOST }}
105+
username: ${{ secrets.VPS_USER }}
106+
key: ${{ secrets.VPS_SSH_KEY }}
107+
port: ${{ secrets.VPS_PORT || 22 }}
108+
script: |
109+
cd ${{ secrets.VPS_DEPLOY_PATH }}
110+
git pull origin main
111+
docker-compose pull
112+
docker-compose up -d

‎.github/workflows/docker-hub.yml‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,52 @@
1+
# Builds Docker images to Docker Hub when a git tag is pushed
2+
# Triggered only on: git tag -a v1.0.0 && git push origin v1.0.0
3+
# Images appear at: dockerhub.com/your-username/nachweise-backend:1.0.0
4+
15
name: Build and Push to Docker Hub
26

37
on:
48
push:
5-
branches:
6-
- main
79
tags:
8-
- "v*"
9-
workflow_dispatch:
10-
11-
env:
12-
DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
10+
- "*" # Triggered on any git tag
1311

1412
jobs:
15-
build-and-push:
13+
build:
1614
runs-on: ubuntu-latest
1715
strategy:
1816
matrix:
1917
include:
20-
- dockerfile: ./backend/Dockerfile
21-
image: nachweise-backend
18+
- image: nachweise-backend
2219
context: ./backend
23-
- dockerfile: ./client-nachweise/Dockerfile
24-
image: nachweise-frontend
20+
- image: nachweise-frontend
2521
context: ./client-nachweise
2622

2723
steps:
28-
- name: Checkout repository
24+
- name: Checkout code
2925
uses: actions/checkout@v4
3026

3127
- name: Set up Docker Buildx
3228
uses: docker/setup-buildx-action@v3
3329

34-
- name: Log in to Docker Hub
30+
- name: Login to Docker Hub
3531
uses: docker/login-action@v3
3632
with:
3733
username: ${{ secrets.DOCKERHUB_USERNAME }}
3834
password: ${{ secrets.DOCKERHUB_TOKEN }}
3935

40-
- name: Extract metadata
36+
- name: Extract tag version
4137
id: meta
4238
uses: docker/metadata-action@v5
4339
with:
44-
images: ${{ env.DOCKER_USERNAME }}/${{ matrix.image }}
40+
images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image }}
4541
tags: |
46-
type=ref,event=branch
4742
type=semver,pattern={{version}}
4843
type=semver,pattern={{major}}.{{minor}}
49-
type=sha,prefix={{branch}}-
50-
type=raw,value=latest,enable={{is_default_branch}}
44+
type=raw,value=latest
5145
52-
- name: Build and push
46+
- name: Build and push image
5347
uses: docker/build-push-action@v5
5448
with:
5549
context: ${{ matrix.context }}
5650
push: true
5751
tags: ${{ steps.meta.outputs.tags }}
5852
labels: ${{ steps.meta.outputs.labels }}
59-
cache-from: type=registry,ref=${{ env.DOCKER_USERNAME }}/${{ matrix.image }}:buildcache
60-
cache-to: type=registry,ref=${{ env.DOCKER_USERNAME }}/${{ matrix.image }}:buildcache,mode=max

0 commit comments

Comments
 (0)