Skip to content

Instantly share code, notes, and snippets.

@ostark
Created May 23, 2017 14:21
Show Gist options
  • Save ostark/0c500b6a8b865ddf1d397ede9757aac6 to your computer and use it in GitHub Desktop.
Save ostark/0c500b6a8b865ddf1d397ede9757aac6 to your computer and use it in GitHub Desktop.
use Bitbucket Pipelines with fortrabbit
image: php:7.1
pipelines:
branches:
master: # this branch gets deployed
- step:
script:
- apt-get update && apt-get install -y unzip git rsync
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install
# change APP and REGION
- php deploy.php APP REGION
<?php
if (count($argv) < 3) {
error_log("missing arguments, call {$argv[0]} app-name region");
exit(1);
}
if (!in_array($argv[2], ['us1','eu2'])) {
error_log("second argument must be eu2 or us1");
exit(1);
}
$frApp = $argv[1];
$frRegion = $argv[2];
$frRepoUrl = sprintf('%s@deploy.%s.frbit.com:%s.git', $frApp, $frRegion, $frApp);
$frRepoDir = sprintf('%s/fr-repo', sys_get_temp_dir());
$gitAuthorEmail = "deploy@bitbucket.com";
$gitAuthorName = "BitBucket";
$curDir = getcwd();
// Simulate changes, which are not in the Git repo, but generated in build
`date > the-current-date`;
// Get commit so it can be forwarded
echo "# Gathering commit message\n";
$lastCommitMessage =`git log -1 --pretty=%B`;
$lastCommitMessage = preg_replace('/["\'\n\r]/', '', $lastCommitMessage);
echo " > $lastCommitMessage (".getenv('BITBUCKET_BRANCH').")\n\n";
// Update SSH config:
// StrictHostKeyChecking=no is not a secure setting, but "yes" or "ask" will
// require read from /dev/tty, which is not available.
// Should be replaced by adding the fingerprint to ~/.ssh/known_hosts instead.
echo "# Updating SSH config\n";
`grep -q PreferredAuthentications ~/.ssh/config || echo "PreferredAuthentications publickey" >> ~/.ssh/config`;
`grep -q StrictHostKeyChecking ~/.ssh/config || echo "StrictHostKeyChecking no" >> ~/.ssh/config`;
//`grep -q LogLevel ~/.ssh/config || echo "LogLevel DEBUG3" >> ~/.ssh/config`;
echo " > Done\n\n";
// Git author must be set, or STDIN is again required
echo "# Init git\n";
`git config --global user.email "$gitAuthorEmail"`;
`git config --global user.name "$gitAuthorName"`;
echo " > Done\n\n";
// make sure fortrabbit repo is cloned - or pulled
if (file_exists("$frRepoDir/.git")) {
chdir($frRepoDir);
echo "# Updating \"$frRepoUrl\" repo in \"$frRepoDir\"\n";
echo `git pull`;
} else {
echo "# Cloning \"$frRepoUrl\" repo into \"$frRepoDir\"\n";
chdir(dirname($frRepoDir));
$command = "git clone \"$frRepoUrl\" \"$frRepoDir\"";
echo `git clone "$frRepoUrl" "$frRepoDir"`;
}
echo " > Done\n\n";
// Abort in case previous clone failed
if (!file_exists("$frRepoDir/.git")) {
error_log("Could not clone/update repo from fortrabbit");
exit(1);
}
// Sync all changed files, including those which were generated during build
echo "# Syncing changes\n";
echo `rsync -avC --delete-after --exclude=/vendor/ "$curDir"/ "$frRepoDir"/`;
echo " > Done\n\n";
// Commit & push everything to fortrabbit
echo "# Deploying changes\n";
chdir($frRepoDir);
echo `git add -Av`;
echo `git commit -am "$lastCommitMessage"`;
echo `git push origin master`;
echo " > Done\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment