Trigger build in Jenkins when git branches are created or deleted

I have a job in Jenkins for a project on GitHub, that I would like to be triggered whenever a new branch is created or an existing branch has been removed. Is this possible?

Notice: The Jenkins server is located internally in a company, so we can’t use web hooks from GitHub.

  • How to manage a code merges on git with multiple users?
  • Can't do git push/pull on Cygwin: error while loading shared libraries
  • git stash clear, except a specific stash
  • How do I save the original creation time in git?
  • Python - A Git module which doesn't depend on the git binary file
  • Line Ending Problems in GIT
  • Git Should Not Checkin If Submodule Has Modified Files
  • Integrate Visual Studio online Git Repository to Android Studio 1.0.2
  • Should the same name and email be used on all devices with Git?
  • Using Git with a website
  • Various ways to remove local Git changes
  • how to host gem in Github and use it?
  • 4 Solutions collect form web for “Trigger build in Jenkins when git branches are created or deleted”

    I can think of one approach, you may use.

    Using Job DSL Plugin allow you to create or delete projects using Groovy. It is not hard to include github scanning and create jobs from that. Good thing about it is, that it recognizes deleted jobs as well.

    I.e. Install Job DSL plugin, create a seed job (free-style) with a regular trigger, and paste something akin the below into your script..

    def project = 'nbn/griffon-maven-plugin'
    def branchApi = new URL("${project}/branches")
    def branches = new groovy.json.JsonSlurper().parse(branchApi.newReader())
    branches.each { 
        def branchName =
        job {
            name "${project}-${branchName}".replaceAll('/','-')
            scm {
                git("git://${project}.git", branchName)
            steps {
                maven("test${project}/${branchName} ")

    You can try this approach if it looks good to you. 🙂

    Schedule a cron on the build machine to perform the following task:

    1. Fetch list of branch from the Git repository and store it in a file, say branch_list

      We use Gitolite and access branch names using git ls-remote command.

      git ls-remote

      For example,

      [tom@master ~]$ git ls-remote
      08a119f0aec5d4286708d2e16275fcd7d80d2c25        HEAD
      a91ef29f1be5bfe373598f6bb20d772dcc65b8ca        refs/heads/dev-mob
      d138356cf752a46fd8c626229809c9eaae63a719        refs/heads/dev-ssorel
      e7d7e2c617c4a42b299b29c0119283813800f1bb        refs/heads/dev-omni
      3193b36d678f1af2dcc3a291c6313f28ede97149        refs/heads/dev-pay
      72fd9d8586708011c763cd7bc4f7bd2a3513a12f        refs/heads/dev-sell
      39455fc2672039a7f325e9cafe3777ed563368ef        refs/heads/dev-apis
      a22eb000ffa1ac0fbbf51b6bc8aea31b040567a3        refs/heads/dev-front
      78a63105ec754d7ba758af97d542e749ceb9c533        refs/heads/dev-tpsp
      82d99796690b6c562872ea68655c74ebc3f0abfb        refs/heads/mainline
      fd82522f9999cedb11e245b515d480187c2e9cc6        refs/heads/master

      To filter out only the branch names, you can use:

      [tom@master ~]$ git ls-remote | grep -v HEAD | cut -d/ -f3 | sort > branch_list_latest
    2. Do a diff with the last fetched file i.e., branch_list. If there is a difference, then trigger the build. You can either use diff or cmp command.

      git ls-remote | grep -v HEAD | cut -d/ -f3 | sort > branch_list_latest
      if ! cmp -s branch_list branch_list_latest; then
         mv branch_list_latest branch_list
         echo "Files differ which means branch created or removed. Triggering build..."
         # Trigger build command

    Cron will keep fetching the list of branches after certain interval. You can define the interval as per your need.

    Where I am now we have two long-lived branches, and the rest is almost exclusively short-lived feature branches.

    We already have jobs for the long-lived branches.

    For the feature branches I have looked at


    and found that they are both slightly too complicated for our use.

    Instead I set up one job with the branch-specifier “*_build” for the convention that if you push a branch ending with “_build” it will be built by Jenkins. Next step is remembering that when you review, the pull request better be named “xyz…_build” 🙂


    According to you should be able to create and delete a job depending on the creation and removal of a branch.

    I haven’t tried it, though…

    You might also want to read

    Git Baby is a git and github fan, let's start git clone.