In Github actions, in an expression is there a way to determine what path/directory was pushed/committed to?

Viewed 1624

Within a single Github action script, I'm looking to differentiate between commits on 2 different paths/directories. The below script is essentially what I'm trying to do. What I'm unsure about is how to determine the path committed to within an expression. For now I have github.event.path but that's probably wrong.

on:
  push:
    branches: [ master ]
    paths: 
      - 'Path1/**'
      - 'Path2/**'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: do something when Path1 directory has a commit
        if: github.event.path == 'Path1/**'
        run: <do something>
      - name: do something when Path2 directory has a commit
        if: github.event.path == 'Path2/**'
        run: <do something else>
3 Answers

I would recommend using the path-filter action. It enables conditional execution of workflow steps and jobs, based on the files modified by pull request, on a feature branch, or by the recently pushed commits.

Here is an example of how it would look in your case:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: dorny/paths-filter@v2
      id: filter
      with:
        filters: |
          path1:
            - 'Path1/**'
          path2:
            - 'Path2/**'

    # run only if 'path1' files were changed
    - name: path1 tests
      if: steps.filter.outputs.path1 == 'true'
      run: ...

    # run only if 'path2' files were changed
    - name: path2 tests
      if: steps.filter.outputs.path2 == 'true'
      run: ...

    # run if 'path1' or 'path2' files were changed
    - name: both paths tests
      if: steps.filter.outputs.path1 == 'true' || steps.filter.outputs.path2 == 'true'
      run: ...

There are also different examples on the action README file if you want to take a look.

Github action workflow information is stored in contexts. You can actually use the script given here to help you with create the if conditionals.

Related