2

We have 12 different projects inside the same repository and have a different job to run for each of these.

I want to know how I can trigger a job only when a change has happened in a specific folder, since running all 12 on every push takes too long to finish.

chamini2
  • 2,820
  • 2
  • 24
  • 37
  • I've been trying to figure this out as well. You can use `Single repository & branch` and set add `Polling ignores commits in certain paths` to `Additional Behaviours`. However, this breaks the github hook triggers so webhooks don't trigger anything. In our case we have a Docker repo in which we have images which depend on each other and this has been driving me crazy for a while now. – tftd May 23 '18 at 00:01
  • @tftd, check my answer. – chamini2 May 24 '18 at 20:08

1 Answers1

3

Well I have hacked a solution that works for us.

First, add an Execute Shell Build Step:

#!/bin/bash
export 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
  done
done

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 here
fi

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


Now I modified it to:

#!/bin/bash
export DIRS="api objects"
DIFF=`git diff --name-only origin/develop`

RUN_TEST=111

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
      RUN_TEST=0
    fi
  done
done

echo "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 error
set -euo pipefail
. "$WORKSPACE/RUN_TEST"

# Rest of build step
chamini2
  • 2,820
  • 2
  • 24
  • 37