Skip to content

Commit 044dad9

Browse files
committed
feat: add GitHub Actions workflows for building and pushing Docker images to GitHub Container Registry and Docker Hub
1 parent 3a16a39 commit 044dad9

2 files changed

Lines changed: 188 additions & 0 deletions

File tree

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

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Build and Push to Docker Hub
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
tags:
8+
- "v*"
9+
workflow_dispatch:
10+
11+
env:
12+
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
13+
14+
jobs:
15+
build-and-push:
16+
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
include:
20+
- dockerfile: ./backend/Dockerfile
21+
image: nachweise-backend
22+
context: ./backend
23+
- dockerfile: ./client-nachweise/Dockerfile
24+
image: nachweise-frontend
25+
context: ./client-nachweise
26+
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@v4
30+
31+
- name: Set up Docker Buildx
32+
uses: docker/setup-buildx-action@v3
33+
34+
- name: Log in to Docker Hub
35+
uses: docker/login-action@v3
36+
with:
37+
username: ${{ secrets.DOCKER_USERNAME }}
38+
password: ${{ secrets.DOCKER_PASSWORD }}
39+
40+
- name: Extract metadata
41+
id: meta
42+
uses: docker/metadata-action@v5
43+
with:
44+
images: ${{ env.DOCKER_USERNAME }}/${{ matrix.image }}
45+
tags: |
46+
type=ref,event=branch
47+
type=semver,pattern={{version}}
48+
type=semver,pattern={{major}}.{{minor}}
49+
type=sha,prefix={{branch}}-
50+
type=raw,value=latest,enable={{is_default_branch}}
51+
52+
- name: Build and push
53+
uses: docker/build-push-action@v5
54+
with:
55+
context: ${{ matrix.context }}
56+
push: true
57+
tags: ${{ steps.meta.outputs.tags }}
58+
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)