如何使用 Jenkins Pipeline 插件启用 SCM 轮询

How to enable SCM polling with the Jenkins Pipeline plugin

这是一个与 有关的问题。该线程回答了如何在管道脚本中使用 SCM 轮询 once SCM 轮询已启用,但未涵盖如何 enable SCM 轮询。

例如,如果您想使用mulit-branch pipeline plugin to create jobs automatically using a Jenkinsfile there is not a way I know of to have the "Poll SCM" option enabled in the job. This makes it difficult to provision on-demand environments such as creating a docker container that has the jobs setup from the beginning. Because you would have to sign-in to Jenkins and go to the configuration and select the "Poll SCM" option once the container was started. Cloudbees offers a template plugin来帮助解决这个问题。

但是,使用免费版本的 Jenkins 无法使用此功能。对于使用免费版 Jenkins 的用户,是否有任何解决方法或解决方案?

我也在想同样的问题

如果您使用在线 Git 服务,例如 GithubBitbucket,我认为您可以使用他们的 Webhooks 功能来解决它​​。我还不能测试该解决方案,但它应该有效。

在您的 Multibranch Pipeline 配置中,启用 Trigger builds remotely 选项。

然后您需要在您的存储库上启用您的 Github/Bitbucket Webhook,使用路径(如 Jenkins 配置描述中所述):JENKINS_URL/job/test/build?token=TOKEN_NAME

if you wanted to use the multi-branch pipeline plugin to create jobs automatically using a Jenkinsfile there is not a way I know of to have the "Poll SCM" option enabled in the job

也不需要。多分支项目对整个分支索引有一个可配置的轮询间隔,它也作为每个分支的构建触发器,并且也会自动接收 webhooks。

要回答如何启用 SCM 轮询的问题,您需要执行以下操作。

使用流水线语法生成器和 "properties: Set job properties" 您可以生成以下内容以启用 SCM 轮询。

properties([pipelineTriggers([pollSCM('H * * * *')])])

然而,正如 Jesse Glick 指出的那样,对于多分支管道,您不需要启用 SCM 轮询。

为了让我的 Bitbucket 连接到网络挂钩,我必须将以下内容添加到我的声明管道中:

pipeline {
    stages {
        stage('Initialize') {
            steps {
                //enable remote triggers
                script {
                    properties([pipelineTriggers([pollSCM('')])])
                }
                //define scm connection for polling
                git branch: BRANCH_NAME, credentialsId: 'my-credentials', url: 'ssh://git@stash.server.fqdn/stash/my-project.git'
            }
        }
    }
}

这允许重建分支,而无需扫描整个 multibranchiverse。这在使用 Bitbucket Project /Github Team-multibranch 项目时特别有价值。一旦你有几个repos/branches.

,扫描可能需要几分钟

通过直接连接到分支,您可以更快地获得构建结果,并且没有任何副作用。

注意:在声明性管道中,属性调用必须由脚本块包装。