Skip to content

Instantly share code, notes, and snippets.

@dleidert
Last active March 1, 2019 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dleidert/2e8834719f98afcb92c1d740c14bedbf to your computer and use it in GitHub Desktop.
Save dleidert/2e8834719f98afcb92c1d740c14bedbf to your computer and use it in GitHub Desktop.
Skip TRAVIS-CI builds for non-build relevant file changes

TRAVIS builds are triggered by every commit to the repository. When hosting github pages with the source (e.g. in the docs folder), changes to the documentation are (usually) not critical to the build itself, so they could be easily skipped. Unfortunately TRAVIS does not offer a file-/foldername-based skip instruction like e.g. AppVeyor's skip_commits field.

But there seem to be two ways of achieving (almost) the same effect. In the following examples changes to the files

.appveyor.yml
.gitattributes
.gitignore
docs/**

should not trigger a TRAVIS build. The first way is using the before_install instruction in .travis.yml:

before_install:
  - |
    if ! git diff --name-only $TRAVIS_COMMIT_RANGE | grep -qvE '(^\.(git|appveyor))|(^(LICENSE|docs))'
    then
      echo "Only build non-relevant files were updated, not running the CI."
      exit
    fi

This will not stop TRAVIS from setting up the environment(!) but will exit the build before the install job is started.

To skip the job, one can also add [skip ci] to the commit message. To automate this I've created the following hook .git/hooks/prepare-commit-msg

#!/bin/sh
  
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3

if git diff --cached --exit-code --name-only -- . ':(top,exclude)docs/' ':!\.git*' ':!\.appveyor.yml' #>/dev/null 2>&1
then
  /usr/bin/perl -i.bak -pe 'print "[skip travis] " if !$first_line++' "$COMMIT_MSG_FILE"
fi

IMO the disadvanage here is, that this pollutes the git log.

The best solution would be, if TRAVIS could add support for something like the already mentioned skip_commits instruction set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment