Trigger specific job on push to specific directory Trigger specific job on push to specific directory jenkins jenkins

Trigger specific job on push to specific directory


Well I have hacked a solution that works for us.

First, add an Execute Shell Build Step:

#!/bin/bashexport DIRS="api objects"DIFF=`git diff --name-only develop`echo "export RUN_TEST=0" > "$WORKSPACE/RUN_TEST"for DIR in $DIRS; do  for LINE in $DIFF; do    # Is this file inside an interesting directory?    echo $LINE | grep -e "^$DIR/"    # Checking if it is inside    if [ $? -eq 0 ]; then      echo "export RUN_TEST=1" > "$WORKSPACE/RUN_TEST"    fi  donedone

Here:

  • api and objects are the 2 directories I want to trigger this Job
  • develop is the main branch we use, so I want to know how my directories compare to that branch in particular
  • I create a file $WORKSPACE/RUN_TEST to set a variable if I should or not run it

Then in the time-consuming build steps add:

#!/bin/sh. "$WORKSPACE/RUN_TEST"if [ $RUN_TEST -eq 1 ]; then  # Time consuming code herefi

That way the job is triggered but runs as fast as if it wasn't triggered.


Now I modified it to:

#!/bin/bashexport DIRS="api objects"DIFF=`git diff --name-only origin/develop`RUN_TEST=111for DIR in $DIRS; do  for LINE in $DIFF; do    # Is this file inside an interesting directory?    echo $LINE | grep -e "^$DIR/"    # Checking if it is inside    if [ $? -eq 0 ]; then      RUN_TEST=0    fi  donedoneecho "RUN_TEST=$RUN_TEST"echo "return $RUN_TEST" > "$WORKSPACE/RUN_TEST"exit $RUN_TEST

And set Exit code to set build unstable to 111 on all build steps. Then, in all following build steps I did:

#!/bin/bash# Exit on any errorset -euo pipefail. "$WORKSPACE/RUN_TEST"# Rest of build step