Created
October 10, 2014 09:10
-
-
Save jonnyvaughan/d07802d43a37820b7ea9 to your computer and use it in GitHub Desktop.
PHP webhook script for dploy.io --> Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Use as a post deployment webhook in dploy.io to act as an intermediary between dploy.io and Slack. | |
Will take deployment notifications and post into a Slack channel. Setup an incoming webhook in Slack & note the token. | |
Customise & host this script somewhere then use in dploy.io server settings: | |
i.e. http://www.example.com/dploy.php?token=<slack_channel_token> | |
*/ | |
// get the json from the post body | |
$json = json_decode(file_get_contents('php://input')); | |
// get your slack channel token from query string | |
$token = $_REQUEST["token"]; | |
// customise xxx to point to your incoming webhook url | |
$url = "https://xxx.slack.com/services/hooks/incoming-webhook?token=$token"; | |
// ignore deploy.io webhook tests | |
if($json->author_name!="" && $json->comment!="WebHook Test") { | |
$payload = array(); | |
$payload["username"] = "dploy.io"; // you can customise this | |
// $payload["icon_url"] = "https://www.example.com/img/dploy-logo.png"; // customise this if you want an alt slack icon | |
$payload["text"] = $json->author_name." finished deploying ".$json->repository." (revision ".$json->revision.") to ".$json->environment." ".$json->server." at ".$json->deployed_at; | |
// add a slack attachment for the release notes | |
if($json->comment!="") { | |
$payload["attachments"][] = array("fallback" => "", "color"=>"good", "fields" => array(array("title"=>"Release Notes", "value"=>$json->comment, "short"=>"false"))); | |
} | |
// set up & post curl | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, 'payload='.json_encode($payload)); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment