Skip to content

Instantly share code, notes, and snippets.

@bakura10 bakura10/deploy.php
Created Jul 16, 2014

Embed
What would you like to do?
<?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
You can’t perform that action at this time.