Creating a trigger in Jenkins when creating or deleting git branches

I have a job in Jenkins for a GitHub project that I would like to run every time a new branch is created or an existing branch is deleted. Is it possible?

Note. The Jenkins server is inside the company, so we cannot use web hooks from GitHub.

+6
source share
4 answers

I can come up with one approach that you can use.

Using the DSL Job Plugin allows you to create or delete projects using Groovy. It’s easy to enable github scanning and create jobs from this. The good thing is that it also recognizes remote jobs.

those. Install the Job DSL plugin, create the initial job (in the free style) using a regular trigger, and paste something similar below into your script ..

def project = 'nbn/griffon-maven-plugin' def branchApi = new URL("https://api.github.com/repos/${project}/branches") def branches = new groovy.json.JsonSlurper().parse(branchApi.newReader()) branches.each { def branchName = it.name job { name "${project}-${branchName}".replaceAll('/','-') scm { git("git://github.com/${project}.git", branchName) } steps { maven("test -Dproject.name=${project}/${branchName} ") } } } 
+3
source

You can try this approach if you like it. :)

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

  • Get a list of branches from a Git repository and save it in a file, e.g. branch_list

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

    git ls-remote gitolite@git.server.com :repository_name

    For instance,

     [ tom@master ~]$ git ls-remote gitolite@git.server.com :repository_name 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 only branch names, you can use:

     [ tom@master ~]$ git ls-remote gitolite@git.server.com :repository_name | grep -v HEAD | cut -d/ -f3 | sort > branch_list_latest 
  • Make a difference with the last extracted file, i.e. the name_ branch. If there is a difference, then run the assembly. You can use diff or cmp command.

     git ls-remote gitolite@git.server.com :repository_name | 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 fi 

Cron will continue to receive the list of branches after a certain interval. You can define the interval according to your needs.

+2
source

Where am I now, we have two long-lived branches, and the rest are almost exclusively short-lived branches of functions.

We already have jobs for long-lived branches.

For function branches I looked

https://pythonhosted.org/jenkins-autojobs/

and

http://entagen.imtqy.com/jenkins-build-per-branch/

and found that they are too complicated for our use.

Instead, I set up one job with the branch specifier "* _build" for the agreement, which, if you click on the branch ending in "_build", will be built by Jenkins. The next step remembers that when you browse, a stretch request is better called "xyz ..._ build" :)

Best, Anders

0
source

According to https://jenkins-ci.org/blog/2015/12/03/pipeline-as-code-with-multibranch-workflows-in-jenkins/ you should be able to create and delete a task depending on the creation and delete branches.

I have not tried, though ...

You can also read https://blog.codecentric.de/en/2015/04/generated-jenkins-jobs-and-automatic-branch-merging-for-feature-branches/

0
source

Source: https://habr.com/ru/post/975884/


All Articles