Skip to content

Instantly share code, notes, and snippets.

@jonnyvaughan
Created October 10, 2014 09:10
Show Gist options
  • Save jonnyvaughan/d07802d43a37820b7ea9 to your computer and use it in GitHub Desktop.
Save jonnyvaughan/d07802d43a37820b7ea9 to your computer and use it in GitHub Desktop.
PHP webhook script for dploy.io --> Slack
<?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