From bf28157423c4a8f001a3a1b1b19fba340e1c52e4 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 8 Oct 2025 11:43:10 +0300 Subject: [PATCH] Enhance deployment workflow to detect changes in Docker images - Updated the GitHub Actions workflow to include steps for detecting changes in the web, bot, and cache proxy directories before building and pushing Docker images. - Added conditional checks to only build and push images if changes are detected, optimizing the deployment process. - Improved the checkout step by setting a fetch depth of 2 to facilitate comparison with the previous commit. --- .github/workflows/deploy.yml | 61 +++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index bd05c65..447ad00 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -10,12 +10,36 @@ jobs: name: Build and Push to Docker Hub runs-on: ubuntu-latest outputs: + web_changed: ${{ steps.changes.outputs.web_changed }} + bot_changed: ${{ steps.changes.outputs.bot_changed }} + cache_proxy_changed: ${{ steps.changes.outputs.cache_proxy_changed }} web_tag: ${{ steps.vars.outputs.web_tag }} bot_tag: ${{ steps.vars.outputs.bot_tag }} cache_proxy_tag: ${{ steps.vars.outputs.cache_proxy_tag }} steps: - name: Checkout code uses: actions/checkout@v3 + with: + fetch-depth: 2 # нужно, чтобы сравнивать с предыдущим коммитом + + - name: Detect changed directories + id: changes + run: | + echo "web_changed=false" >> $GITHUB_OUTPUT + echo "bot_changed=false" >> $GITHUB_OUTPUT + echo "cache_proxy_changed=false" >> $GITHUB_OUTPUT + + git diff --name-only HEAD~1 HEAD > changed_files.txt + + if grep -q '^apps/web/' changed_files.txt; then + echo "web_changed=true" >> $GITHUB_OUTPUT + fi + if grep -q '^apps/bot/' changed_files.txt; then + echo "bot_changed=true" >> $GITHUB_OUTPUT + fi + if grep -q '^apps/cache-proxy/' changed_files.txt; then + echo "cache_proxy_changed=true" >> $GITHUB_OUTPUT + fi - name: Create fake .env file for build run: | @@ -42,27 +66,36 @@ jobs: - name: Login to Docker Hub run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin + # ---- Web ---- - name: Build web image + if: steps.changes.outputs.web_changed == 'true' run: | docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/zapishis-web:${{ steps.vars.outputs.web_tag }} -f ./apps/web/Dockerfile . - - name: Push web image to Docker Hub + - name: Push web image + if: steps.changes.outputs.web_changed == 'true' run: | docker push ${{ secrets.DOCKERHUB_USERNAME }}/zapishis-web:${{ steps.vars.outputs.web_tag }} + # ---- Bot ---- - name: Build bot image + if: steps.changes.outputs.bot_changed == 'true' run: | docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/zapishis-bot:${{ steps.vars.outputs.bot_tag }} -f ./apps/bot/Dockerfile . - - name: Push bot image to Docker Hub + - name: Push bot image + if: steps.changes.outputs.bot_changed == 'true' run: | docker push ${{ secrets.DOCKERHUB_USERNAME }}/zapishis-bot:${{ steps.vars.outputs.bot_tag }} + # ---- Cache Proxy ---- - name: Build cache-proxy image + if: steps.changes.outputs.cache_proxy_changed == 'true' run: | docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/zapishis-cache-proxy:${{ steps.vars.outputs.cache_proxy_tag }} -f ./apps/cache-proxy/Dockerfile . - - name: Push cache-proxy image to Docker Hub + - name: Push cache-proxy image + if: steps.changes.outputs.cache_proxy_changed == 'true' run: | docker push ${{ secrets.DOCKERHUB_USERNAME }}/zapishis-cache-proxy:${{ steps.vars.outputs.cache_proxy_tag }} @@ -82,11 +115,11 @@ jobs: chmod 600 ~/.ssh/id_rsa ssh-keyscan -p ${{ secrets.VPS_PORT }} -H ${{ secrets.VPS_HOST }} >> ~/.ssh/known_hosts - - name: Ensure zapishis directory exists on VPS + - name: Ensure zapishis directory exists run: | ssh -i ~/.ssh/id_rsa -p ${{ secrets.VPS_PORT }} -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} "mkdir -p /home/${{ secrets.VPS_USER }}/zapishis" - - name: Create real .env file for production + - name: Create .env for production run: | echo "BOT_TOKEN=${{ secrets.BOT_TOKEN }}" > .env echo "LOGIN_GRAPHQL=${{ secrets.LOGIN_GRAPHQL }}" >> .env @@ -105,27 +138,19 @@ jobs: echo "OFFER_URL=${{ secrets.OFFER_URL }}" >> .env echo "PRIVACY_URL=${{ secrets.PRIVACY_URL }}" >> .env - - name: Copy .env to VPS via SCP + - name: Copy .env and docker-compose.yml to VPS uses: appleboy/scp-action@master with: host: ${{ secrets.VPS_HOST }} username: ${{ secrets.VPS_USER }} key: ${{ secrets.VPS_SSH_KEY }} port: ${{ secrets.VPS_PORT }} - source: '.env' + source: | + .env + docker-compose.yml target: '/home/${{ secrets.VPS_USER }}/zapishis/' - - name: Copy docker-compose.yml to VPS via SCP - uses: appleboy/scp-action@master - with: - host: ${{ secrets.VPS_HOST }} - username: ${{ secrets.VPS_USER }} - key: ${{ secrets.VPS_SSH_KEY }} - port: ${{ secrets.VPS_PORT }} - source: 'docker-compose.yml' - target: '/home/${{ secrets.VPS_USER }}/zapishis/' - - - name: Login and deploy on VPS + - name: Deploy on VPS run: | ssh -i ~/.ssh/id_rsa -p ${{ secrets.VPS_PORT }} -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} " cd /home/${{ secrets.VPS_USER }}/zapishis && \