2017-09-13 98 views
0

我想使用github-pr-coverage-status-plugin,該文件說,我需要運行在我的主分支下面:詹金斯 - 如何發佈覆蓋報告到github上

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://[email protected]/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://[email protected]/mobilityhouse/testci.git"') 
      env.GIT_URL_PROPERTY = "https://[email protected]/mobilityhouse/testci.git" 
      step([$class: 'MasterCoverageAction']) 
     } 
     } 
    } 

... 

}

唉,這也失敗。那麼我該如何正確使用這個插件呢?任何幫助,將不勝感激。

回答

0

旁敲側擊一段時間後,我決定去深入瞭解一下這個詹金斯插件做了什麼(一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,例如其他人可以從此修復中受益。