Detect which subdirectory is affected on push event in Github Actions workflow

I have a multi-project (gradle) repo with each project organized in it’s own subdirectory:

root-project
 |_ sub-project-1
      |...
 |_ sub-project-2
      |...
...
 |_ sub-project-n
      |...

In my Github Actions workflow, I’d like to be able to detect which sub-project-*directories are affected so that I can trigger a build for just those sub-projects.

I’m aware that I can define a per-sub-project workflow with push event paths filter on the workflow. However that requires me to duplicate the workflow for every sub-project. What would have worked is if I could inspect the paths in the push event dynamically, so that I could use it with conditional job execution. As far as I can tell this is not possible, but I wonder if I may be missing alternatives that could allow me to parameterize a job execution with the subdirectory name.

Hi @adeshmukh-quizlet,

Please check my answer in similar ticket: Check pushed file changes with git diff-tree in GitHub Actions.

You can check the file path and parse the directory, run the build job accordingly then.

Thanks.

Thanks for linking to that thread. Indeed, this nearly gets me there:

git diff --name-only ${{ github.event.before }} ${{ github.sha }} | cut -d/ -f1 | sort -u

Hi @adeshmukh-quizlet,

Thanks for your reply! Yes, you compare the two sha for the diff and get the directory. Please notice to specify ‘fetch-depth’ value as ‘0’ for actions/checkout to get all history, code sample as below:

      - name: checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: test
        run: |
          git diff --name-only ${{ github.event.before }} ${{ github.sha }} | cut -d/ -f1 | sort -u

Let me know if you have any other queries.

Thanks.