Twitter Webhook for Travis CI [
](https://travis-ci.org/jamesseanwright/jamesswright.co.uk)
Build 25 of #jamesswright.co.uk has passed: https://t.co/JJMujJr6jC
— James Wright (@JamesSWrightWeb) January 12, 2015
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
Just a quick webhook project using TDD to tweet Travis CI build notifications.
To begin with, fork and clone the repository. You'll want to find a public host, such as Heroku or Cloudnode, if you can't host it yourself.
Run npm i
to install the required dependencies.
Before configuring the server, you'll need to create and authorise a new Twitter app:
- Go to apps.twitter.com and sign in with the Twitter account from which the tweets will be published
- Click the Create New App button.
- Complete the form with the appropriate information. For the website, you could list the production website which you're building with Travis
- Click Create your Twitter application
- To enable the app to receive HTTP POST requests, click the Permissions tab, choose Read and Write, and click Update Settings
- Click the Keys and Access Tokens tab
- Under the Your Access Token header, click Create my access token
You should be returned to the Keys and Access Tokens tab with the necessary information to tweet via the webhook.
Note: In terms of the roadmap, I am considering writing a centralised HTTPS server that will use a single Twitter app for authentication and proxy Twitter API requests; this would eradicate the above steps in favour of simply authorising said Twitter app against you account and exporting your access token and secret as environment variables. Any thoughts on this would be greatly appreciated.
The server is configured via these environment variables:
WEBHOOK_PORT
- the HTTP port on which the server should run. Defaults to 3000 if unspecifiedTRAVIS_TOKEN
- the authorization token associated with your Travis CI account. If you're logged in, you can find it hereCONSUMER_KEY
- the Consumer Key (API Key) listed in the Keys and Access Tokens of your Twitter appCONSUMER_SECRET
- the Consumer Secret (API Secret) listed in the Keys and Access Tokens of your Twitter appACCESS_TOKEN_KEY
- the Access Token listed under Your Access Token in the Keys and Access Tokens of your Twitter appACCESS_TOKEN_SECRET
- the Access Token Secret* listed under Your Access Token in the Keys and Access Tokens of your Twitter app
Configure these appropriately. Here's a good guide to setting environment variables across operating systems. Most modern cloud hosting services will permit you to set these via a CLI or a web interface.
Start the server, either via your host or by running node server
.
In your .travis.yml file, you can really easily integrate with the webhook:
notifications:
webhooks: http://your-webhook.com/
There's no additional route to specify as the server's root endpoint handles the requests. You just need to specify the host's URL as above.
Read this guide for more advanced integrations.
You're now good to go. Whenever you make a new build, it should tweet depending upon your configuration. With the most basic configuration as above, it will tweet on both the success and failure of your build.
This webhook is compliant with Travis' authorisation mechanism, so only a request triggered by Travis for one of your own repositories will be accepted.
Although these are ignored by Travis, there are some response bodies to aid with debugging. Error messages are returned with the respective status code and the following body format:
{ "error": "some message" }
Returned when the tweet was successfully posted, with the following body:
{ "result": "success" }
Returned when the required properties in Travis' payload are missing.
Returned when the TRAVIS_TOKEN
env variable is incorrect, or the Authorization
/Travis-Repo-Slug
headers are invalid.
Returned when the POST to Twitter's statuses/update
endpoint failed. This can occur when the API keys are set incorrectly or when the API has detected a duplicate tweet.
##Contributions I'd be more than happy to accept contributions to this webhook. Just create an issue or a pull request. Please write tests for your code where possible.
Simply run npm test
.