|
| 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" & |
0 commit comments