Webhook Example
Here's a simple example of a Webhook implementation.
This simple example listens for POST requests from Conductrics. The Conductrics servers will call the Webhook when a new version of the Conductrics script is deployed. As discussed in the Using Webhooks page, the POST request includes the JavaScript that was just deployed.
This example simply commits the posted version of the JavaScript code to a Git repository. It only does the commit, but you could adjust the example to also do the "push" step to push the commit to your remote repository. From there another process could possibly send a notification or start a build or an automated QA process, etc.
To run the example server:
- Create a folder called simple-git.
- Create a subfolder called demo-repo.
- Run the command
git init demo-repoto initialize the subfolder as a git repository. - Copy the package.json and example.coffee files below into the folder.
- Run
npm installto install the dependencies.
Your folder should now look like this:
simple-git
- demo-repo
- .git
- example.coffee
- node_modules
- (lots of subfolders)
- package.json
Now start the server by typing npm start.
You should see a message something like Webhook example server listening on 3000.
To actually use the example server:
- Make the server available at a URL that Conductrics can hit over the Internet. Probably you will use nginx or HAProxy or something similar to expose it safely. You should use "basic authentication" or similar at the proxy level to provide some security.
- Go to the Conductrics admin and provide your Webhook URL for the desired Deploy Target under Settings > Distribution. You can add a username and password to the URL in the "basic authentication" style if desired. See Using Webhooks for screenshots and details.
- Test the webhook by making some trivial change (such as starting and stopping an agent, etc).
Heads UpThis is a very simple example, meant mainly for illustration. It's fine to start with this to get started, but you would probably want to do something more sophisticated depending on your needs.
# dependencies
simpleGit = require('simple-git') # see https://github.com/steveukx/git-js
express = require('express')
bodyParser = require('body-parser')
fs = require('fs')
# where is the git repository we should commit to?
repo_path = "#{__dirname}/demo-repo"
port = 3000
log = console.log
# create an Express server
express()
.use bodyParser.json
limit: "1mb" # adjust as desired
# create route for Conductrics to POST to
.post '/webhooks/simple-git', (req, res) =>
# Conductrics provides a "payload" object when calling this webhook
payload = req.body ? {}
unless typeof (js_code = payload.js) is 'string'
res.end "Invalid request"
# we'll save the just-deployed JS locally
file_path = "#{repo_path}/conductrics_script.js"
log "Incoming webhook call for deploy target '#{payload.name}', will write to file: #{file_path}"
fs.writeFileSync file_path, js_code
# commit to git repo
simpleGit repo_path
.add file_path
.commit "Automated commit to deploy target '#{payload.name}' via webhook"
# done
res.send "OK"
.listen port, (err) =>
if (err) then return log('something bad happened', err)
log "Webhook example server listening on #{port}"{
"name": "conductrics-webhook-example-git",
"version": "1.0.0",
"description": "Conductrics Webhook Example for Git",
"main": "example.coffee",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.3",
"simple-git": "^1.92.0"
},
"scripts": {
"start": "coffee example.coffee"
},
"devDependencies": {
"coffee-script": "^1.12.7"
},
"author": "Conductrics Team",
"license": "MIT"
}Updated about 1 year ago