DEV Community

Cover image for Streamline your CI/CD Pipeline with Git-diff-script-exclusions
Corentin Le Berre
Corentin Le Berre

Posted on

Streamline your CI/CD Pipeline with Git-diff-script-exclusions

If you've ever experienced the pain of running a build pipeline or test suite only to realize that none of the modified files affected the functionality you're testing, you'll appreciate the usefulness of git-diff-script-exclusions.

This command-line interface (CLI) tool allows you to compare two git commits and see if the modified files are strictly limited to the excluded paths you've configured. If so, you can avoid running unnecessary scripts, saving valuable time in your CI pipeline.

The CLI is simple to use, and it can be installed globally or used via npx. Before running the tool, you'll need to configure the exceptions in the git-diff-script-exclusions.conf.json file. This file stores the paths to the files and directories that should be excluded from consideration.

The format is straightforward, as shown below πŸ‘‡

    {
    "exceptions": [
            "docs/*",
            "git-diff-script-exclusions.conf.json"
            "src/ressource/application.yml"
        ]
    }
Enter fullscreen mode Exit fullscreen mode

You can specify folders or files for exceptions, and the CLI will check whether any modified files fall within these paths.

To use the tool, simply execute the following command, replacing source_commit_sha and target_commit_sha with the relevant SHA or branch name πŸ‘‡

$: npx git-diff-script-exclusions --source source_commit_sha --target targe_commit_sha
Enter fullscreen mode Exit fullscreen mode

Target argument is optional, by default it will take the head of your branch.

The CLI will output the modified files in the newest commit and whether they're strictly contained within the configured exceptions. For instance, if you see onlyExceptions: true, it means the modified files are within the exceptions you've specified, and you can safely skip the script.

The tool can also be integrated into your CI pipeline. The examples below show how to use it in GitLab and GitHub Actions.

πŸ‘‰ GitLab

stages:
  - test

test:
  stage: test
  image:
    name: node:lts
  script:
    - npm install -g git-diff-script-exclusions
    - echo 'Checking if tests need to be executed...'
    - GIT_DIFF_SCRIPT_EXCLUSIONS=$(npx git-diff-script-exclusions --source $CI_COMMIT_SHA)
    - echo $GIT_DIFF_SCRIPT_EXCLUSIONS
    - >
      if grep -q "onlyExceptions: true" <<< "$GIT_DIFF_SCRIPT_EXCLUSIONS"; then
        echo 'Tests are not required';
      else
        echo 'Launching tests...';
        npm ci
        npm test
      fi;
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ GitHub

name: Run Test

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v3
      with:
        node-version: 16
    - name: Install dependencies
      run: npm install -g git-diff-script-exclusions
    - name: Launch tests if needed
      run: |
        echo "Checking if tests need to be executed..."
        GIT_DIFF_SCRIPT_EXCLUSIONS=$(npx git-diff-script-exclusions --source $GITHUB_REF)
        echo $GIT_DIFF_SCRIPT_EXCLUSIONS
        if grep -q "onlyExceptions: true" <<< "$GIT_DIFF_SCRIPT_EXCLUSIONS"; then
          echo "Tests are not required"
        else
          echo "Launching tests..."
          npm ci
          npm test
        fi
Enter fullscreen mode Exit fullscreen mode

If you wish to contribute to this project, you may do so as the repository is open for contributions. If you have an idea for a new feature or bug fix, please open an issue on our GitHub page. We appreciate contributions. Thank you!

Thank you for reading this article. If you need more information, feel free to check out the Git-diff-script-exclusions repo.

Top comments (0)