GitHub - Get - modified files from the current commit.

I was looking into a way to get the files changed between the latest and previous commit in GitHub so that the workflow can process only the modified files after the commit or merge.

The below workflow, gets the modified files using the latest commit and previous commit sha and invokes the modified scripts in the repository.

...
jobs:
  # This workflow contains a single job called "build"
  deployM365:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: deploy
        id: deploy
        run: |
          FILES=(`git diff --diff-filter=M --name-only ${{ github.event.before }}  ${{ github.sha }}`)
          matrixFiles=""
          for file in "${FILES[@]}"; do
            chmod +x ./${file}
            ./${file}
          done

This workflow uses “git diff” cli, previous and latest commit sha from the “github” object to get the modified files. This can be amended to filter further either to include “created”, “deleted” files etc by passing the relevant values to the “–diff-filter” parameter. More details about the “git diff” can be found here.

Respository

This repository contains a script to deploy M365 sites and M365 list.

If any of the deployment scripts are modified, workflow will execute those scripts.

Written on June 12, 2023