Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Test Incoming Webhooks Locally with Ngrok

DZone's Guide to

Test Incoming Webhooks Locally with Ngrok

· DevOps Zone ·
Free Resource

Automatic continuous monitoring keeps continuous deployment pipelines flowing smoothly and efficiently. Try Instana APM to automatically monitor containers and microservices!

I'm enjoying working with hubot; I have a few different instances running for different groups. One of the things that I use hubot for is to echo activities from the source control repo, the bug tracker, the CI system, the (you get the idea) into the channel for a project. This is achieved via webhooks; a feature offered by many providers to POST information about an event to your endpoint as soon as it happens.

In this case, the webhook points to the hubot code running on heroku, which isn't an ideal place to debug a problem or fiddle about getting a message wording just right with all the fields in place. Instead, I do all that locally using the hubot and its shell adapter. How? Using Ngrok!

Ngrok Opens Localhost

Ngrok is a command you run locally, which opens a tunnel on the port you specify from your machine to the outside world. Ngrok will then make an incoming tunnel available and let you know which URL to use - ideal for allowing people to inspect development versions of your websites, or for testing incoming webhooks against a local dev platform.

Hubot runs on port 8080 by default so I start the tunnel with this command:

ngrok 8080

Ngrok gets going and presents a kind of commandline console view:

Screenshot from 2015-03-11 08:17:25

When requests start coming through, you'll see them listed here, but there are two important pieces of information that we want to grab at this point. One is the "Web Interface" URL, usually http://127.0.0.1:4040, which you can open in your browser - we'll revisit this in a bit. The most important one is the "Forwarding" URL. Requests to this endpoint will end up on your machine on the port you specified.

As an example, today I'm doing some testing with the webhooks that come in from GitHub. I didn't find that the support in existing hubot plugins was great, so I forked an existing one and added some functionality to it (you can find my plugin here, try the "joindin-hubot" branch: https://github.com/lornajane/hubot-github-repo-event-notifier). To test, I edited the webhooks on my repo to instead point to the URL ngrok gave me, so for this plugin, the webhook endpoint is now: http://42aa88bf.ngrok.com/hubot/gh-repo-events.

Screenshot from 2015-03-11 08:36:36

Then I trigger a hook to fire, and see what my hubot does. Probably, it might need a bit of fine tuning, so I can edit the code on my local machine, run the bot again, and repeat. Even better, check out the "Web Interface" link that ngrok produces - this has full details of all the requests we received and responded to. It also allows you to replay requests, so once you've triggered a webhook once you can just replay as many times as you need to (actually GitHub in particular have excellent replay features on their webhook pages, but not all providers do).

Screenshot from 2015-03-11 08:40:17

One you're happy, shut down your ngrok tunnel with Ctrl+c, and you can commit and push your changes to your live hubot. I find this technique incredibly helpful, and hopefully you do too! If you have anything to add, please leave me a comment, I think these toolchains are improving all the time so it would be great to hear how others are approaching the same problems or tools.

Automatic real-time observability is critical to getting the full benefit of CI/CD. Hear @DevOpsDon discuss how Franklin American Mortgage Company cut their new application deployment time from 6-12 months to 2 weeks with the help of Instana APM.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}