Santhosh
Santhosh
Code with passion
Aug 14, 2022 2 min read

Run Github Workflow Based on Files Changed

Consider scenario of a monorepo in which you only need to run github workflow jobs based on the changes in the respective src directories. Github path filter action can exactly do this. Let’s look at an example on the same, Consider the folder structure:

.
├── client                   
├── server                 

Run jobs according to the changes in these directory i.e frontend deployment should only happen when there’s changes in client folder and backend deployment only when changes in server folder.

Create github workflow with path-filter action

Create workflow file in .github/workflows/deployment.yml.

name: DeployStaging
on:
  push:
    branches:
      - main
  workflow_dispatch:
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: check for file changes
        uses: dorny/paths-filter@v2
        id: changes
        with:
          filters: |
            backend:
              - 'server/**'
            frontend:
              - 'client/**'            
      - name: client deployment
        if: steps.changes.outputs.frontend == 'true'
        run: echo 'frontend deployment'
      - name: backend deployment
        if: steps.changes.outputs.backend == 'true'
        run: echo 'frontend deployment'

steps explained:

  1. checkout the repo on push
  2. check for changes using the filter, id will be used to get context and outputs of the current job in subsequent steps
  3. use the if condition to decide whether to run this job
  4. run: just prints message for now, it will have your actual deployment job.

Checkout this repository for running example on the same. In next blog post we’ll look at an example of building a real world monorepo application with deployment previews.