简体   繁体   English

Jenkins 和 Github 从特定分支触发 PUSH

[英]Jenkins and Github trigger PUSH from specific branch

I set up the Github webhook in Jenkins and I created a job with the following configuration:我在 Jenkins 中设置了 Github webhook,并使用以下配置创建了一个作业:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

However, every time I push something from any branch, the job build a new version (using the branch which I specified).但是,每次我从任何分支推送某些内容时,该作业都会构建一个新版本(使用我指定的分支)。

I set up the job by reading some related questions on Stackoverflow but all those questions are quite old and without a clear solution:我通过阅读 Stackoverflow 上的一些相关问题来设置工作,但所有这些问题都很老,没有明确的解决方案:

GitHub WebHooks triggered globally instead of per branch GitHub WebHooks 全局触发而不是每个分支

Trigger Jenkins job on GitHub PUSH to specific branch 在 GitHub 上触发 Jenkins 作业推送到特定分支

Is it possible to trigger Push only from a specific branch?是否可以仅从特定分支触发推送?

You are using a GitHub Webhook to trigger a preconfigured job.您正在使用 GitHub Webhook 来触发预配置的作业。 Your Job has the Branch "JENKINS-TEST-AUTODEPLOYMENT" hardcoded, so it will always build this branch only, no matter which branch you are pushing on GitHub.您的工作具有硬编码的分支“JENKINS-TEST-AUTODEPLOYMENT”,因此无论您在 GitHub 上推送哪个分支,它都会始终只构建此分支。

How about using GitHub Branch Source Plugin ?使用GitHub Branch Source Plugin怎么样? With this Plugin, Hooks will be auto-created in github for the corresponding repos.使用此插件,将在 github 中为相应的 repos 自动创建 Hook。 Each Branch will have a job in jenkins, which is also auto-created as long as you have a Jenkinsfile inside your Repo.每个分支都会在 jenkins 中有一个工作,只要你的 Repo 中有一个 Jenkinsfile,它也会自动创建。 With this your desired state should be easily achievable.有了这个,你想要的状态应该很容易实现。

Finally I did it by using Multibranch Pipeline .最后我使用Multibranch Pipeline做到了。

  • In Branch Sources:在分支源中:

    1. Add credentials and repository URL.添加凭据和存储库 URL。
    2. In Behaviours add: Discover branches: all branches.在行为中添加: 发现分支:所有分支。
    3. Filter by name (with regular expression): for example, ^master|dev|release.按名称过滤(使用正则表达式):例如, ^master|dev|release。 $* $*
  • In Build Configuration add the path to the Jenkinsfile.在构建配置中添加 Jenkinsfile 的路径。

So the job will scan for all the branches of the repository that match with the regular expression.因此该作业将扫描与正则表达式匹配的存储库的所有分支。

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

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