Jenkins在Jenkinsfile中使用导入

时间:2019-01-10 13:58:34

标签: jenkins

如何在import中使用Jenkinsfile语句?

这是导入语句:

import hudson.model.*
import jenkins.model.*
import hudson.tasks.test.AbstractTestResultAction

我想在AbstractTestResultAction部分中使用script

1 个答案:

答案 0 :(得分:0)

我以这种方式解决了它-Jenkinsfile的样子:

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import hudson.tasks.test.AbstractTestResultAction;

@NonCPS
  def getTestSummary = { ->
    def testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
    def summary = ""

    if (testResultAction != null) {
        def total = testResultAction.getTotalCount()
        def failed = testResultAction.getFailCount()
        def skipped = testResultAction.getSkipCount()

        summary = "Test results:\n\t"
        summary = summary + ("Passed: " + (total - failed - skipped))
        summary = summary + (", Failed: " + failed + " ${testResultAction.failureDiffString}")
        summary = summary + (", Skipped: " + skipped)
    } else {
        summary = "No tests found"
    }
    return summary
  }

pipeline {

    ... 

 post {
      always {
          script {
              def testSummaryRaw = getTestSummary()
              def testSummary = "`${testSummaryRaw}`"
              ...
          }
      }
    }

    ... 

}