Created
July 16, 2014 08:17
-
-
Save bakura10/001ce1e6da734e9c6363 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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