lolyoshi
lolyoshi

Reputation: 1536

Get payload parameters in Jenkins

I'm a newbie in Jenkins. I followed this help to implement Jenkins, Github and Webhook.

However, I'm stuck in this problem. I want to get the payload parameters in Jenkins to check information such as changes, actions...from payload json.

I wrote some script to test. ACTION and $payload always returns empty

# !/bin/bash    
ACTION=`echo $payload | php -r '$data = file_get_contents("php://stdin"); $json = json_decode($data, true); echo $json["action"];'`

I read some tutorials but unsuccessfully.

Upvotes: 1

Views: 10838

Answers (2)

jackofall
jackofall

Reputation: 316

There are few steps that you should follow:

  1. Configure a web-hook in GitHub by which you will start communicating with Jenkins. This can be easily by going to your GitHub repository --> Setting --> Web-hooks.

  2. Make the entry for content-type as application/x-www-form-encoded.

  3. Select the event as Push event.

  4. Create a String parameter in Jenkins Server by the name of payload.

  5. Following Step 3 GitHub would be able to wrap the content in this variable and send it to Jenkins.

  6. In Jenkins you can then read $payload variable when your build is kicked off.

  7. Parse the JSON using library of your choice

References

Upvotes: 4

Tomas Bjerre
Tomas Bjerre

Reputation: 3500

Easier way of doing this, without an extra library for parsing JSON, would be using the Generic Webhook Trigger Plugin in Jenkins.

It can resolve any value from the JSON using JSONPath and assign it to variables available in the build.

Upvotes: 3

Related Questions