Skip to content

Instantly share code, notes, and snippets.

@kevtainer
Created January 7, 2016 21:39
Show Gist options
  • Save kevtainer/4f4e1eaedc41529da2ae to your computer and use it in GitHub Desktop.
Save kevtainer/4f4e1eaedc41529da2ae to your computer and use it in GitHub Desktop.
Bamboo CodeDeploy replacement
<?php
/**
* Class Deploy
*
* Bamboo CodeDeploy integration conveniently ignores .hidden files, and has been broken for quite some time.
* (https://jira.atlassian.com/browse/BAM-16226)
*
* This is a PHP script which does the job right. Any Linux (and probably Windows) based Bamboo build server will
* run this script natively. Currently this script will ship the entire contents of the working directory. Keep that in mind.
*
* This script leverages project environmental parameters and custom parameters for for application name and s3 deploy bucket.
* (see __construct() for which properties this method will lean on)
*
* Also, this script will ignore applicationStop failures (because I needed it to), grep for it and remove that parameter
* if you need that behavior restored.
*
* @todo add timestamp to stdout
* @todo make applicationStop a configuration option
* @todo everything else
*/
class Deploy {
const TIMEOUT = 300;
protected $current_time;
protected $az_deploy_id;
protected $package;
protected $bamboo_deploy_project;
protected $bamboo_working_directory;
protected $bamboo_deploy_version;
protected $application_name;
protected $s3_deploy_bucket;
protected $working_id;
public function __construct() {
$this->bamboo_deploy_project = getenv('bamboo_deploy_project');
$this->bamboo_working_directory = getenv('bamboo_working_directory');
$this->bamboo_deploy_version = getenv('bamboo_deploy_version');
$this->application_name = getenv('application_name');
$this->s3_deploy_bucket = getenv('s3_deploy_bucket');
$this->working_id = array_pop(explode("/", $this->bamboo_working_directory));
$this->push();
$this->createDeployment();
$this->getDeployment();
}
public function push() {
$output = [];
$this->package = sprintf("pkg-%s-%s-%s.zip",
$this->working_id,
$this->bamboo_deploy_project,
$this->bamboo_deploy_version
);
exec(sprintf("aws deploy push \\
--application-name %s \\
--description \"%s Deployment - %s:%s:%s\" \\
--s3-location s3://%s/%s \\
--source .",
$this->application_name,
$this->application_name,
$this->working_id,
$this->bamboo_deploy_project,
$this->bamboo_deploy_version,
$this->s3_deploy_bucket,
$this->package
), $output, $code);
$this->validateCode($output, $code);
print_r(sprintf("[PUSH] %s - uploaded successfully.\n", $this->package));
}
public function createDeployment() {
$output = [];
exec(sprintf("aws deploy create-deployment \\
--ignore-application-stop-failures \\
--application-name %s \\
--s3-location bucket=%s,key=%s,bundleType=zip \\
--deployment-group-name %s",
$this->application_name,
$this->s3_deploy_bucket,
$this->package,
$this->bamboo_deploy_project
), $output, $code);
$output = implode("\n", $output);
$this->validateCode($output, $code);
$this->az_deploy_id = json_decode($output, true);
print_r(sprintf("[CREATE] %s - created successfully.\n", $this->az_deploy_id['deploymentId']));
}
public function getDeployment() {
$this->current_time = time();
while ($this->current_time + self::TIMEOUT > time()) {
print_r(sprintf("[CHECK] %s - verifying deployment.\n", $this->az_deploy_id['deploymentId']));
sleep(10);
$output = [];
exec(sprintf("aws deploy get-deployment --deployment-id %s", $this->az_deploy_id['deploymentId']), $output, $code);
$output = implode("\n", $output);
$this->validateCode($output, $code);
$deploy_status = json_decode($output, true);
switch ($deploy_status['deploymentInfo']['status']) {
case 'Failed':
print_r("[CHECK] Deployment Failed. :(\n");
print_r($deploy_status);
exit(1);
break;
case 'Succeeded':
print_r("[CHECK] Deployment Succeeded. :)\n");
print_r($deploy_status);
exit(0);
break;
}
}
if ($this->current_time + self::TIMEOUT <= time()) {
print_r("[CHECK] Deployment Timeout. :(\n");
print_r($deploy_status);
exit(1);
}
print_r(sprintf("[CHECK] %s - deployment verified!", $this->az_deploy_id['deploymentId']));
}
public function validateCode($output, $code) {
if ($code != 0) {
echo "abnormal termination!\n";
print_r($output);
exit(1);
}
}
}
$deploy = new Deploy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment