Access Build Webhook payload in 'deploy-succeeded.js'

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?

Thanks

Hey @sparkle,

Event-based functions cannot be called or invoked otherwise, so what you’re looking for would not work. You’ll have to create a separate function.

However, if you’re using a custom function, let us know the site name and the function URL and we can see what could be happening.

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 .”

Thanks for clarifying!

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 :smiley:

This article explains that pattern of interpolating env vars into code at run time:

[Support Guide] Using environment variables on Netlify correctly (most relevant part for you, starts in the 4th paragraph)

Hi @fool !

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:

  1. change my build to `npm run build && echo INCOMING_HOOK_BODY > public/hookBody.txt
  2. 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.

So can I use the fs module in my deploy-succeeded function file to read and then determine the action to take?

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:


Hey there, @ONeill45 :wave:

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?

Hi @hillary !

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:

const fetch = require('node-fetch')
const fs = require('fs')
const path = require('path')

const handler = async (event, context) => {
  const webhookBody = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../../../public/webhookBody.txt')))
  console.log(webhookBody)
  const endpoint = 'https://31f9-2600-2b00-7247-3b00-6017-1890-8b78-3c37.ngrok.io/webhook'
  try {
    const response = await fetch(endpoint, {
      headers: {
        'Content-Type': 'application/json',
      },
      method: 'POST',
      body: JSON.stringify(webhookBody),
    })
    const result = await response.text()
    console.log(result)
    return {
      statusCode: 200,
      body: JSON.stringify({ message: 'Hello World' }),
    }
  } catch (error) {
    console.log(error)
    return {
      statusCode: 500,
      body: JSON.stringify(error),
    }
  }
}

module.exports = { handler }

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?

Did you try this:

@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?

Do I add it from the root like:

[functions]
included_files = ["public"]

Or is it relative to the functions folder?

@hillary @hrishikesh

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.

I am able to successfully reference the file in my document, however no matter what, the file is always empty:

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