简体   繁体   中英

Pushing a branch interpreted as pushing master

Environment:

  • Jenkings project to only build the "master" branch of a Git repository.
  • A Git repository with a post-receive hook to inform Jenkins of new changes (using Git 1.7.10). Although the script tells Jenkins there are new changes, but when Jenkins scan the repository it won't trigger a build if changes are not in "master" branch.

Problem

A developer pushed a commit to remote master 10 days ago. The Jenkins project was set up 5 days ago. Yesterday, changes were pulled by second developer, a new branch was created and pushed back to remote. That is, nothing in master was changed. However, Jenkins project kicked in. Looking at changes detected by Jenkins, it was the last commit made by first developer 10 days ago that triggered the build.

I realized this issue has not been answered.. This is how I fixed it back then..

Reason

Jenkins' builds are triggered from our Git repo's "post-receive" hook script:

#!/bin/bash
while read oldrev newrev refname
do
    true
done

curl http://jenkins/git/notifyCommit?url=ssh://git@devserver/repos/particualr_repo.git

As it can be seen, the script read the stdin and then ignore it. So the build kicks in when a commit is pushed from any branch. But since our build project is configured to build master and since the last commit to master was 10 days ago, it registers that as the change that caused the build.

Solution (to queue builds only when master is pushed)

Change the hook script to build only master is pushed.

#!/bin/bash

if ! [ -t 0 ]; then
   read -a ref
fi

IFS='/' read -ra REF <<< "${ref[2]}"
branch="${REF[2]}"

if [ "master" == "$branch" ]; then
   curl http://jenkins/git/notifyCommit?url=ssh://git@devserver/repos/particualr_repo.git
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM