Search code examples
githubcontinuous-integrationgithub-actions

Run github actions if branch has updated files from a specific directory


File structure:

apps
-- app-1
-- app-2
libs
-- lib-1
-- lib-2

We have tests that should run only in case if files were changed in lib-2. I have tried to do

on:
  push:
    paths:
      - 'libs/lib-2/**'

But it runs tests only when files from lib-2 were pushed in a commit but not running if some others were pushed after that. Imagine tests are failed for lib-2, then developer have submitted files from lib-1 in the next commit and tests just wouldn't run for previous changes and github will consider checks as a success.

Is there a way to run actions if files from a certain directory were changed in a branch no matter in what commit?


Solution

  • I have designed a solution thanks to @guifalourd.

    name: 'UI-kit Tests'
    on:
      pull_request:
        branches:
          - proto
          - develop
          - staging
          - master
    jobs:
      filter-ui-kit:
        runs-on: ubuntu-latest
        name: Filter Ui kit
        steps:
          - uses: actions/checkout@v3
            with:
              fetch-depth: 0
    
          - name: Get changed files in the docs folder
            id: changed-files-specific
            uses: tj-actions/changed-files@v34
            with:
              files: libs/ui-kit/**
    
          - name: Run step if any file(s) in the docs folder change
            if: steps.changed-files-specific.outputs.any_changed == 'true'
            run: echo UI-kit is affected
    
          - name: Prevent from running
            if: steps.changed-files-specific.outputs.any_changed != 'true'
            run: exit 1
      test:
        timeout-minutes: 60
        runs-on: ubuntu-latest
        needs: [filter-ui-kit]
        steps:
          ... test actions goes there