Skip to content

Instantly share code, notes, and snippets.

@bakura10
Created July 16, 2014 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakura10/001ce1e6da734e9c6363 to your computer and use it in GitHub Desktop.
Save bakura10/001ce1e6da734e9c6363 to your computer and use it in GitHub Desktop.
<?php
use Aws\Common\Aws;
const AWS_ACCESS_KEY = '';
const AWS_SECRET_KEY = '';
const AWS_REGION = 'us-east-1';
const S3_BUCKET = '';
const EB_APPLICATION_NAME = '';
ob_start();
// Composer autoloading
if (file_exists('vendor/autoload.php')) {
$loader = include 'vendor/autoload.php';
}
$environments = [
'master' => ['env1', 'env2']
];
$gitHash = exec('git log -1 --format="%H"');
$branch = exec('git rev-parse --abbrev-ref HEAD');
exec('git log -1 --format="%B"', $commitMessage);
$commitMessage = reset($commitMessage);
if (!isset($environments[$branch])) {
echo 'No environments are bound to the branch ' . $branch . PHP_EOL;
die();
}
echo sprintf(
"\nBranch '%s' is going to be deployed to following environments: %s. Do you want to confirm? [y/N]\n",
$branch,
implode(', ', $environments[$branch])
);
flush();
ob_flush();
if (trim(fgets(STDIN)) !== 'y') {
die();
}
echo "\nCreating the archive... This may take some minutes!\n";
$archiveFileName = $gitHash . '.zip';
// Create the archive in the current branch
exec('vendor/bin/zfdeploy.php build ' . $archiveFileName . ' --composer=on --gitignore=on');
$aws = Aws::factory([
'key' => AWS_ACCESS_KEY,
'secret' => AWS_SECRET_KEY,
'region' => AWS_REGION
]);
/** @var \Aws\S3\S3Client $s3 */
$s3 = $aws->get('S3');
$s3->upload(S3_BUCKET, 'git-' . $gitHash . '.zip', file_get_contents($archiveFileName));
/** @var \Aws\ElasticBeanstalk\ElasticBeanstalkClient $elasticBeanstalkClient */
$elasticBeanstalkClient = $aws->get('ElasticBeanstalk');
$elasticBeanstalkClient->createApplicationVersion([
'ApplicationName' => EB_APPLICATION_NAME,
'Description' => $commitMessage,
'VersionLabel' => substr('git-' . $gitHash, 0, 11),
'SourceBundle' => [
'S3Bucket' => S3_BUCKET,
'S3Key' => 'git-' . $gitHash . '.zip'
]
]);
foreach ($environments[$branch] as $environment) {
$elasticBeanstalkClient->updateEnvironment([
'EnvironmentName' => $environment,
'VersionLabel' => substr('git-' . $gitHash, 0, 11)
]);
}
unlink($archiveFileName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment