我在Jenkins中为GitHub上的项目工作,希望在创建新分支或删除现有分支时触发它。这可能吗?在创建或删除git分支时在Jenkins中触发构建
注意:Jenkins服务器位于公司内部,因此我们无法使用来自GitHub的Web钩子。
我在Jenkins中为GitHub上的项目工作,希望在创建新分支或删除现有分支时触发它。这可能吗?在创建或删除git分支时在Jenkins中触发构建
注意:Jenkins服务器位于公司内部,因此我们无法使用来自GitHub的Web钩子。
我可以想到一种方法,你可以使用。
使用作业DSL插件允许您使用Groovy创建或删除项目。包含github扫描并创建工作并不难。关于它的好处是,它也识别已删除的作业。
I.e.安装工作DSL插件,创建一个种子作业(自由风格)有规律的触发,并粘贴一个类似于以下到脚本..
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} ")
}
}
}
呵呵,实际上这是我创建或者销毁分支时我想运行的DSL工作。现在它只是间隔运行,而且也可以正常工作。只是想知道是否可以让它在有理由的时候才运行。 – Tobias 2014-09-26 20:29:57
你仍然需要以任何一种方式轮询。作业DSL不应该有太多的解析整个配置的开销,并且应该正确地进行脏检查,所以虽然Technext的解释很好,但它可能只是需要以某种方式维护的开销。如果您对Job DSL有任何建议,请随时放下评论或两条评论。 – 2014-10-01 01:48:59
你可以试试这个方法,如果它看起来对你很好。 :)
附表build机器上的一个cron来执行以下任务:
从Git仓库取分支的列表,并将其存储在一个文件,说branch_list
我们使用Gitolite并使用git ls-remote
命令访问分支名称。
git ls-remote [email protected]:repository_name
例如,
[[email protected] ~]$ 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
要过滤掉仅分支的名称,你可以使用:
[[email protected] ~]$ git ls-remote [email protected]:repository_name | grep -v HEAD | cut -d/ -f3 | sort > branch_list_latest
做一个DIFF与最后牵强文件即branch_list。如果有差异,则触发构建。您可以使用diff
或cmp
命令。
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将会保持一定的时间间隔后取分支列表。您可以根据需要定义间隔。
我现在在哪里我们有两个长寿的分支,其余的几乎都是短命的特色分支。
我们已经有长期分支机构的工作。
对于功能分支我已经看过
https://pythonhosted.org/jenkins-autojobs/
和
http://entagen.github.io/jenkins-build-per-branch/
,发现它们都是稍微有点为我所用复杂。
相反,我使用分支说明符“* _build”为约定设置了一个作业,如果推送以“_build”结尾的分支,它将由Jenkins构建。下一步是要记住,当你回顾,拉要求更好地被命名为“某某..._打造” :)
最佳, 安德斯
如何从詹金斯连接(协议)到GitHub上? – Technext 2014-09-26 13:46:49
应该不会太难为它编写脚本。基于ls命令http://stackoverflow.com/questions/10911923/get-list-of-git-branches-for-a-repo-hosted-on-github – 2014-09-26 16:13:00
@Technext通过SSH,因为它是一个私人存储库。 – Tobias 2014-09-26 20:32:30