|
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 |
2 | 6 |
|
3 | 7 | 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 |
14 | 11 |
|
15 | 12 | 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 |
19 | 16 |
|
20 | 17 | 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 |
0 commit comments