You misunderstand - I need the body of the request to the Netlify build hooks:
to be available in the function ‘deploy-succeeded.js’:
I am NOT triggering an event directly. I WANT the request body from the build hook request to be available in the ‘deploy-succeeded.js’ function, JUST LIKE the same request body is available to the build, as described in the ‘Payload’ section here:
describes how that request body IS available to the BUILD as environment variables. I want the same data to be available in the ‘deploy-succeeded.js’.
“You can send a custom payload in your build hook POST request. The contents must be passed as a string, which will be URL-encoded and made available in the triggered build as an environment variable. You can access it in the build by using the variable INCOMING_HOOK_BODY .”
I don’t know a direct way to get it to your function as you prefer; you cannot change how we invoke that function, and we don’t forward that data, as you’ve discovered.
You could use a pattern like this:
deploy the contents in a file,
since they are available during build as $WEBHOOK_BODY,
which you could choose to add to your code,
and deploy with your site.
Finally, the deploy-succeeded event-triggered function could go look at the file contents while it runs, since you’ll know the URL you just left those contents at. Heck, you could even write them RIGHT INTO THE FUNCTION CODE if you want to
This article explains that pattern of interpolating env vars into code at run time:
I was running into the same sort of issue as @sparkle . To make sure I understand based on this comment and the one you linked I would need to:
change my build to `npm run build && echo INCOMING_HOOK_BODY > public/hookBody.txt
Then, in my function that runs after deploy-succeeded, how would you go about accessing that payload from the file? I’m using the payload to determine if I need to call a third party API after an update or not.
Thanks for following up with those details. I can see based on the photo above I can see that the functions ran for 2 to 3 milliseconds. This makes me wonder if the function is initiating and then closing immediately. Can you share your entire function file so that we can see how it is configured?
Seems I’ve gotten past the console.log issue, but I’m still having trouble achieving my goal with fs. Currently my build command is: npm run build && echo $INCOMING_HOOK_BODY > public/webhookBody.txt, as per the recommendation from @fool. However, my deploy-succeeded function cannot seem to find the file using fs once deployed, even though it works locally using netlify dev:
@hrishikesh I’m not sure what you’re asking. I have read the file yes. I see they use an example with require and a relative path, which I have attempted. Or are you speaking about the manual configuration section?
This remains true no matter if I save the file as .json or .txt. What’s strange however, is I can clearly see the value of $INCOMING_HOOK_BODY in the deploy console when I echo it without pointing at a file:
It just seems that even though the file is getting written to /var/task/netlify/functions/deploy-succeeded there is never the value expected when using echo $INCOMING_HOOK_BODY > netlify/functions/deploy-succeeded/webhookBody.txt