简体   繁体   English

创建或删除git分支时触发Jenkins中的构建

[英]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. 我在詹金斯(Jenkins)上有一个在GitHub上的项目的工作,我想在创建新分支或删除现有分支时触发。 Is this possible? 这可能吗?

Notice: The Jenkins server is located internally in a company, so we can't use web hooks from GitHub. 注意:Jenkins服务器位于公司内部,因此我们不能使用来自GitHub的Web挂钩。

I can think of one approach, you may use. 我可以想到一种方法,您可以使用。

Using Job DSL Plugin allow you to create or delete projects using Groovy. 使用Job DSL插件,您可以使用Groovy创建或删除项目。 It is not hard to include github scanning and create jobs from that. 包含github扫描并从中创建作业并不难。 Good thing about it is, that it recognizes deleted jobs as well. 这样做的好处是,它也可以识别已删除的作业。

Ie Install Job DSL plugin, create a seed job (free-style) with a regular trigger, and paste something akin the below into your script.. 即安装Job 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} ")
        }
    }
}

You can try this approach if it looks good to you. 如果您觉得不错,可以尝试这种方法。 :) :)

Schedule a cron on the build machine to perform the following task: 在构建机器上安排cron以执行以下任务:

  1. Fetch list of branch from the Git repository and store it in a file, say branch_list 从Git存储库中获取分支列表并将其存储在文件中,例如branch_list

    We use Gitolite and access branch names using git ls-remote command. 我们使用Gitolite并使用git ls-remote命令访问分支名称。

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

    For example, 例如,

     [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 out only the 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 
  2. Do a diff with the last fetched file ie, branch_list . 最后获取的文件,即branch_list进行比较 If there is a difference, then trigger the build. 如果存在差异,则触发构建。 You can either use diff or cmp command. 您可以使用diffcmp命令。

     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 keep fetching the list of branches after certain interval. Cron将在特定时间间隔后继续获取分支列表。 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 对于我看过的功能分支

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

and

http://entagen.github.io/jenkins-build-per-branch/ 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. 取而代之的是,我为分支规范“ * _build”设置了一项作业,即如果您推送以“ _build”结尾的分支,它将由Jenkins构建。 Next step is remembering that when you review, the pull request better be named "xyz..._build" :) 下一步要记住,当您查看时,拉取请求最好命名为“ xyz ..._ build” :)

Best, Anders 最好的,安德斯

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. 根据https://jenkins-ci.org/blog/2015/12/03/pipeline-as-code-with-multibranch-workflows-in-jenkins/,您应该能够根据创建的内容创建和删除作业和删除分支。

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/ 您可能还想阅读https://blog.codecentric.de/zh/2015/04/generated-jenkins-jobs-and-automatic-branch-merging-for-feature-branches/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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