2021-04-02

Jenkins: fire on change of specific file in specific branch in Bitbucket

Jenkins: fire on change of specific file in specific branch in Bitbucket

Perhaps different set of plugins can execute this task, but I’ve implemented with Generic Webhook trigger.

Final version is for Bitbucket’s Pull request merged webhook, but it also may be adopted to Repository push Webhook

In the Bitbucket webhook is configured that calls Jenkins.

In Jenkins build trigger is Generic Webhook Trigger.
Post content parameters

Variable Expression Comment
merge_commit_hash $.pullRequest.properties.mergeCommit.id For pull request merge
pull_request_branch $.pullRequest.toRef.id For pull request merge
commit_hash $.changes[0].toHash For repository push
push_branch $.changes[0].refId For repository push

Filter in Jenkins’ Generic Webhook Trigger to process only changes in specific branch:

Expression Text
refs/heads/dev $push_branch$pull_request_branch

Example of simple Execute shell step :

#!/bin/bash

echo "Looking for $FILE_NAME in Commit $commit_hash $merge_commit_hash"
# list of changed files for a commit
#filelist=$(git diff-tree --no-commit-id --name-only -r $commit_hash)
#commit is merge commit - must use different tactics
filelist=$(git log -m -1 --name-only --pretty="format:" $merge_commit_hash)
if [[ $filelist == *"$FILE_NAME"* ]]; then
$ process file
else
	echo "Skip"
fi

Комментариев нет: