Issue
I have my codebase in GIT. There are 5 files in the code base i.e.
- File1
- File2
- File3
- File4
- File5
I want to trigger a jenkins build only if File 2 is changed.Please note that, post the build is triggered it should pull the entire codebase.
I tried using poll SCM, but the problem is that the build gets triggered if any of the files in the repository are changed.I want to trigger it only if File 2 changes.
Is it possible?
Solution
Here's how I would do it.
- Configure your Jenkins job to
Trigger builds remotely
.
This enables a build trigger URL in the form of JENKINS_URL/job/MYJOB/build?token=TOKEN_NAME
. You have to specify the token name yourself.
- Configure a git post-receive hook on your repo to call that trigger.
The script for the hook is usually under .git/hooks/post-receive
. The post-receive hook runs after the entire process is completed and can be used to update other services or notify users.
A post-receive hook gets its arguments from stdin, in the form <oldrev> <newrev> <refname>
.
You can retrieve the file list from the new HEAD commit and then only proceed to the next step if it includes File2
. Something like this (not tested):
#!/bin/bash
while read oldrev newrev refname
do
# list of changed files for a commit
filelist=$(git diff-tree --no-commit-id --name-only -r $newrev)
if [[ $filelist == *"File2"* ]]; then
# call the URL according to your build trigger config
fi
done
Answered By - Martin