How to process a github webhook payload in Jenkins?

后端 未结 2 1551
难免孤独
难免孤独 2020-11-30 02:24

I\'m currently triggering my Jenkins builds through a GitHub webhook. How would I parse the JSON payload? If I try to parameterize my build and use the $payload variable, th

2条回答
  •  没有蜡笔的小新
    2020-11-30 03:05

    There is a Generic Webhook Trigger plugin that can contribute values from the post content to the build.

    If the post content is:

    {
       "app":{
          "name":"GitHub API",
          "url":"http://developer.github.com/v3/oauth/#oauth-authorizations-api"
       }
    }
    

    You can configure it like this:

    And when triggering with some post content:

    curl -v -H "Content-Type: application/json" -X POST -d '{ "app":{ "name":"GitHub API", "url":"http://developer.github.com/v3/oauth/" }}' http://localhost:8080/jenkins/generic-webhook-trigger/invoke?token=sometoken
    

    It will resolv variables and make them available in the build job.

    {  
       "status":"ok",
       "data":{  
          "triggerResults":{  
             "free":{  
                "id":2,
                "regexpFilterExpression":"",
                "regexpFilterText":"",
                "resolvedVariables":{  
                   "app_name":"GitHub API",
                   "everything_app_url":"http://developer.github.com/v3/oauth/",
                   "everything":"{\"app\":{\"name\":\"GitHub API\",\"url\":\"http://developer.github.com/v3/oauth/\"}}",
                   "everything_app_name":"GitHub API"
                },
                "searchName":"",
                "searchUrl":"",
                "triggered":true,
                "url":"queue/item/2/"
             }
          }
       }
    }
    

提交回复
热议问题