Jenkins:Generic Webhook Trigger插件和Multibranch Scan Webhook Trigger -genericVariables的问题

brgchamk  于 2023-04-11  发布在  Jenkins
关注(0)|答案(1)|浏览(165)

附录第2部分:作为POST请求执行:
请求详情:

Event type:repo:refs_changed
URL endppoint:https://jenkins.foo.bar/multibranch-webhook-trigger/invoke?token=test_token

请求标头:

X-Request-Id: 686cd082-2bdd-4abb-857a-e46e2d466dfa
Content-Type: application/json; charset=utf-8
X-Event-Key: repo:refs_changed

请求正文:

{
    "eventKey":"repo:refs_changed",
    "date":"2023-04-06T14:35:57+0200",
    "actor":{
        "name":"foo@bar.de",
        "emailAddress":"foo@bar.de",
        "id":3,
        "displayName":"Foo Bar",
        "active":true,
        "slug":"foo_bar.de",
        "type":"NORMAL",
        "links":{
            "self":[
                {
                    "href":"https://bb.foo.de/users/foo_bar.de"
                }
            ]
        }
    },
    "repository":{
        "slug":"foo.bar",
        "id":194,
        "name":"foo.bar",
        "description":"Foo bar",
        "hierarchyId":"ec2a64701705b2c36097",
        "scmId":"git",
        "state":"AVAILABLE",
        "statusMessage":"Available",
        "forkable":true,
        "project":{
            "key":"Foo",
            "id":124,
            "name":"Bar",
            "description":"Foo bar",
            "public":false,
            "type":"NORMAL",
            "links":{
                "self":[
                    {
                        "href":"https://bb.foo.bar/projects/foobar"
                    }
                ]
            }
        },
        "public":false,
        "links":{
            "clone":[
                {
                    "href":"https://bb.foobar/scm/foo/bar.git",
                    "name":"http"
                },
                {
                    "href":"ssh://git@bb.foo.bar:7999/foo/bar.git",
                    "name":"ssh"
                }
            ],
            "self":[
                {
                    "href":"https://bb.foo/projects/bar/browse"
                }
            ]
        }
    },
    "changes":[
        {
            "ref":{
                "id":"refs/heads/feature/foo-bar",
                "displayId":"feature/foo-bar",
                "type":"BRANCH"
            },
            "refId":"refs/heads/feature/foo-bar",
            "fromHash":"905944ba5c39999c64e66378613caa1578735780",
            "toHash":"5d7da403f10c07f44aa78f62d4fa8d9d8e5a125f",
            "type":"UPDATE"
        }
    ]

}

回复详情:

HTTP-Status: 200
Duration 64ms

响应标头:

Date: Thu, 06 Apr 2023 12:35:58 GMT
Server: nginx
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=UTF-8
Connection: keep-alive
Via: 1.1 localhost (Apache-HttpClient/4.5.5 (cache))
Content-Length: 157

响应主体:

{
  "status": "ok",
  "data": {
    "triggerResults": {
      "foo/foo.bar/foo.bar Testing": {
        "triggered": true,
        "id": 1163,
        "url": "queue/item/1163/"
      }
    }
  }
}

附录:
这里是我尝试了一些之后的更多信息。触发的webhook地址的URL结构如下:https://URL_TO_JENKINS/multibranch-webhook-trigger/invoke?token = test_token
经过一些调整后,Jenkinsfile的开头看起来像这样:

pipeline {
  agent any

  triggers {
    GenericTrigger(
     genericVariables: [
       [key: 'name', value: '$actor.name'],
       [key: 'reviewers', value: '$.pullRequest.reviewers'],
       [key: 'ref', value: '$.ref']
    ],
    causeString: 'Triggered on $ref',
    token: 'test_token',
    tokenCredentialId: '',
    printContributedVariables: true,
    printPostContent: true,
    silentResponse: false,
    regexpFilterText: '$ref',
    regexpFilterExpression: 'refs/heads/' + BRANCH_NAME
    )
  }

  stages {
    stage('Preparing') {
      steps {
        sh "echo Ref: $ref"
        sh "echo Name: $name"
        sh "echo Reviewers: $reviewers"
      }
....

在jenkins控制台中,在管道执行期间,我得到以下输出:

groovy.lang.MissingPropertyException: No such property: ref for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:285)
    at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:375)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:379)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:355)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:355)
    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:25)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:666)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:395)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:393)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:665)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:288)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:544)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:543)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:276)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:443)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:442)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:275)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(ModelInterpreter.groovy:481)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(ModelInterpreter.groovy:480)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:274)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inDeclarativeAgent(ModelInterpreter.groovy:586)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inDeclarativeAgent(ModelInterpreter.groovy:585)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:272)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.stageInput(ModelInterpreter.groovy:356)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.stageInput(ModelInterpreter.groovy:355)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:261)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inWrappers(ModelInterpreter.groovy:618)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inWrappers(ModelInterpreter.groovy:617)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:259)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:443)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:442)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:254)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:73)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:65)
    at jdk.internal.reflect.GeneratedMethodAccessor188.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    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:152)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:146)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:146)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:187)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:420)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:330)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:294)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
    at jenkins.util.ErrorLoggingExecutorService.lambda$wrap$0(ErrorLoggingExecutorService.java:51)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)

当我查看通过webhook发送的请求和响应的详细信息时,我还看到有效负载也被发送。
如果我修改Jenkinsfile如下:

pipeline {
  agent any

  triggers {
    GenericTrigger(
     genericVariables: [
       [key: 'name', value: '$actor.name'],
       [key: 'reviewers', value: '$.pullRequest.reviewers'],
       [key: 'ref', value: '$.ref']
    ],
    causeString: 'Triggered on $ref',
    token: 'test_token',
    tokenCredentialId: '',
    printContributedVariables: true,
    printPostContent: true,
    silentResponse: false,
    regexpFilterText: '$ref',
    regexpFilterExpression: 'refs/heads/' + BRANCH_NAME
    )
  }

  stages {
    stage('Preparing') {
      steps {
        sh '''
        echo Reference: ${ref}
        echo Name: ${name}
        echo Reviewers: ${reviewers}
        '''
      }
....

我得到以下输出:

[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Preparing)
[Pipeline] sh
+ echo Reference:
Reference:
+ echo Name:
Name:
+ echo Reviewers:
Reviewers:
[Pipeline] sh
+ echo 'removing old container: rails'
removing old container: rails

原问题:
我已经在我的Jenkins(2.387.1)中构建了一个多分支管道,我想通过一个webhook触发它。一个Bitbucket服务器连接到Jenkins。现在的目标是一旦所有pull请求的审阅者都批准了它,就触发一个管道。
我安装了Generic Webhook Trigger Plugin。由于这是一个多分支管道,我还使用了Multibranch Scan Webhook触发器插件(如此处所述:https://plugins.jenkins.io/generic-webhook-trigger/)。
有了这个插件,我设法在令牌“test_token”作为URL的一部分传递时触发管道。但是由于PR可以有多个审阅者,我想在我的管道内检查(由声明性Jenkinsfile描述)是否所有审阅者都批准了PR。因此,我想使用通过webhook发送的数据的有效负载来检查是否所有审阅者都批准了PR。
最后说说我的问题:不幸的是,我的Jenkinsfile中的变量赋值不能正常工作,或者有效负载是空的(无论什么原因)。当我输出变量名称和审阅者时,我只得到一个空字符串。
以下是我的Jenkinsfile开头的摘录:

pipeline {
  agent any
  // To learn how to use environment variables in Jenkinsfiles, see: https://e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/
  // environment {
  // }

    triggers {
      GenericTrigger(
       genericVariables: [
        [key: 'name', value: '$.actor.displayName'],
        [key: 'reviewers', value: '$.pullRequest.reviewers']
       ]
      )
    }

  stages {
    stage('Preparing') {
      steps {
        sh '''
        echo Name: $name
        echo Reviewers: $reviewers
        '''

        // Removes previously created containers to enable a clean start of the pipeline before creating the new ones in a further step
        sh '''
        echo "removing old container: rails"
        echo | docker container rm -f rails

        echo "removing old container: maria_test_db"
        echo | docker container rm -f maria_test_db

        echo | docker container ls -a
        '''
....

谁能告诉我,我是否犯了一个错误?
先谢谢你的帮助

ifmq2ha2

ifmq2ha21#

问题是您配置的jsonpath与请求体不匹配。这是关于jsonpath的问题,而不是关于插件的问题。

相关问题