在jenkins的github pull请求中添加注释

时间:2018-05-23 12:34:33

标签: jenkins github

我在Jenkins中使用Git pull request builder插件将Pull请求链接到Jenkins。通过钩子触发作业,一切顺利进行。

然而,在构建结束时,我想在失败/成功或错误时添加评论。

我尝试使用管道并使用

pullRequest.comment('This is a comment sent from the Pipeline')

但它没有认识到它(我为github安装了管道):

groovy.lang.MissingPropertyException: No such property: pullRequest for class: groovy.lang.Binding
  at groovy.lang.Binding.getVariable(Binding.java:63)
  at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:242)
  at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)
  at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
  at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
  at WorkflowScript.run(WorkflowScript:5)
  at ___cps.transform___(Native Method)
  at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
  at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
  at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
  at sun.reflect.GeneratedMethodAccessor446.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
  at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
  at com.cloudbees.groovy.cps.Next.step(Next.java:83)
  at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
  at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
  at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
  at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
  at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)
  at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)
  at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
  at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
  at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
  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

自由式工作:

java.lang.NullPointerException
  at org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getGhRepositoryFromPRTrigger(JobHelper.java:162)
  at org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getGhPullRequest(JobHelper.java:178)
  at org.jenkinsci.plugins.github.pullrequest.publishers.impl.GitHubPRCommentPublisher.perform(GitHubPRCommentPublisher.java:72)
  at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
  at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
  at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
  at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
  at hudson.model.Build$BuildExecution.post2(Build.java:186)
  at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
  at hudson.model.Run.execute(Run.java:1752)
  at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
  at hudson.model.ResourceController.execute(ResourceController.java:97)
  at hudson.model.Executor.run(Executor.java:429)
Build step 'GitHub PR: post comment' marked build as failure

现在我真的不知道在哪里寻找

1 个答案:

答案 0 :(得分:0)

我找不到插件,所以我确实使用了curl。这是我的常规功能

def call(def text_pr) {
  def repository_url = scm.userRemoteConfigs[0].url
  def repository_name = repository_url.replace("git@github.com:","").replace(".git","")

  withCredentials([string(credentialsId: '<YOUR-TOKEN-ID>', variable: 'GITHUB_TOKEN')]) {
    sh "curl -s -H \"Authorization: token ${GITHUB_TOKEN}\" -X POST -d '{\"body\": \"${text_pr}\"}' \"https://api.github.com/repos/${repository_name}/issues/${ghprbPullId}/comments\""
  }
}