Blogs

Github Workflow — Feature branch changes— How to trigger a job based on changes to specific path

Imagine you have the following requirements:

  • Workflow triggers when a commit is pushed to a feature branch
  • Capture if changes have been made to files in a specific path
  • Run Step only if changes have been made to files in that path.

Because the workflow was triggered when a change was pushed to the feature branch and the checkout action would set the feature branch as the active branch, to detect the changes cannot be done the same way you would do it when the Workflow is triggered by a Pull Request.

After trying multiple options/parameters of the git diff command ( a lot of trial an error), finally the below version worked.

Steps:

  • Fetch-depth needs to be set to 2 or more, otherwise it was not working for me.
  • get the latest commit hash
  • use git diff-tree for that hash and filter on the specific path you want to monitor for changes
  • Output the returned list of files to a variable. when multiple files were returned I would get an error trying to assign the value to the output variable so I did a quick/dirty fix to trim the result.
  • Output variable is then used in a subsequent Step or in my case in a different JOB to determine if the job needs to run

In the example below I have 2 GitHub workflow jobs ( BUILD and INIT). In the BUILD job, there is a step that checks the file changes in the last commit and outputs these changes to a output variable. The INIT job check if there are any changes in the output variable. The job only runs if there are changes in the output variable, otherwise it will skip the job.

Example

1- Checking file changes in specific file path

Checking for file changes in the last commit in specific path → INIT_PATH: ‘src/init_account/**’ and output changes to output variable → init_changes: ${{ steps.file_changes.outputs.changes }}

DEPLOY:
    name: na
    runs-on: ubuntu-latest
    environment: DEV
    outputs:
      init_changes: ${{ steps.file_changes.outputs.changes }}
    needs: [BUILD]
    steps:
      - uses: actions/checkout@v3 
        with:
          fetch-depth: 2
      - name: Export file changes
        id: file_changes
        env:
          INIT_PATH: 'src/init_account/**' 
        run: |
          commit_hashes=$(git log -n 1 --pretty=format:"%H")
          echo "Last commit hashes: $commit_hashes"
          result=$(git diff-tree --no-commit-id --name-only -r $commit_hashes -- ${{env.INIT_PATH}})
          echo "File changes: $result"
          echo "changes=${result:0:18}" >> $GITHUB_OUTPUT 

2- Use the output variable as a condition to run or skip the INIT job.

INIT:
    name: Init Workspace
    runs-on: ubuntu-latest
    environment: DEV
    needs: [DEPLOY]
    if: ${{needs.DEPLOY.outputs.init_changes != ''}}
[mailpoet_form id="1"]

Other Recent Blogs

Microsoft Teams IP Phones and Intune Enrollment

Microsoft Teams provides a growing portfolio of devices that can be used as desk and conference room phones. These IP phones run on Android 8.x or 9.x and are required to be enrolled in Intune. By default, these devices are enrolled as personal devices, which is not ideal as users should not be able to enrol their own personal Android devices.

Read More »

Level 9, 360 Collins Street, 
Melbourne VIC 3000

Level 2, 24 Campbell St,
Sydney NSW 2000

200 Adelaide St,
Brisbane QLD 4000

191 St Georges Terrace
Perth WA 6000

Level 10, 41 Shortland Street
Auckland

Part of

Arinco trades as Arinco (VIC) Pty Ltd and Arinco (NSW) Pty Ltd. © 2023 All Rights Reserved Arinco™ | Privacy Policy | Sustainability and Our Community
Arinco acknowledges the Traditional Owners of the land on which our offices are situated, and pay our respects to their Elders past, present and emerging.

Get started on the right path to cloud success today. Our Crew are standing by to answer your questions and get you up and running.