Skip to content

Instantly share code, notes, and snippets.

@varnav
Created July 18, 2016 15:46
Show Gist options
  • Save varnav/95592943a0dfd58c2c804728ca885c81 to your computer and use it in GitHub Desktop.
Save varnav/95592943a0dfd58c2c804728ca885c81 to your computer and use it in GitHub Desktop.
Will receive web hook from slack and trigger build job at Jenkins
<?php
if (!($_REQUEST['token'] == '')) die('I`m afraid, Dave, I can`t do that.');
$jenkins_url = 'http://jenkins.local';
$job_name = str_replace($_REQUEST['trigger_word'].' ', '', $_REQUEST['text']);
$job_name = str_replace('/', '', $job_name); // Anti-injection
//$job_name = preg_replace('/[^A-Za-z0-9\-]/', '', $job_name); // Remove special chars
$url=$jenkins_url.'/job/'.$job_name.'/build';
// CURL
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_NOBODY, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
#echo $httpCode;
if($httpCode == 201) {
echo '{';
//echo '"text": "Thank you!"';
echo '"text": "'.$job_name.' triggered by '.$_REQUEST['user_name'].'"';
echo '}';
} else {
echo '{';
echo '"text": "Something is wrong"';
echo '}';
}
curl_close($handle);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment