From bfc88f164b92dc42eb6cc7d55f99ccfcc63f408a Mon Sep 17 00:00:00 2001 From: Kozachenko Diana Date: Thu, 26 Sep 2024 16:10:00 +0300 Subject: [PATCH] =?UTF-8?q?ci/cd:=20=D0=9F=D0=B0=D1=80=D0=B0=D0=BB=D0=BB?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20=D0=B4=D0=B6=D0=BE=D0=B1?= =?UTF-8?q?=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D1=83=D1=81=D0=BA=D0=BE=D1=80?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=B0=D0=B9=D0=BF=D0=BB=D0=B0?= =?UTF-8?q?=D0=B9=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 135 +++++++++++++++++++++++++---------- docker-compose.prod.yml | 5 +- 2 files changed, 100 insertions(+), 40 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 8701a0e..7969792 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,17 +6,36 @@ on: env: REGISTRY: ghcr.io IMAGE_TAG: latest + FRONTEND_PATH: ./frontend + BACKEND_PATH: ./backend + DOCKER_COMPOSE_FILE: docker-compose.prod.yml jobs: - publish-image: + form-repository-lowercase-name: + runs-on: ubuntu-latest + outputs: + repository_name_lowercase: ${{ steps.formatting.outputs.lowercase }} + steps: + # - name: Get repository code + # uses: actions/checkout@v4 + - name: Get repository name in lowercase + id: formatting + uses: ASzc/change-string-case-action@v5 + with: + string: ${{ github.repository }} + + build-and-push-frontend-image: + needs: [form-repository-lowercase-name] runs-on: ubuntu-latest permissions: contents: read packages: write + outputs: + image-name: ${{ steps.image-name.outputs.IMAGE_NAME }} steps: - name: Get repository code uses: actions/checkout@v4 - - name: Log in to the Container registry + - name: Log in container registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} @@ -27,67 +46,109 @@ jobs: uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ github.repository }} - - name: Get image name in lowercase - id: formatting - uses: ASzc/change-string-case-action@v5 - with: - string: ${{ github.repository }} + - name: Output Frontend Docker image name + id: image-name + run: echo "IMAGE_NAME=${{ env.REGISTRY }}/${{ needs.form-repository-lowercase-name.outputs.repository_name_lowercase }}-frontend:${{ env.IMAGE_TAG }}" >> "$GITHUB_OUTPUT" - name: Build and push Frontend Docker image uses: docker/build-push-action@v5 with: - context: ./frontend - file: ./frontend/Dockerfile + context: ${{ env.FRONTEND_PATH }} + file: ${{ env.FRONTEND_PATH }}/Dockerfile push: true - tags: ${{ steps.meta.outputs.tags }},${{ env.REGISTRY }}/${{ steps.formatting.outputs.lowercase }}-frontend:${{env.IMAGE_TAG}} + tags: ${{ steps.meta.outputs.tags }},${{ steps.image-name.outputs.IMAGE_NAME }} labels: ${{ steps.meta.outputs.labels }} + + build-and-push-backend-image: + needs: [form-repository-lowercase-name] + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + outputs: + image-name: ${{ steps.image-name.outputs.IMAGE_NAME }} + steps: + - name: Get repository code + uses: actions/checkout@v4 + - name: Log in container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ github.repository }} + - name: Output Backend Docker image name + id: image-name + run: echo "IMAGE_NAME=${{ env.REGISTRY }}/${{ needs.form-repository-lowercase-name.outputs.repository_name_lowercase }}-backend:${{ env.IMAGE_TAG }}" >> "$GITHUB_OUTPUT" - name: Build and push Backend Docker image uses: docker/build-push-action@v5 with: - context: ./backend - file: ./backend/Dockerfile + context: ${{ env.BACKEND_PATH }} + file: ${{ env.BACKEND_PATH }}/Dockerfile push: true - tags: ${{ steps.meta.outputs.tags }},${{ env.REGISTRY }}/${{ steps.formatting.outputs.lowercase }}-backend:${{env.IMAGE_TAG}} + tags: ${{ steps.meta.outputs.tags }},${{ steps.image-name.outputs.IMAGE_NAME }} labels: ${{ steps.meta.outputs.labels }} - # TODO: убрать хардкоды из путей и тд + + pull-frontend-image: + needs: [build-and-push-frontend-image] + runs-on: ubuntu-latest + steps: + # - name: Get repository code + # uses: actions/checkout@v4 + - name: Pull latest Frontend Docker image + uses: appleboy/ssh-action@v1.0.0 + with: + host: ${{ secrets.SERVER_IP }} + username: ${{ secrets.SERVER_USERNAME }} + password: ${{ secrets.SERVER_PASSWORD }} + script: docker pull ${{ needs.build-and-push-frontend-image.outputs.image-name }} + + pull-backend-image: + needs: [build-and-push-backend-image] + runs-on: ubuntu-latest + steps: + # - name: Get repository code + # uses: actions/checkout@v4 + - name: Pull latest Backend Docker image + uses: appleboy/ssh-action@v1.0.0 + with: + host: ${{ secrets.SERVER_IP }} + username: ${{ secrets.SERVER_USERNAME }} + password: ${{ secrets.SERVER_PASSWORD }} + script: docker pull ${{ needs.build-and-push-backend-image.outputs.image-name }} + deploy: - needs: [publish-image] + needs: [build-and-push-frontend-image, build-and-push-backend-image, pull-frontend-image, pull-backend-image] runs-on: ubuntu-latest steps: - - name: Get repository code - uses: actions/checkout@v4 - - name: Stop all containers - uses: appleboy/ssh-action@v1.0.0 - with: - host: ${{ secrets.SERVER_IP }} - username: ${{ secrets.SERVER_USERNAME }} - password: ${{ secrets.SERVER_PASSWORD }} - script: docker stop $(docker ps -a -q) - - name: Pull latest frontend image version - uses: appleboy/ssh-action@v1.0.0 - with: - host: ${{ secrets.SERVER_IP }} - username: ${{ secrets.SERVER_USERNAME }} - password: ${{ secrets.SERVER_PASSWORD }} - script: docker pull ${{ env.REGISTRY }}/${{ github.repository }}-frontend:${{env.IMAGE_TAG}} - - name: Pull latest backend image version + # - name: Get repository code + # uses: actions/checkout@v4 + - name: Stop docker compose uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.SERVER_IP }} username: ${{ secrets.SERVER_USERNAME }} password: ${{ secrets.SERVER_PASSWORD }} - script: docker pull ${{ env.REGISTRY }}/${{ github.repository }}-backend:${{env.IMAGE_TAG}} - - name: Copy docker compose config to remote server + script: docker compose -f /tmp/${{ env.DOCKER_COMPOSE_FILE }} down + - name: Copy docker compose to remote server uses: appleboy/scp-action@v0.1.7 with: host: ${{ secrets.SERVER_IP }} username: ${{ secrets.SERVER_USERNAME }} password: ${{ secrets.SERVER_PASSWORD }} - source: "docker-compose.prod.yml" + source: ${{ env.DOCKER_COMPOSE_FILE }} target: /tmp - - name: Run container + - name: Run docker compose uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.SERVER_IP }} username: ${{ secrets.SERVER_USERNAME }} password: ${{ secrets.SERVER_PASSWORD }} - script: NOTISEND_TOKEN=${{ secrets.NOTISEND_TOKEN }} docker compose -f /tmp/docker-compose.prod.yml up -d \ No newline at end of file + script: | + NOTISEND_TOKEN=${{ secrets.NOTISEND_TOKEN }} \ + FRONT_IMAGE=${{ needs.build-and-push-frontend-image.outputs.image-name }} \ + BACK_IMAGE=${{ needs.build-and-push-backend-image.outputs.image-name }} \ + docker compose -f /tmp/${{ env.DOCKER_COMPOSE_FILE }} up -d \ No newline at end of file diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 0a1f122..fb6232e 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -25,9 +25,8 @@ services: - /etc/letsencrypt:/etc/letsencrypt - /var/run/docker.sock:/var/run/docker.sock - # TODO: убрать хардкод на образ frontend: - image: ghcr.io/vklachkov/space-fest-ui-frontend:latest + image: ${FRONT_IMAGE} depends_on: - backend networks: @@ -41,7 +40,7 @@ services: - "traefik.http.services.spacefest_frontend.loadBalancer.server.port=80" backend: - image: ghcr.io/vklachkov/space-fest-ui-backend:latest + image: ${BACK_IMAGE} command: - "--db-url" - "postgresql://postgres:postgres@postgres:5432/spacechamp"