简体   繁体   中英

Jenkins - How do I publish coverage report to github

I am trying to use github-pr-coverage-status-plugin , the documentation says I need to run the following on my master branch:

step([$class: 'MasterCoverageAction'])

But when I add this to my pipeline I get the following error:

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

I have tried multiple ways to inject these variables. In my latest try, I even looked at the source code for this class , so I changed my pipeline to be:

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'])
        }
      }
    }

...

}

Alas, this fails too. So how am I supposed to properly use this plugin from within the pipeline? Any help would be appreciated.

After beating around the bush for a while, I decided to take a deeper look what this Jenkins plugin does (an issue report in github wasn't yielding much ...)

It turned out that the master coverage reports are stored properly for example with the environment variable properly set:

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

This would have added an entry in the XML log file in JENKINS_HOME , how ever the plugin would complain:

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}

This lit a red light, so I dug into the code and found the problem. Which is that the PR is detected by the plugin as: https://github.com/xxx/testci/pull/6 however master coverage action should have only saved https://github.com/xxx/testci so the key is not found in the configuration file (which is parsed to a hash map). After reading through the code, it was pretty easy to fix the code.

In githubprcoveragestatus/CompareCoverageAction.java I replaced:

final float masterCoverage = masterCoverageRepository.get(gitUrl);

with the following lines to

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);
}

This solved my problem, in the good spirit of open source, I made a pull request to fix how a URL for Pull Request is detected , such that others can benefit from this fix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM