0

I have a Visual Studio solution with 2 projects for the backend, and a Shared library for both projects where I handle SQL queries, DTOs, etc, the first project is an API and the second is a Webapp made with Blazor Server

I wanted to use Github Actions to perform a CI/CD but now the confussion comes in

I made this yml script which allows me to sync my repo with my VPS where the apps are hosted and then it performs the deployment but there's the problem. The deployment is performed in both projects everytime I push a change to the repo in only one of them, which is not the desired behavior.

What I need is to execute the deployment steps when there's some changes in a specific project, in other case, just sync the changes and that's it.

This is how it looks so far:

name: CI/CD

on:
  push:
    branches:
      - main
      
jobs:
  build-and-deploy-backend-website:
    runs-on: ubuntu-latest

    # Pull, compile and restart the website service
    steps:       
    - name: Deploy using ssh
      uses: appleboy/ssh-action@master
      with:
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        host: ${{ secrets.SSH_HOST }}
        username: ${{ secrets.SSH_USER }}
        port: ${{ secrets.SSH_PORT }}
        script_stop: true
        script: |
          cd /home/user/src/
          git pull origin main
          cd Backend/Backend.Website
          dotnet publish /p:PublishProfile=FolderProfile
          touch /home/user/publish/website/something #In case the directory was empty to avoid an error removing all the content
          rm -r /home/user/publish/website/*
          cp -R /home/user/src/Backend/Backend.Website/bin/publish/. /home/user/publish/website/
          sudo systemctl restart website.service

  build-and-deploy-backend-api:
    runs-on: ubuntu-latest

    # Pull, compile and restart the api service
    steps:       
    - name: Deploy using ssh
      uses: appleboy/ssh-action@master
      with:
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        host: ${{ secrets.SSH_HOST }}
        username: ${{ secrets.SSH_USER }}
        port: ${{ secrets.SSH_PORT }}
        script_stop: true
        script: |
          cd /home/user/src/
          git pull origin main
          cd Backend/Backend.API
          dotnet publish /p:PublishProfile=FolderProfile
          touch /home/user/publish/api/something #In case the directory was empty to avoid an error removing all the content
          rm -r /home/user/publish/api/*
          cp -R /home/user/src/Backend/Backend.API/bin/publish/. /home/user/publish/api/
          sudo systemctl restart api.service

But then, as you may see, both jobs will be executed on push, how can I determine where is changes and where is not? so I can decide where to execute the deploy and where to skip those steps, I see no other way of doing this since I'm not using Azure or AWS and it's just a VPS where I control everything

devLander
  • 3
  • 3
  • 1
    @Azeem thanks, worked just fine, for those who needs to know, I created 2 separated yml scripts for each project, and I added the `paths: - Backend/Backend.API/**` and `paths: - Backend/Backend.Website/**` filter below the branches filter and now it detects where a change is made and runs the deployment, just as expected! – devLander Jun 20 '23 at 17:17

0 Answers0