jenkinsci / generic-webhook-trigger-plugin

Can receive any HTTP request, extract any values from JSON or XML and trigger a job with those values available as variables. Works with GitHub, GitLab, Bitbucket, Jira and many more.
https://plugins.jenkins.io/generic-webhook-trigger
403 stars 159 forks source link

How can I get the raw json body? #284

Closed yuchengren closed 7 months ago

yuchengren commented 7 months ago

Hello, my pipeline like this genericVariables: [ [key: 'BODY', value: '$', expressionType: 'JSONPath', //Optional, defaults to JSONPath regexpFilter: '', //Optional, defaults to empty string defaultValue: '' //Optional, defaults to empty string ], ],

when I user $BODY in shell script, it is not a raw json, but a JSONPATH format string 。 How can I get the raw json body, sample: gitlab payload json body

tomasbjerre commented 7 months ago

Can you supply complete pipeline, request with curl command and response?

yuchengren commented 7 months ago

Can you supply complete pipeline, request with curl command and response? ` pipeline { agent any

environment { SCRIPT_PROJECT_ROOT = "/Users/macuser/PycharmProjects/smartscripts" }

triggers { GenericTrigger( genericVariables: [ [key: 'BODY', value: '$', expressionType: 'JSONPath', //Optional, defaults to JSONPath regexpFilter: '', //Optional, defaults to empty string defaultValue: '' //Optional, defaults to empty string ], ], causeString: 'Generic Cause', token: 'gitlab', printContributedVariables: true, printPostContent: true, silentResponse: true, ) }

stages { stage('action') { steps { sh "export PYTHONPATH=${SCRIPT_PROJECT_ROOT} && python3 -u ${SCRIPT_PROJECT_ROOT}/android/jenkins/webhook/webhook-gitlab.py \ --data $BODY \ " } } } } `

the jenkins execute log: `

$BODY output is not raw json,but a JSONPATH string.

tomasbjerre commented 7 months ago

I am missing the curl-command. Also the log, ans specifically the contributed variables.

yuchengren commented 7 months ago

I am missing the curl-command. Also the log, ans specifically the contributed variables.

It it just trigger by gitlab webhook, the URL is http://JENKINS_URL/generic-webhook-trigger/invoke?token=gitlab, I want to do something after push, and I need to raw json body

tomasbjerre commented 7 months ago

This is impossible to investigate if you do not supply something that I can reproduce.

yuchengren commented 7 months ago

the gitlab we

This is impossible to investigate if you do not supply something that I can reproduce.

sorry, I do not understand, Has any else Do you need? The webhook is not a curl-command, it is just trigger by gitlab push event。the gitlab push event, webhook body sample is on docs: https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html

yuchengren commented 7 months ago

more jenkins log: Generic Cause GenericWebhookEnvironmentContributor Received:

{"object_kind":"push","event_name":"push",...}

Contributing variables:

BODY = {"object_kind":"push","event_name":"push",..}
tomasbjerre commented 7 months ago

So you are saying that if you do:

sh """
 echo "BODY: " + BODY
"""

You do not get BODY: {"object_kind":"push","event_name":"push",..} ? What exactly do you get?

yuchengren commented 7 months ago
  • object_kind:push event_name:push ...

Yes ! , sh "export PYTHONPATH=${SCRIPT_PROJECT_ROOT} && python3 -u ${SCRIPT_PROJECT_ROOT}/android/jenkins/webhook/webhook-gitlab.py --data $BODY " the $BODY is not json, but JSONTPATH format string -> object_kind:push event_name:push ...

tomasbjerre commented 7 months ago

If you print the variable, like I said, you will see that it is the raw json.

You problem is probably is how you are using the value. See https://stackoverflow.com/a/38385227/2477084

yuchengren commented 7 months ago

If you print the variable, like I said, you will see that it is the raw json.

You problem is probably is how you are using the value. See https://stackoverflow.com/a/38385227/2477084

you are right! the BODY is a raw json. thanks very much . The problem is that, shell command json param is need to wrap by ''