【问题标题】:How to avoid Jenkins triggering on git commands within Jenkinsfile如何避免 Jenkins 触发 Jenkinsfile 中的 git 命令
【发布时间】:2017-07-26 01:14:57
【问题描述】:

每当我推送到远程分支时,Jenkins 都会触发。然后使用我的 Jenkinsfile 脚本运行管道。

在我 git push 标签的 jenkinsfile 中,这个命令会再次触发 Jenkins 并启动另一个构建,然后依次运行 git push 标签导致无限循环。

如何避免 jenkins 触发 jenkinsfile 中的 git 命令?
我花了一个多星期寻找解决方案,但似乎没有任何效果,谢谢。

【问题讨论】:

  • 你用的是哪个触发器插件?
  • 他正在使用jenkins-pipeline
  • 我将 jenkins-pipeline 与 git 插件一起使用。每当发生远程分支的推送事件或合并事件时,都会触发詹金斯

标签: jenkins jenkins-plugins jenkins-pipeline


【解决方案1】:

我假设您使用的是 Multibranch PipelineGitHub/Bitbucket 组织

您可以在 Branch Sources 配置下为所有或指定的分支抑制自动 SCM 触发

【讨论】:

  • 我使用的是管道而不是多分支管道,因此我没有选项来抑制自动 SCM 触发
  • 您为什么要使用它?我真的没有看到 Pipeline 作业类型的优势。
  • 这是我公司设置当前 jenkins 配置的方式。一个多星期以来,我一直在试图找到一个解决方案,以解决如何阻止 jenkins 从 jenkinsfile 触发我的标签推送命令。但到目前为止还没有运气。
  • 我决定创建一个多分支管道项目,但是如何保存特定分支的配置?
  • Jenkinsfile 中使用if 条件。您可以在env.BRANCH_NAME 中找到分支名称。
【解决方案2】:

通常,Jenkins 使用a (non-Jenkinsfile) refspec like refs/tags/* 触发构建

因此,为避免这种情况,您可以指定refs/heads/*(如seen here)或check issue 507,并取消选中“轻量级结帐”以避免获取标签。

【讨论】:

  • 但这与jenkins-pipelineJenkinsfiles 背后的技术)完全无关。
  • 我已经取消选中轻量级结帐框。至于 refs/heads/* 我会将该路径放在分支说明符框中还是作业配置的 refspec 框中?
  • @Nadim 分支说明符框(如果没有,也测试另一个)
  • 使用 refs/heads/* 时仍然会触发触发*你知道另一种方法可以让 Jenkins 触发除标签推送之外的所有推送吗?
【解决方案3】:

您可以使用Generic Webhook Trigger Plugin 来做到这一点。

有很多替代品,例如:

  • 如果提交消息具有特殊格式,则不会触发。
  • 如果特定用户推送提交,则不会触发。就像 Jenkins 中使用的特殊用户一样。

来自一个测试用例:

Scenario: Trigger a job when commit is pushed and the pusher is not named "build".

Given the following generic variables are configured:
  | variable   | expression    | expressionType  | defaultValue | regexpFilter  |
  | user       | $.pusher.name | JSONPath        |              |               |

Given filter is configured with:
  | text   | expression   |
  | $user  | ^((?!build)) |

Given received post content is:
"""
{
  "pusher": {
    "name": "baxterthehacker",
  }
}
"""
Then the job is triggered

Given received post content is:
"""
{
  "pusher": {
    "name": "build",
  }
}
"""
Then the job is not triggered

【讨论】:

    猜你喜欢
    • 2022-01-03
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2017-08-16
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多