【问题标题】:Jenkins pipelines from SCM in different branch not working来自不同分支的 SCM 的 Jenkins 管道不起作用
【发布时间】:2018-08-16 23:06:24
【问题描述】:

我在 Jenkins 中使用管道的环境已经配置了来自 SCM 的管道脚本,该脚本然后使用 groovy 文件来处理管道内的阶段/作业。该脚本位于主分支的 Bitbucket 上。

每次 jenkins 作业启动时,它都会调用 master 分支,它运行没有问题,并且运行管道的各个阶段。

现在我在 bitbucket 上创建了一个新分支并修改了 groovy 文件以包含更多步骤(例如运行单元测试和更多内容),我希望 jenkins 运行该脚本,但使用我指定的分支(我创建)。

即使我在“分支说明符”中指定了我的分支,jenkins 仍然运行主分支。以下是我配置的一些图片。
如何指定要在 SCM 的管道脚本上运行的分支?

Lightweight checkout support not available, falling back to full checkout.
Checking out git git@bitbucket.xxxxxx/xxxxxx.git into /data/jobs/extractor-pipeline-test-dev/workspace@script to read extractor-dev/Jenkinsfile
Cloning the remote Git repository
Cloning repository git@bitbucket.org:xxxxxx/xxxxxxxxxx.git
 > /usr/bin/git init /data/jobs/extractor-pipeline-test-dev/workspace@script # timeout=10
Fetching upstream changes from git@bitbucket.org:xxxx/xxxxxx.git
 > /usr/bin/git --version # timeout=10
 > /usr/bin/git fetch --tags --progress git@bitbucket.org:xxxxxx/deploy.git +refs/heads/*:refs/remotes/origin/*
 > /usr/bin/git config remote.origin.url git@bitbucket.org:xxxxx/deploy.git # timeout=10
 > /usr/bin/git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /usr/bin/git config remote.origin.url git@bitbucket.org:xxxxxxx/deploy.git # timeout=10
Fetching upstream changes from git@bitbucket.org:xxxxx/deploy.git
 > /usr/bin/git fetch --tags --progress git@bitbucket.org:grydev/gp_deploy.git +refs/heads/*:refs/remotes/origin/*
**Seen branch in repository origin/DEVOPS-568-pipeline-ci
Seen branch in repository origin/dev
Seen branch in repository origin/master**
Seen 3 remote branches
 > /usr/bin/git tag -l # timeout=10
Checking out Revision e3270789a8181b26464f878bfccdf39b3fdabcb0 (master)
Commit message: " ....."
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f e3270789a8181b26464f878bfccdf39b3fdabcb0
 > /usr/bin/git rev-list e3270789a8181b26464f878bfccdf39b3fdabcb0 # timeout=10

这是 groovy 文件,但 groovy 文件会执行将要部署的代码的步骤。它不运行任何詹金脚本。它说“master”的地方是要部署的maser代码,而不是部署脚本。

Groovy 文件:

def call(body) {

    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    def artifactName = 'imp'
    def artifactExt = '.war'
    def artifactVersion = '0.0.1'

    def buildPath = 'target/'
    def warFile = artifactName + '-' + artifactVersion + artifactExt
    def warPath = buildPath + warFile
    def warNoVersion = artifactName + artifactExt

    def deployPath = '/var/lib/tomcat8/webapps/'
    def deployFile = deployPath + warNoVersion

    node {
        // Clean workspace before doing anything
        //deleteDir()

        try {

            stage ('Code Checkout') {
                git branch: 'master',
                    credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                    url: 'ssh://git@bitbucket.org/xxxxx/xxxximporter'

【问题讨论】:

  • 一切看起来都很好...您验证了控制台输出中的提交 ID 吗?...您可以通过隐藏机密信息来共享控制台输出...
  • 在那里我编辑并添加了它。可以看到 3 个分支,但是 master 正在运行。我希望该工作运行我的分支“DEVOPS-568-pipeline-ci”
  • 我包括开始运行主分支作业的部分。然后作业成功运行,管道正常结束,但只有主分支
  • 你能分享一下“checkout scm”发生的管道脚本吗?
  • 脚本只包含这个:@Library("gp-extractor-deploy") _ DeployExtractor { confiFileId = "extractor-conf.properties" serverIp = 'xxxx' slackChannel = '#xxxxx' } 其中调用一个运行所有作业/步骤的 groovy 文件

标签: jenkins groovy jenkins-pipeline


【解决方案1】:

问题是即使 Jenkinsfile 来自所需的分支,代码签出也是通过“master”分支进行的。 原因是代码签出来自“代码签出阶段”中的“master”分支。修改代码如下:

try {
    stage ('Code Checkout') {
        git branch: 'REQUIRED BRANCH',
            credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            url: 'ssh://git@bitbucket.org/xxxxx/xxxximporter'

另一种更好的选择是提供 GIT BRANCH 作为 Jenkins 作业的参数。截图如下。

更新: 这可以通过安装git parameter plugin.

来实现

并在您的“代码签出”阶段添加以下 code sn-p 并进行相应更改。这里 "gitbranch" 是您从 build 传递的参数

 checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: gitbranch]], doGenerateSubmoduleConfigurations: false,    
extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '********', url: '**********']]]

【讨论】:

  • ry { stage ('Code Checkout') { git branch: 'REQUIRED BRANCH', credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx', url: 'ssh://git@bitbucket.org/xxxxx/xxxximporter' this code 是要部署的代码,而不是进行部署的脚本的代码。我会检查你建议的第二部分
  • 另一种更好的选择是提供 GIT BRANCH 作为 Jenkins 作业的参数。下面的快照。为什么我在 Jenkins 中没有那个选项?这是插件还是我必须添加的东西?谢谢
  • @Guillermo-Casco :如果新的更新有效,请将答案标记为已接受/支持答案,因为它可以在其他人面临类似问题时帮助他们..
  • 还没有,我在上周五工作,并添加了新配置和新插件,并被建议,工作仍在主服务器上运行。我正在检查 Jenkins 配置,看看是否有不同之处。会检查并通知您。
  • 仍在运行 master :( 它看到 3 个分支,但只运行 master。这是我现在配置的图像。从 git@bitbucket.org:xxxxxx/deploy.git 获取上游更改 > / usr/bin/git --version # timeout=10 > /usr/bin/git fetch --tags --progress git@bitbucket.org:xxxxxx/deploy.git +refs/heads/*:refs/remotes/origin/ * 在存储库 origin/DEVOPS-568-pipeline-ci 中看到分支 在存储库 origin/dev 中看到分支 在存储库 origin/master 中看到分支 看到 3 个远程分支 > /usr/bin/git show-ref --tags -d # timeout= 10 签出修订版 ccdf39b3fdabcb0 (master)
【解决方案2】:

对我来说,上面提到的任何一种方法都没有帮助。

似乎 Git 参数插件将分支作为 'origin/<...>' 传递,它不会根据日志转换为有效的 refspec:

> git fetch --tags --force --progress -- ssh://git@***************/group/repo.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse "refs/remotes/origin/origin/devel^{commit}" # timeout=10
> git rev-parse "refs/remotes/origin/refs/heads/origin/devel^{commit}" # timeout=10
> git rev-parse "refs/heads/origin/devel^{commit}" # timeout=10

我无法将 Jenkinsfile 与“refs/tags/${TAG}”一起使用。

此外,当未选中“轻量级结帐”时,作业总是失败并显示“未找到”<...>@script\Jenkinsfile。

【讨论】:

    【解决方案3】:

    我在这里遇到了完全相同的问题,并尝试了很多次。原来,script path中指定的Jenkinsfile,只是从git默认url获取的,千万不要使用我们在Branch to build指定的分支。

    为了避免这种情况,我必须使用多个分支管道......

    【讨论】:

      【解决方案4】:

      在“分支说明符”中单击“添加分支”并再次放置您选择的分支。即使使用环境变量,这也对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多