'In Github actions, in an expression is there a way to determine what path/directory was pushed/committed to?
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>
Solution 1:[1]
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.
Solution 2:[2]
The best way to deal with that is to use of the existing actions in the Marketplace.
https://github.com/tj-actions/changed-files
Natively GitHub doesn't give you a solution directly for what you are looking for.
Solution 3:[3]
Github action workflow information is stored in contexts. You can actually use the script given here to help you with create the if conditionals.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GuiFalourd |
| Solution 2 | Grzegorz Krukowski |
| Solution 3 | Brad Rehman |
