<?php // Important constants :) $pantheon_yellow = '#EFD01B'; // Default values for parameters - this will assume the channel you define the webhook for. // The full Slack Message API allows you to specify other channels and enhance the messagge further // if you like: https://api.slack.com/docs/messages/builder $defaults = array('slack_username' => 'Pantheon-Quicksilver', 'always_show_text' => false); // Load our hidden credentials. // See the README.md for instructions on storing secrets. $secrets = _get_secrets(array('slack_url'), $defaults); // Build an array of fields to be rendered with Slack Attachments as a table // attachment-style formatting: // https://api.slack.com/docs/attachments $fields = array(array('title' => 'Site', 'value' => $_ENV['PANTHEON_SITE_NAME'], 'short' => 'true'), array('title' => 'Environment', 'value' => '<http://' . $_ENV['PANTHEON_ENVIRONMENT'] . '-' . $_ENV['PANTHEON_SITE_NAME'] . '.pantheonsite.io|' . $_ENV['PANTHEON_ENVIRONMENT'] . '>', 'short' => 'true'), array('title' => 'By', 'value' => $_POST['user_email'], 'short' => 'true'), array('title' => 'Workflow', 'value' => ucfirst($_POST['stage']) . ' ' . str_replace('_', ' ', $_POST['wf_type']), 'short' => 'true'), array('title' => 'View Dashboard', 'value' => '<https://dashboard.pantheon.io/sites/' . PANTHEON_SITE . '#' . PANTHEON_ENVIRONMENT . '/deploys|View Dashboard>', 'short' => 'true')); // Customize the message based on the workflow type. Note that slack_notification.php // must appear in your pantheon.yml for each workflow type you wish to send notifications on. switch ($_POST['wf_type']) { case 'deploy': // Find out what tag we are on and get the annotation. $deploy_tag = `git describe --tags`; $deploy_message = $_POST['deploy_message']; // Prepare the slack payload as per: // https://api.slack.com/incoming-webhooks $text = 'Deploy to the ' . $_ENV['PANTHEON_ENVIRONMENT']; $text .= ' environment of ' . $_ENV['PANTHEON_SITE_NAME'] . ' by ' . $_POST['user_email'] . ' complete!'; $text .= ' <https://dashboard.pantheon.io/sites/' . PANTHEON_SITE . '#' . PANTHEON_ENVIRONMENT . '/deploys|View Dashboard>'; $text .= "\n\n*DEPLOY MESSAGE*: {$deploy_message}"; // Build an array of fields to be rendered with Slack Attachments as a table // attachment-style formatting: // https://api.slack.com/docs/attachments
<?php // Get the environment; we will post a new comment to Jira each time // a commit appears on a new branch on Pantheon. $env = $_ENV['PANTHEON_ENVIRONMENT']; // Do not watch test or live, though. if ($env == 'live' || $env == 'test') { exit(0); } // Look up the secrets from the secrets file. $secrets = _get_secrets(array('jira_url', 'jira_user', 'jira_pass'), array()); // Get latest commit $current_commithash = shell_exec('git rev-parse HEAD'); $last_commithash = FALSE; // Retrieve the last commit processed by this script $commit_file = $_SERVER['HOME'] . "/files/private/{$env}_jira_integration_commit.txt"; if (file_exists($commit_file)) { $last_processed_commithash = trim(file_get_contents($commit_file)); // We should (almost) always find our last commit still in the repository; // if the user has force-pushed a branch, though, then our last commit // may be overwritten. If this happens, only process the most recent commit. exec("git rev-parse {$last_processed_commithash} 2> /dev/null", $output, $status); if (!$status) { $last_commithash = $last_processed_commithash; } } // Update the last commit file with the latest commit file_put_contents($commit_file, $current_commithash, LOCK_EX); // Retrieve git log for commits after last processed, to current $commits = _get_commits($current_commithash, $last_commithash, $env); // Check each commit message for Jira ticket numbers
<?php // Secrets helper function require_once dirname(__FILE__) . '/secrets_helper.php'; // Load Slack helper functions require_once dirname(__FILE__) . '/slack_helper.php'; // An example of using Pantheon's Quicksilver technology to do // automatic performance test using Load Impact. Adapted from // a random GitHub done by someone at Rackspace. // Provide the API Key and API Path provided by Load Impact $secrets = _get_secrets(array('loadimpact_key', 'loadimpact_key_v3', 'slack_channel', 'live_url')); $api_key = $secrets['loadimpact_key']; $api_key_v3 = $secrets['loadimpact_key_v3']; // Provide the Project ID for the test on Load Impact // $test_config_id = 3359026; $test_config_id = 3420633; // Provide the Slack Details $slack_channel_name = $secrets['slack_channel']; $slack_user_name = 'PerformanceTesting-with-LoadImpact'; $slack_user_icon = $secrets['live_url'] . '/wp-content/uploads/icons/loadimpact.png'; // If we are deploying to test, run a performace test if (defined('PANTHEON_ENVIRONMENT') && PANTHEON_ENVIRONMENT == 'test') { echo 'Starting a performance test on the test environment...' . "\n"; $text = 'Performance test has started. Doing a test of 50 virtual users for 3 minutes...'; $fields = array(array('title' => 'Site', 'value' => $_ENV['PANTHEON_SITE_NAME'], 'short' => 'true'), array('title' => 'Environment', 'value' => '<http://' . $_ENV['PANTHEON_ENVIRONMENT'] . '-' . $_ENV['PANTHEON_SITE_NAME'] . '.pantheonsite.io|' . $_ENV['PANTHEON_ENVIRONMENT'] . '>', 'short' => 'true'), array('title' => 'By', 'value' => $_POST['user_email'], 'short' => 'true'), array('title' => 'Message', 'value' => $text, 'short' => 'false')); $attachment = array('fallback' => $text, 'color' => 'warning', 'fields' => $fields); _slack_tell($text, $slack_channel_name, $slack_user_name, $slack_user_icon, false, $attachment); $curl = curl_init(); $curl_options = array(CURLOPT_URL => 'https://api.loadimpact.com/v2/test-configs/' . $test_config_id . '/start', CURLOPT_USERPWD => $api_key . ':', CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_RETURNTRANSFER => 1, CURLOPT_POST => 1); curl_setopt_array($curl, $curl_options); $curl_response = json_decode(curl_exec($curl));
<?php // Secrets helper function require_once dirname(__FILE__) . '/secrets_helper.php'; // Load Slack helper functions require_once dirname(__FILE__) . '/slack_helper.php'; $secrets = _get_secrets(array('slack_channel', 'live_url')); // Provide the Slack Details $slack_channel_name = $secrets['slack_channel']; $slack_user_name = 'WP-CLI-on-Pantheon'; $slack_user_icon = $secrets['live_url'] . '/wp-content/uploads/icons/wp-cfm.png'; // Activate the wp-cfm plugin exec('wp plugin activate wp-cfm 2>&1'); // Automagically import config into WP-CFM site upon code deployment $config_map = array('test' => 'pantheon_live', 'test' => 'pantheon_test', 'dev' => 'pantheon_dev', 'default' => 'pantheon_dev'); $config_name = array_key_exists(PANTHEON_ENVIRONMENT, $config_map) ? $config_map[PANTHEON_ENVIRONMENT] : $config_map['default']; _slack_tell('Importation of WordPress WP-CFM Default Configuration on the ' . PANTHEON_ENVIRONMENT . ' environment is starting...', $slack_channel_name, $slack_user_name, $slack_user_icon); exec('wp config pull ' . $config_name . ' 2>&1', $output); if (count($output) > 0) { $output = preg_replace('/\\s+/', ' ', array_slice($output, 1, -1)); $output = str_replace(' update', ' [update]', $output); $output = str_replace(' create', ' [create]', $output); $output = str_replace(' delete', ' [delete]', $output); $output = implode($output, "\n"); $output = rtrim($output); _slack_tell($output, $slack_channel_name, $slack_user_name, $slack_user_icon, '#A9A9A9'); } _slack_tell('Importation of WordPress WP-CFM Default Configuration on the ' . PANTHEON_ENVIRONMENT . ' environment is complete.', $slack_channel_name, $slack_user_name, $slack_user_icon); $path = $_SERVER['DOCUMENT_ROOT'] . '/private/config'; $files = scandir($path); $files = array_diff(scandir($path), array('.', '..'));
/** * Helper Function to Alert Slack */ function _slack_tell($message, $slack_channel_name, $slack_user_name, $slack_icon_url, $left_color_bar = false, $attachment = false) { $defaults = array(); $secrets = _get_secrets(array('slack_url'), $defaults); _slack_notification($secrets['slack_url'], $slack_channel_name, $slack_user_name, $message, $slack_icon_url, $left_color_bar, $attachment); }
<?php // Load a secrets file. // See the included example.secrets.json and instructions in README. $secrets = _get_secrets('secrets.json'); //Create curl post request to hit the Jenkins webhook $curl = curl_init($secrets->jenkins_url); //Setup header with authentication curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic ' . base64_encode("{$secrets->username}:{$secrets->api_token}"))); //Declare request as a post and setup the fields curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, array('token' => $secrets->token)); //Execute the request $response = curl_exec($curl); // TODO: could produce some richer responses here. // Could even chain this to a slack notification. It's up to you! if ($response) { echo "Build Queued"; } else { echo "Build Failed"; } /** * Get secrets from secrets file. * * @param string $file path within files/private that has your json */ function _get_secrets($file) { $secrets_file = $_SERVER['HOME'] . '/files/private/' . $file; if (!file_exists($secrets_file)) { die('No secrets file found. Aborting!');
<?php // Secrets helper function require_once dirname(__FILE__) . '/secrets_helper.php'; // Load Slack helper functions require_once dirname(__FILE__) . '/slack_helper.php'; // An example of using Pantheon's Quicksilver technology to do // automatic visual regression testing using Spotbot.qa // Provide the API Key provided by Spotbot.qa $secrets = _get_secrets(array('spotbot_key', 'slack_channel', 'live_url', 'test_url')); $api_key = $secrets['spotbot_key']; // Provide the Project URL for the project on Spotbot.qa $project_url = $secrets['test_url']; // Provide the Slack Details $slack_channel_name = $secrets['slack_channel']; $slack_user_name = 'CrossBrowserTesting-with-Spotbot'; $slack_user_icon = $secrets['live_url'] . '/wp-content/uploads/icons/spotbot.png'; // If we are deploying to test, run a visual regression test // between the production environment and the testing environment. if (defined('PANTHEON_ENVIRONMENT') && PANTHEON_ENVIRONMENT == 'test') { echo 'Kicking off browser testing for the test site...' . "\n"; $curl = curl_init(); $curl_options = array(CURLOPT_URL => 'https://spotbot.qa/api/scans', CURLOPT_HTTPHEADER => array('Authorization: ' . $api_key), CURLOPT_POST => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => 'pageUrl=' . urlencode($project_url)); curl_setopt_array($curl, $curl_options); $curl_response = json_decode(curl_exec($curl)); curl_close($curl); if ($curl_response->status == 'ok') { echo "Check out the result here: " . $curl_response->result[0]->url . "\n"; $text = 'Kicking off a cross browser test using Chrome, Firefox, Internet Explorer, Android, and iOS...'; $fields = array(array('title' => 'Site', 'value' => $_ENV['PANTHEON_SITE_NAME'], 'short' => 'true'), array('title' => 'Environment', 'value' => '<http://' . $_ENV['PANTHEON_ENVIRONMENT'] . '-' . $_ENV['PANTHEON_SITE_NAME'] . '.pantheonsite.io|' . $_ENV['PANTHEON_ENVIRONMENT'] . '>', 'short' => 'true'), array('title' => 'By', 'value' => $_POST['user_email'], 'short' => 'true'), array('title' => 'Message', 'value' => $text, 'short' => 'false'), array('title' => 'Results', 'value' => $curl_response->result[0]->url, 'short' => false)); $attachment = array('fallback' => $text, 'color' => 'good', 'fields' => $fields);
<?php // Default values for parameters $defaults = array('hipchat_url' => 'https://api.hipchat.com/v2/room/', 'hipchat_room_id' => 'quicksilver'); // Load our hidden credentials. // See the README.md for instructions on storing secrets. $secrets = _get_secrets(array('hipchat_auth_token'), $defaults); // Add a slash on the end of the hipchat URL if it does not already have one. if ($secrets['hipchat_url'][strlen($secrets['hipchat_url']) - 1] != '/') { $secrets['hipchat_url'] .= '/'; } $workflow_description = ucfirst($_POST['stage']) . ' ' . str_replace('_', ' ', $_POST['wf_type']); // Customize the message based on the workflow type. Note that hipchat_notification.php // must appear in your pantheon.yml for each workflow type you wish to send notifications on. switch ($_POST['wf_type']) { case 'deploy': // Find out what tag we are on and get the annotation. $deploy_tag = `git describe --tags`; $deploy_message = $_POST['deploy_message']; // Prepare the message $url = 'https://dashboard.pantheon.io/sites/' . PANTHEON_SITE . '#' . PANTHEON_ENVIRONMENT . '/deploy'; $text = '<b>' . $_POST['user_fullname'] . '</b> deployed <a href="' . $url . '">' . $_ENV['PANTHEON_SITE_NAME'] . '</a><br /> <b>On branch "' . PANTHEON_ENVIRONMENT . '"</b><br />Workflow: ' . $workflow_description . '<br /> Deploy Message: ' . htmlentities($deploy_message); break; case 'sync_code': // Get the committer, hash, and message for the most recent commit. $committer = `git log -1 --pretty=%cn`; $email = `git log -1 --pretty=%ce`; $message = `git log -1 --pretty=%B`;
<?php // Default values for parameters $defaults = array('chikka_url' => 'https://post.chikka.com/smsapi/request', 'mobile_number' => 'xxxxxxxxxxxx'); // Load our hidden credentials. // See the README.md for instructions on storing secrets. $secrets = _get_secrets(array('chikka_client_id', 'chikka_client_secret', 'chikka_accesscode'), $defaults); $number = $secrets['mobile_number']; $workflow_description = ucfirst($_POST['stage']) . ' ' . str_replace('_', ' ', $_POST['wf_type']); // Customize the message based on the workflow type. Note that chikka_sms_notification.php // must appear in your pantheon.yml for each workflow type you wish to send notifications on. switch ($_POST['wf_type']) { case 'deploy': // Find out what tag we are on and get the annotation. $deploy_tag = `git describe --tags`; $deploy_message = $_POST['deploy_message']; // Prepare the message $text = $_POST['user_fullname'] . ' deployed ' . $_ENV['PANTHEON_SITE_NAME'] . ' On branch "' . PANTHEON_ENVIRONMENT . '"Workflow: ' . $workflow_description . ' Deploy Message: ' . htmlentities($deploy_message); break; case 'sync_code': // Get the committer, hash, and message for the most recent commit. $committer = `git log -1 --pretty=%cn`; $email = `git log -1 --pretty=%ce`; $message = `git log -1 --pretty=%B`; $hash = `git log -1 --pretty=%h`; // Prepare the message $text = $_POST['user_fullname'] . ' committed to' . $_ENV['PANTHEON_SITE_NAME'] . ' On branch "' . PANTHEON_ENVIRONMENT . '"Workflow: ' . $workflow_description . ' - ' . htmlentities($message);