I am triggering a build using the webhook, and sending some metadata in the body of the request. I am not able to send any parameters in heading or as URL params, the data has to be sent in the body.
Does anyone know how I can access this data in a Netlify Function, that is listening for a deploy-succeeded event?
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.
You can access the payload just like any other file. If you use a Node.js script, you can use fs module to read the file and make conditional decisions in that script.
Yes! You should be able to use fs in your deploy-succeeded function file. Please let us know if this doesn’t work, and we will continue to look into this.
So I’ve deployed a deploy-succeeded function, and it appears to get hit at the end of the deploy, but none of the code runs. When adding console.log() statements, they don’t even show up in the logs:
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:
I’ve tried using require, fs, and now using path, but every time my deploy-succeeded event function hits, it cannot find the file. What is the proper way to reference the file?
@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?
Sorry, I should have been more descriptive. But yes, if an automatic require is not working, forcing the file should be the way to go forward. Does that work?
I now am getting a file located next to my deploy-succeeded.js file using npm run build && echo $INCOMING_HOOK_BODY > netlify/functions/deploy-succeeded/webhookBody.txt.
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