如何在推送到掌握时使用 Git 触发 Jenkins CI?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5784329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:45:42  来源:igfitidea点击:

How can I make Jenkins CI with Git trigger on pushes to master?

gitgithubjenkins

提问by Ziggy

I'm trying to set up Jenkins-ci for a project using GitHub. I've already set up Jenkins with the appropriate plugins. I want Jenkins to run build scripts only whenever someone on the project pushes to master. So far I've been able to set it up so that a build will be triggered anytime anyone pushes to anywhere, but that is too broad. I've done this with post-receive service hooks on Git.

我正在尝试使用 GitHub 为项目设置 Jenkins-ci。我已经使用适当的插件设置了 Jenkins。我希望 Jenkins 仅在项目中有人推动掌握时才运行构建脚本。到目前为止,我已经能够对其进行设置,以便在任何人推送到任何地方时都会触发构建,但这太宽泛了。我已经在 Git 上使用 post-receive 服务挂钩完成了这项工作。

I've read the Jenkins wiki, and a couple of tutorials, but this particular detail is missing... is it something to do with polling maybe? Or should work be done on the Git side, so that Git only triggers Jenkins when masteris changed?

我已经阅读了 Jenkins wiki 和一些教程,但是缺少这个特定的细节......这可能与轮询有关吗?还是应该在 Git 端完成工作,以便 Git 仅在master更改时触发 Jenkins ?

采纳答案by olenz

As already noted by gezzedin his comment, meanwhile there is a good solution (described in Polling must die: triggering Jenkins builds from a Git hook):

正如gezzed在他的评论中已经指出的那样,同时有一个很好的解决方案(在Polling must die: triggering Jenkins builds from a Git hook 中描述):

  • Set the Jenkins job's build trigger to Poll SCM, but do notspecify a schedule.

  • Create a GitHub post-receive trigger to notify the URL

    http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>?token=<get token from git to build remotely>
    
  • This will trigger all builds that poll the specified Git repository.

  • However, polling actually checks whether anything has been pushed to the used branch.

  • 将 Jenkins 作业的构建触发器设置为Poll SCM,但指定时间表。

  • 创建一个 GitHub post-receive 触发器来通知 URL

    http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>?token=<get token from git to build remotely>
    
  • 这将触发所有轮询指定 Git 存储库的构建。

  • 然而,轮询实际上检查是否有任何东西被推送到使用的分支。

It works perfectly.

它完美地工作。

回答by docwhat

As of version 0.5, the GitHub plugin for Jenkins can trigger a build when a change is pushed to GitHub.

从 0.5 版本开始,Jenkins 的 GitHub 插件可以在将更改推送到 GitHub 时触发构建

回答by Wei Wang

Instead of triggering buildsremotely, change your Jenkins project configuration to trigger builds by polling.

不是远程触发构建,而是更改您的 Jenkins 项目配置以通过轮询触发构建。

Jenkins can poll based on a fixed internal, or by a URL. The latter is what you want to skip builds if there are not changes for that branch. The exact details are in the documentation. Essentially you just need to check the "Poll SCM" option, leave the schedule section blank, and set a remote URL to hit JENKINS_URL/job/name/polling.

Jenkins 可以根据固定的内部或 URL 进行轮询。如果该分支没有更改,则后者是您要跳过构建的内容。确切的细节在文档中。本质上,您只需要选中“Poll SCM”选项,将计划部分留空,然后设置一个远程 URL 以点击 JENKINS_URL/job/name/polling。

One gotcha if you have a secured Jenkins environment is unlike /build, the /pollingURL requires authentication. The instructions herehave details. For example, I have a GitHub Post-Receive hook going to username:apiToken@JENKIS_URL/job/name/polling.

如果您有一个安全的 Jenkins 环境/build,一个问题与 不同,/pollingURL 需要身份验证。这里的说明有详细信息。例如,我有一个 GitHub Post-Receive 挂钩转到username:apiToken@JENKIS_URL/job/name/polling.

回答by etusm

For GitLab, use these steps:

对于GitLab,请使用以下步骤:

  1. Go to your project's settings → Web hooks
  2. Enter the "Build Now" URL from your Jenkins project as a Push Event URL:

    http://server.com/jenkins/job/project_name/build?delay=0secfor example

  3. Click Add Web Hookand then test hook
  1. 转到您的项目设置 → Web hooks
  2. 输入 Jenkins 项目中的“立即构建”URL 作为推送事件 URL:

    http://server.com/jenkins/job/project_name/build?delay=0sec例如

  3. 点击Add Web Hook然后test hook

Then any time you commit to the repository, the web hook is triggered and a build is created. Be sure to set your Jenkins workspace to delete workspace before each buildso you get a fresh copy of the new code.

然后,无论何时提交到存储库,都会触发 Web 挂钩并创建构建。请务必将您的 Jenkins 工作区设置为 ,delete workspace before each build以便您获得新代码的全新副本。

回答by Krishnam

Not related to Git, but below I will help with the Jenkins job configuration in detail with Mercurial. It may help others with a similar problem.

与 Git 无关,但下面我将使用 Mercurial 详细帮助 Jenkins 作业配置。它可能会帮助其他有类似问题的人。

  1. Install the URL Trigger Plugin
  2. Go to the job configuration page and select Poll SCMoption. Set the value to * * * * *
  3. Check the option: [URLTrigger] - Poll with a URL. Now you can select some options like modification date change, URL content, etc.
  4. In the options, select URL content change, select first option – Monitor change of content
  5. Save the changes.
  1. 安装 URL 触发器插件
  2. 转到作业配置页面并选择Poll SCM选项。将值设置为* * * * *
  3. 勾选选项:[URLTrigger] - Poll with a URL。现在您可以选择一些选项,例如修改日期更改、URL 内容等。
  4. 在选项中,选择 URL 内容更改,选择第一个选项 - Monitor change of content
  5. 保存更改。

Now, trigger some change to the Mercurial repository by some test check-ins.

现在,通过一些测试签入触发对 Mercurial 存储库的一些更改。

See that the Jenkins job now runs by detecting the SCM changes. When the build is run due to Mercurial changes, then, you will see text Started by an SCM change. Else, the user who manually started it.

通过检测 SCM 更改来查看 Jenkins 作业现在正在运行。当由于 Mercurial 更改而运行构建时,您将看到 text Started by an SCM change。否则,手动启动它的用户。

回答by Adam Dymitruk

You need to specify the branch. By default it listens to anything. See the blog post Hudson: Git and Maven plugins.

您需要指定分支。默认情况下,它会收听任何内容。请参阅博客文章Hudson:Git 和 Maven 插件

回答by Nav

I hope this helps: How to trigger a Jenkins build on Git commit

我希望这会有所帮助:如何在 Git 提交上触发 Jenkins 构建

It's just a matter of using curl to trigger a Jenkins job using the Git hooks provided by Git.

这只是使用 curl 使用 Git 提供的 Git 钩子触发 Jenkins 作业的问题。

The command curl http://localhost:8080/job/someJob/build?delay=0seccan run a Jenkins job, where someJobis the name of the Jenkins job.

该命令curl http://localhost:8080/job/someJob/build?delay=0sec可以运行一个 Jenkins 作业,其中 Jenkins 作业someJob的名称。

Search for the "hooks" folder in your hidden .git folder. Rename the "post-commit.sample" file to "post-commit". Open it with Notepad, remove the ": Nothing" line and paste the above command into it.

在隐藏的 .git 文件夹中搜索“hooks”文件夹。将“post-commit.sample”文件重命名为“post-commit”。用记事本打开它,去掉“:Nothing”行,然后把上面的命令粘贴进去。

That's it. Whenever you do a commit, Git will trigger the post-commit commands defined in the file.

就是这样。每当您进行提交时,Git 都会触发文件中定义的提交后命令。

回答by Venkata Naresh Babu

Continuous Integration with Jenkins, after code is pushed to repository from Git command/ GUI:

与 Jenkins 的持续集成,在代码从 Git 命令/ GUI 推送到存储库之后:

  1. Create a job in Jenkins with only job name and select type of the project freestyle. Click OK. The next page doesn't add anything - just click Save.
  2. Go to the your local Git repository where you have the source code and navigate to the .git/hooksfolder.
  3. The hooksfolder contains the few files. Check for the "post-commit". If not present, create a file, "post-commit" without a file extension:

    C:\work\test\.git\hooks\post-commit
    
  4. Edit the "post-commit" file with the below command. Make sure it is present in your local source code hooks folder.

    curl -u userName:apiToken -X POST http://localhost:8080/jenkins/job/jobName/build?token=apiToken
    

    Example:

    curl -u admin:f1c55b3a07bb2b69b9dd549e96898384 -X POST http://localhost:8080/jenkins/job/Gitcommittest/build?token=f1c55b3a07bb2b69b9dd549e96898384
    

    5.

    userName: Jenkins user name

    jobName: Job name of the build

    apiToken: To get your API token, go to your Jenkins user page (top right in the interface). It is available in the "Configure" menu on the left of the page: "Show API token"

  5. Make changes in your source code and commit the code to repository.

  6. Your job, http://localhost:8080/jenkins/job/Gitcommittest/, should be building.

  1. 在 Jenkins 中创建一个只有作业名称的作业,并选择项目自由式的类型。单击OK。下一页不添加任何内容 - 只需单击Save
  2. 转到您拥有源代码的本地 Git 存储库并导航到该.git/hooks文件夹。
  3. hooks文件夹包含几个文件。检查“提交后”。如果不存在,请创建一个没有文件扩展名的“提交后”文件:

    C:\work\test\.git\hooks\post-commit
    
  4. 使用以下命令编辑“提交后”文件。确保它存在于您的本地源代码 hooks 文件夹中。

    curl -u userName:apiToken -X POST http://localhost:8080/jenkins/job/jobName/build?token=apiToken
    

    例子:

    curl -u admin:f1c55b3a07bb2b69b9dd549e96898384 -X POST http://localhost:8080/jenkins/job/Gitcommittest/build?token=f1c55b3a07bb2b69b9dd549e96898384
    

    5.

    userName: 詹金斯用户名

    jobName: 构建的作业名称

    apiToken:要获取您的 API 令牌,请转到您的 Jenkins 用户页面(界面右上角)。它在页面左侧的“配置”菜单中可用:“显示 API 令牌”

  5. 在源代码中进行更改并将代码提交到存储库。

  6. 你的工作,http://localhost:8080/jenkins/job/Gitcommittest/应该是建筑。

回答by Tomas Bjerre

Generic Webhook Trigger Plugincan be configured with filters to achieve this.

通用 Webhook 触发器插件可以配置过滤器来实现这一点。

When configured with

当配置为

  • A variable named refand expression $.ref.
  • A filter with text $refand filter expression like ^refs/heads/master$.
  • 一个名为ref和表达式的变量$.ref
  • 带有文本$ref和过滤器表达式的过滤器,如^refs/heads/master$.

Then that job will trigger for every push to master. No polling.

然后该作业将在每次推送到 时触发master。没有投票。

You probably want more values from the webhook to actually perform the build. Just add more variables, with JSONPath, to pick what you need.

您可能需要来自 webhook 的更多值来实际执行构建。只需使用 JSONPath 添加更多变量即可选择您需要的内容。

There are some use cases here: https://github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd

这里有一些用例:https: //github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd

回答by jesses.co.tt

In my current organization, we don't do this in master but do do it on both develop and release/ branches (we are using Git Flow), in order to generate snapshot builds.

在我目前的组织中,我们不在 master 中执行此操作,而是在开发和发布/分支上执行此操作(我们使用 Git Flow),以生成快照构建。

As we are using a multi branch pipeline, we do this in the Jenkinsfile with the when{}syntax...

由于我们使用的是多分支管道,因此我们在 Jenkinsfile 中使用when{}语法执行此操作...

stage {
    when { 
        expression { 
            branch 'develop'
        }
    }
}

This is detailed in this blog post: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/#longer-pipeline

这在这篇博文中有详细说明:https: //jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/#longer-pipeline