Git hooks - how to handle only files that belong to specific folder inside repo?

Viewed 195

I'm kind of new in git, and not very good with bash.

I have a repository (in Bitbucket if it matters), and it contains only two folders.

I already have a hook (written in bash), that triggers a Jenkins job.

Now I want to change it, so it will trigger the job only for files that are committed to one of the folders, and not for the other one.

I wanted to use the name of the committed file, because it will contain the folder name, but I don't know how to do that.

Any help will be much appreciated.

1 Answers

The post-receive hook will be fed old reference, new reference and a branch reference on stdin. Since multiple branches can be pushed, we'll look for the one we are interested in. Then we can use git diff-tree to list the files that are being pushed on that branch. The glob expression, $deploy_dir/*, will only list files going to the deploy directory.

#!/bin/bash
# post-receive script

deploy_branch='master'
deploy_dir='folder2'
trigger_jenkins=0

while read -r oldref newref ref; do
    if [ "${ref##*/}" == "$deploy_branch" ]; then
        if [ ! -z $( git diff-tree --no-commit-id --name-only -r "$newref" $deploy_dir/* ) ]; then
            trigger_jenkins=1
        fi
    fi
done

if [ $trigger_jenkins -eq 1 ]; then
    echo trigger jenkins job
fi
Related