首页 > 解决方案 > Jenkins Pipeline:如何轮询管道中的特定分支

问题描述

我正在研究 Jenkins,我正在定义一个管道。在管道中,我有一个代码构建阶段,它将为我编译项目。我需要为特定分支(开发)轮询 SCM。我无法弄清楚如何使用轮询来做到这一点。我无法使用 webhook,因为我们的 Jenkins 实例位于公司网络后面,无法通过 github 访问。如果从 Jenkins 打开连接,那么是的,github 可以交谈。

我在构建触发器中看不到任何分支选项。我在管道中遗漏了什么吗?

在此处输入图像描述

所以这些是构建触发器。我推动了发展,但民意调查日志显示没有推动任何事情。我如何轮询开发?谢谢你。

在管道脚本中,我还添加了轮询机制,但没有运气:

   stage('Preparation') {
            git (
              poll: true,
              branch: 'origin/develop',
              credentialsId: 'ID',
              url: 'https://github.com/company/reponame.git'
            )
        }

标签: jenkinsjenkins-pipeline

解决方案


你的 jenkinsfile 应该是这样的

pipeline {

agent any

triggers {
  cron('*/30 * * * *')  <-----This will run every 30 min and will pull
}

stages {

  stage('Git Checkout') {
    steps {
      checkout([$class: 'GitSCM',
      branches: [[name: "*/yourbranchname"]],   <----This branch
      doGenerateSubmoduleConfigurations: false,
      submoduleCfg: [],
      userRemoteConfigs: [
        [credentialsId: "ID",
          url: "yourgitrepo url"
        ]
      ]
    ])
  }
 }
}

推荐阅读