【问题标题】:Jenkins - How do I publish coverage report to githubJenkins - 如何将覆盖率报告发布到 github
【发布时间】:2018-02-22 16:28:10
【问题描述】:

我正在尝试使用github-pr-coverage-status-plugin,文档说我需要在我的主分支上运行以下内容:

step([$class: 'MasterCoverageAction'])

但是当我将它添加到我的管道时,我收到以下错误:

java.lang.UnsupportedOperationException: Can't find GIT_URL or CHANGE_URL in envs: {BRANCH_NAME=master, BUILD_DISPLAY_NAME=#41, BUILD_ID=41, BUILD_NUMBER=41, BUILD_TAG=jenkins-testci-master-41, BUILD_URL=https://jnkns-ci.myserver.com/job/testci/job/master/41/, CLASSPATH=, HUDSON_HOME=/var/jenkins_home, HUDSON_SERVER_COOKIE=01f6aedeea333d1f, HUDSON_URL=https://jnkns-ci.myserver.com/, JENKINS_HOME=/var/jenkins_home, JENKINS_SERVER_COOKIE=01f6aedeea333d1f, JENKINS_URL=https://jnkns-ci.myserver.com/, JOB_BASE_NAME=master, JOB_DISPLAY_URL=https://jnkns-ci.myserver.com/job/testci/job/master/display/redirect, JOB_NAME=testci/master, JOB_URL=https://jnkns-ci.myserver.com/job/testci/job/master/, RUN_CHANGES_DISPLAY_URL=https://jnkns-ci.myserver.com/job/testci/job/master/41/display/redirect?page=changes, RUN_DISPLAY_URL=https://jnkns-ci.myserver.com/job/testci/job/master/41/display/redirect}
    at com.github.terma.jenkins.githubprcoveragestatus.PrIdAndUrlUtils.getGitUrl(PrIdAndUrlUtils.java:85)
    at com.github.terma.jenkins.githubprcoveragestatus.MasterCoverageAction.perform(MasterCoverageAction.java:71)
    at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:80)
    at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:67)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:49)
    at hudson.security.ACL.impersonate(ACL.java:260)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:46)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

我尝试了多种方法来注入这些变量。在我最近的尝试中,我什至查看了source code for this class,所以我将管道更改为:

pipeline {

  agent any

  options {
    skipDefaultCheckout()
  }
  environment {
    // calling credentials() actually sets 3 environment variables
    // GIT_HUB with  <username>:<password>
    // GIT_HUB_USER with <username>
    // GIT_HUB_PSW with <password>

    GIT_HUB = credentials('tmhjenkins')
    DOCKER_REPO  = 'mobilityhouse'
    DOCKER_HUB   = credentials('tmhitadmin')
    GIT_URL_PROPERTY = "https://$GIT_HUB@github.com/mobilityhouse/testci.git"
  }

  stages{
    ...
    ...
stage('Coverage & Tests') {
      steps {
        sh 'pip3 install -e .'
        sh 'make coverage-xml'
        script {
          currentBuild.result = 'SUCCESS'
          sh(script: 'export GIT_URL_PROPERTY="https://$GIT_HUB@github.com/mobilityhouse/testci.git"')
          env.GIT_URL_PROPERTY = "https://$GIT_HUB@github.com/mobilityhouse/testci.git"
          step([$class: 'MasterCoverageAction'])
        }
      }
    }

...

}

唉,这也失败了。那么我应该如何在管道中正确使用这个插件呢?任何帮助将不胜感激。

【问题讨论】:

    标签: java git github jenkins groovy


    【解决方案1】:

    在拐弯抹角了一阵子之后,我决定更深入地了解这个 Jenkins 插件的作用(issue report in github 并没有产生太多...)

    事实证明,主覆盖率报告存储正确,例如环境变量设置正确:

      step([$class: 'MasterCoverageAction',
                  scmVars:
                    [GIT_URL:
                     "https://github.com/xxx/testci.git",]
                ])
    

    这会在JENKINS_HOME 的 XML 日志文件中添加一个条目,但插件会抱怨:

    Can't find master coverage repository: https://github.com/xxx/testci/pull/8 in stored: {https://github.com/myorga/testci/pull/5=0.6923, https://github.com/xxx/testci/pull/6=0.6923, https://****@github.com/myorga/testci.git=0.5385, https://github.com/xxx/testci/pull/7=0.5385}
    

    这点亮了红灯,因此我深入研究了代码并发现了问题。也就是说,PR 被插件检测为:https://github.com/xxx/testci/pull/6 但是主覆盖操作应该只保存了https://github.com/xxx/testci,因此在配置文件中找不到密钥(被解析为哈希映射)。通读代码后,很容易修复代码。

    githubprcoveragestatus/CompareCoverageAction.java我替换了:

    final float masterCoverage = masterCoverageRepository.get(gitUrl);
    

    用下面几行来

    float masterCoverage;
    if (gitUrl.contains("pull/")) {
        final String myCorrectURL = "https://github.com/" + GitUtils.getUserRepo(gitUrl);
        // Using  masterCoverageRepository.get(myCorrectURL); is failing because URL is
        // https://github.com/USER/REPO/pull/PR_ID
        buildLog.println(BUILD_LOG_PREFIX + "myCorrectURL:" + myCorrectURL);
        masterCoverage = masterCoverageRepository.get(myCorrectURL);
    } else {
        masterCoverage = masterCoverageRepository.get(gitUrl);
    }
    

    这解决了我的问题,本着开源的良好精神,我向fix how a URL for Pull Request is detected 提出了拉取请求,以便其他人可以从这个修复中受益。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 2021-02-11
      • 2020-07-10
      • 2012-11-28
      相关资源
      最近更新 更多