使用Git轮询的Jenkins管道无法运行

时间:2019-03-13 22:38:03

标签: git jenkins jenkins-pipeline

我有一个带有分支参数的Jenkins作业,设置为每5分钟轮询一次SCM,然后从SCM运行管道脚本:

General and build triggers configuration

Pipeline configuration

脚本执行的第一件事是删除先前的工作空间并获得源代码的新副本:

#!/usr/bin/env groovy
node {
    try {
        stage('Get Source') {
            // Clear the workspace
            deleteDir()

            // Get the source code
            checkout scm
        }

        // Stages for building and running unit tests...
    }
}

根据Git轮询日志,它每5分钟检查一次存储库,但未发现任何更改:

Started on Mar 13, 2019 4:29:34 PM
Using strategy: Default
[poll] Last Built Revision: Revision 47251333f2d6c740275f24dd667255e66f7b5665 (refs/remotes/origin/master)
using credential **********
 > git --version # timeout=10
using GIT_SSH to set credentials Jenkins SSH Authentication Key
 > git ls-remote -h git@bitbucket.org:myuser/myrepo.git # timeout=10
Found 1 remote heads on git@bitbucket.org:myuser/myrepo.git
Using strategy: Default
[poll] Last Built Revision: Revision 47251333f2d6c740275f24dd667255e66f7b5665 (refs/remotes/origin/master)
using credential **********
 > git --version # timeout=10
using GIT_SSH to set credentials Jenkins SSH Authentication Key
 > git ls-remote -h git@bitbucket.org:myuser/myrepo.git # timeout=10
Found 1 remote heads on git@bitbucket.org:myuser/myrepo.git
Done. Took 1.8 sec
No changes

但是,在47251333f2d6c740275f24dd667255e66f7b5665之后,还有一些其他提交已被推送到远程主分支。

我读到here,该作业必须手动运行一次才能开始SCM轮询,但是我已经手动运行了几次。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

我想我已经解决了问题。因为我的管道脚本配置中的分支说明符是*/${BRANCH},所以我必须在结帐步骤中指定它:

#!/usr/bin/env groovy
node {
    try {
        def repo = 'dice-seeker-android'
        def branch = params.Branch
        def credentialsID = params.CredentialsID

        stage('Get Source') {
            // Clear the workspace
            deleteDir()

            // Get the source code
            checkout([
                $class: 'GitSCM',
                branches: [[
                    name: '*/' + branch
                ]],
                extensions: [[
                    $class: 'RelativeTargetDirectory',
                    relativeTargetDir: repo
                ]],
                userRemoteConfigs: [[
                    credentialsId: credentialsID,
                    url: 'git@bitbucket.org:myuser/' + repo + '.git'
                ]]
            ])
        }

        // Stages for building and running unit tests...
    }
}

这意味着我还必须包括使用我的SSH密钥的凭据参数。

最后,我不得不手动运行一次作业。现在似乎正在接受更改。

如果有人有更好的解决方案,需要更少的代码,那么我仍然会对听到它感兴趣。

答案 1 :(得分:0)

脚本的第一步是删除工作区并获取更新的工作区(包含所有新提交),所以我想它已经更新了。这就是为什么它不检测您的提交的原因。

相关问题