为什么 stash/unstash 在此 Jenkinsfile 中不起作用?

Why does stash/unstash not work in this Jenkinsfile?

我有一个 Jenkins 服务器 运行ning 现场,它使用 Jenkinsfile 来管理一个管道,该管道使用并行测试执行器插件 运行 我在几个代理上的所有 JUnit 测试以加快测试速度。我们有一台自己制造的 blade 服务器(比购买一台便宜多了!),它将我们的测试时间从近 2 小时缩短到 22 分钟。 JUnit 插件非常适合并行测试。

然而 Jacoco 插件没有。所以我试图将覆盖率文件合并到一个文件中,以便 Jacoco 插件可以发布覆盖率结果。 Stash/unstash 正在存储源代码,但当我尝试存储不同的 Jacoco 输出文件以将它们取消存储在主服务器上时,它不起作用。

知道为什么吗?

这是我的 Jenkins 文件:

#!/usr/bin/env groovy

def branch
def hash

node('remote') {
  sh 'echo starting'

  branch = env.gitlabBranch ?: '**'
  echo "Branch: $branch"

  checkout([$class: 'GitSCM',
        branches: [[name: "$branch"]],
        extensions: [
          [$class: 'PruneStaleBranch'],
          [$class: 'CheckoutOption', timeout: 120],
          [$class: 'CloneOption', depth: 0, noTags: true, shallow: true, timeout: 180]
        ],
        doGenerateSubmoduleConfigurations: false,
        submoduleCfg: [],
        userRemoteConfigs: [[credentialsId: 'gitlabLabptop', url: 'git@gitlab.com:protocase/my_project_url.git']]
       ]
      )

  hash = sh (script: 'git rev-parse HEAD', returnStdout: true).trim()

  ### - this stash works fine -###
  stash name: 'sources', includes: '**', excludes: '**/.git,**/.git/**'
}

def numBranches = 9
def splits = splitTests count(numBranches)
def branches = [:]

for (int i = 0; i < splits.size(); i++) {
  def index = i // fresh variable per iteration; i will be mutated

  branches["split${i}"] = {
    timeout(time: 125, unit: 'MINUTES') {
      node('remote') {
    sh 'echo starting a node'
    deleteDir()

    ### - this unstash works fine - ###
    unstash 'sources'

    def exclusions = splits.get(index);
    writeFile file: 'test/exclusions.txt', text: exclusions.join("\n")

    sh 'ant clean'

    sh 'rm -rf build'

    sh 'ant jar'

    sh 'ant -buildfile build-test.xml buildTests'

    sh 'ant -buildfile build-test.xml jenkinsBatch'

    junit 'build/test/results/*.xml'

    sh "mv build/test/jacoco/jacoco.exec build/test/jacoco/jacoco${index}.exec"
    echo "name: coverage$index, unclude jacoco${index}"

       ### - this stash appears to work - ### 
       stash name: "coverage$index", includes: "build/test/jacoco/jacoco${index}.exec"
       echo "stashed"

      }
    }
  }
}

parallel branches


def branchIndecis = 0..numBranches

node('master') {
  if (currentBuild.result != "ABORTED") {

    echo "collecting exec files"

    branchIndecis.each {
      echo "unstash coverage${it}"

      ### !!! this unstash causes an error !!! ###
      unstash name: "coverage${it}"



      echo "make file name"
      def coverageFileName = "build/test/jacoco/jacoco${it}.exec"
      echo "merge file"
      sh "ant -buildfile build-test.xml -Dfile=${coverageFileName} coverageMerge"
    }

    echo "collected exec files"

    step([$class: 'JacocoPublisher',
      execPattern:'build/test/jacoco/jacoco.exec',
      classPattern: 'build/classes',
      sourcePattern: 'src'])

    echo "finishing ${branch} - ${hash}"

  }
}

我得到的输出是:

[split7] [jdesigner] Running shell script
[split7] + mv build/test/jacoco/jacoco.exec build/test/jacoco/jacoco7.exec
[Pipeline] [split7] echo
[split7] name: coverage7, unclude jacoco7
[Pipeline] [split7] stash
[split7] Stashed 1 file(s)
[Pipeline] [split7] echo
[split7] stashed
[Pipeline] [split7] }
[Pipeline] [split7] // node
[Pipeline] [split7] }
[Pipeline] [split7] // timeout
[Pipeline] [split7] }
[Pipeline] // parallel
[Pipeline] node
Running on eightyeight in /var/jenkins/workspace/jdesigner
[Pipeline] {
[Pipeline] echo
collecting exec files
[Pipeline] echo
unstash coverage0
[Pipeline] unstash
[Pipeline] }
[Pipeline] End of Pipeline
Finished: FAILURE

[编辑] coverage0 的存储是

[split0] Recording test results
[Pipeline] [split0] sh
[split0] [jdesigner] Running shell script
[split0] + mv build/test/jacoco/jacoco.exec build/test/jacoco/jacoco0.exec
[Pipeline] [split0] echo
[split0] name: coverage0, include jacoco0
[Pipeline] [split0] stash
[split0] Stashed 1 file(s)
[Pipeline] [split0] echo
[split0] stashed
[Pipeline] [split0] }
[Pipeline] [split0] // node
[Pipeline] [split0] }
[Pipeline] [split0] // timeout
[Pipeline] [split0] }
[split3]     [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 18.737 sec
[split3]     [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 18.737 sec

注意行

[split0] name: coverage0, include jacoco0

只是我的 echo 语句,我在其中回显了脚本这一部分的名称:

    sh "mv build/test/jacoco/jacoco.exec build/test/jacoco/jacoco${index}.exec"
    echo "name: coverage$index, include jacoco${index}"

    stash name: "coverage$index", includes: "build/test/jacoco/jacoco${index}.exec"
    echo "stashed"

请注意,实际的存储不是在节点上完成的,它被列为管道,即使它是在远程节点上完成的。我看到的东西表明存储是在主服务器上完成的,但实际上不是该目录所在的位置。

[[进一步编辑]] - 感谢 eis 的建议。

master 上的 jobs/jdesigner/builds/1639/stashes/ 目录覆盖#.tar.gz 文件,其中包括适当的 jacoco#.exec 文件。当我尝试捕获 unstash 时:

try {
    unstash name: "coverage${it}"
} catch (error) {
    echo "error unstashing: ${error}"
}

我得到的输出是:

collecting exec files
[Pipeline] echo
unstash coverage0
[Pipeline] unstash
[Pipeline] echo
error unstashing: java.io.NotSerializableException: groovy.lang.IntRange
[Pipeline] echo
make file name

TLDR:这是迭代样式导致问题的一个案例,因为使用的键 it 不是 Serializable.

导致此问题难以调试的原因是错误消息未正确报告,可能是由于 this issue。在代码中捕获异常并 "manual" 报告解决了这个问题。

实际问题已通过使用 Serializable 键解决。


更长的版本:

因为在你的例子中这是可行的:

node('remote') {
    ### - this stash works fine -###
    stash name: 'sources', includes: '**', excludes: '**/.git,**/.git/**'
}
node('remote') {    
    ### - this unstash works fine - ###
    unstash 'sources'
}

但事实并非如此:

node('remote') {

   ### - this stash appears to work - ### 
   stash name: "coverage$index", includes: "build/test/jacoco/jacoco${index}.exec"
   echo "stashed"

}
node('master') {
   echo "unstash coverage${it}"

   ### !!! this unstash causes an error !!! ###
   unstash name: "coverage${it}"
}

我最初认为工作的是在你的远程节点上隐藏和取消隐藏的,而不工作的是隐藏在你的远程节点上但你试图在你的主节点上取消它(它自然不会在那里)找到)。

然而,事实并非如此。根据this

When you stash a file on a slave, the files are send to the master. The files will be stored in the Job folder, in the associated build folder under the stash folder. Each stash will be stored as a tar file. These files are deleted at the end of the build.

所以主远程分离应该不会有什么不同。此外,如果是关于找不到藏匿处,您可以 see from the sources that it would fail with "No such saved stash ‘" + name + "’, since according to AbortException javadoc "When this exception is caught, the specified message will be reported."。这显然不会发生。

相反,应该使用 try-catch 块进行调试,以找出破坏构建的真正异常是什么。

至于为什么默认不正确报告,有this issue:"Serialization error at end of flow not reported properly in build log, only Jenkins log"。错误报告声称它 "fixed" 但显然只是因为在新版本中,一些 对此行为的测试没有触发问题,所以它可能仍然存在。

捕获到错误消息后,可以看出问题出在 - 我们在传递它时试图序列化一个不可序列化的密钥。

在 Jenkins 中使用并行进程时,有两种可能性让我印象深刻:

  1. 您可能试图在 stash name: "coverage$index", includes: "build/test/jacoco/jacoco${index}.exec"master 节点

    上完成之前取消存储在一个(或多个)进程中
  2. 进程之间可能存在名称争用。

解释(2):

进程 1 创建一个名为 stashed_files

的存储

处理 2 个相同名称的存储,stashed_files,然后成功取消存储。

进程 1 尝试取消存储 stashed_files。它在取消存储期间出错,因为 stashed_files 被进程 2 覆盖。我也遇到过进程 2 无法再找到 stashed_files 的情况,因为进程 1 管道已完成并且其存储项已被删除(请参阅 here 进行解释。)

可以找到一些有用的 Groovy 代码来解决这个问题 in this question