Reputation: 1938
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.
Upvotes: 8
Views: 9795
Reputation: 1240
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 job depending on the creation and removal of a branch.
I haven't tried it, though...
You might also want to read https://blog.codecentric.de/en/2015/04/generated-jenkins-jobs-and-automatic-branch-merging-for-feature-branches/
Upvotes: 0
Reputation: 1080
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
https://pythonhosted.org/jenkins-autojobs/
and
http://entagen.github.io/jenkins-build-per-branch/
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" :)
Best, Anders
Upvotes: 0
Reputation: 8107
You can try this approach if it looks good to you. :)
Schedule a cron on the build machine to perform the following task:
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 [email protected]:repository_name
For example,
[tom@master ~]$ git ls-remote [email protected]: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 out only the branch names, you can use:
[tom@master ~]$ git ls-remote [email protected]:repository_name | grep -v HEAD | cut -d/ -f3 | sort > branch_list_latest
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 [email protected]: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 keep fetching the list of branches after certain interval. You can define the interval as per your need.
Upvotes: 3
Reputation: 4859
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("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} ")
}
}
}
Upvotes: 3