Every feature a branch, every branch a dev environment... without breaking the bank! The perfect git-flow dev environment for small shops

mysql_user=‘root’ mysql_pw=‘your-mysql-password’ worktree_root="/var/www/public_html" http_root=“http://example.com” support_files="/home/git"

while read oldrev newrev ref do   branch=echo $ref |cut -d/ -f3   worktree="$worktree_root/$GL_REPO/$branch"   repo_sanitized=echo ${GL_REPO//[-._]/}   branch_sanitized=echo ${branch//[-._]/}   db_name="$repo_sanitized"_"$branch_sanitized"

  # Exit nicely if this is the gitolite-admin repo.   if [ $GL_REPO = ‘gitolite-admin’ ]; then     exit 0   fi

  # Did we delete a branch? Delete the DB and checkout   if [ $newrev = ‘0000000000000000000000000000000000000000’ ]; then     if [ ! $branch = ‘master’ ]; then       echo “Deleting working tree and DB for branch $branch.”       rm -rf $worktree       mysqladmin -f -u “$mysql_user” –password="$mysql_pw" drop $db_name     else       echo “Refusing to delete working tree and db for master branch.”       exit 1     fi   else     # Make the worktree if it doesn’t exist     if [ ! -d “$worktree” ]; then       mkdir -p $worktree     fi     # Check out the latest version in place.     git –work-tree=$worktree checkout -f -q $branch     echo “Checked out updated code to $http_root/$GL_REPO/$branch”     # Are we missing settings.php ? this could be a new branch or a fresh repo.     if [ ! -e “$worktree/sites/default/settings.php” ]; then       # Create settings.php from a template       cp “$support_files”/template.settings.php “$worktree”/sites/default/settings.php       sed -i -e “s/placeholder/$db_name/g” “$worktree”/sites/default/settings.php     fi     # Is it a new branch?     if [ $oldrev = ‘0000000000000000000000000000000000000000’ ]; then       # Create the DB       mysqladmin -u “$mysql_user” –password="$mysql_pw" create $db_name       # Copy the DB from master branch. This is asynchronous because some DBs take a loooong time.       if [ ! $branch = “master” ]; then         email=git log --format="%ae" "$newrev" -1         “$support_files”/clone-db-files.sh $GL_REPO $branch $email &         echo “New branch detected, setting up db and files directories. This can take some time, depending on the size of the site. We’ll email your git address ($email) when it’s ready to use.”       else         # New repo! Create the files directory and give them the install URL.         mkdir $worktree/sites/default/files         chmod ug+rwx $worktree/sites/default/files         echo “New repo detected, creating empty DB and files directory. Your site is prepared; go and install Drupal at  $http_root/$GL_REPO/$branch/install.php”       fi     fi     if [ ! $branch = ‘master’ ]; then       # so that HEAD always sits at master       git –work-tree=$worktree_root/$GL_REPO/master checkout -q -f master &     fi   fi done

mysql_user=‘root’ mysql_pw=‘mysql-password’ worktree_root="/var/www/public_html" http_root=“http://ec2-54-226-103-245.compute-1.amazonaws.com” logfile="/home/git/new-branches.log"

project=$1 branch=$2 email=$3 repo_sanitized=echo ${project//[-._]/} branch_sanitized=echo ${branch//[-._]/} db_name="$repo_sanitized"_"$branch_sanitized" mysql_start_vars=“SET AUTOCOMMIT=0; SET UNIQUE_CHECKS=0; SET FOREIGN_KEY_CHECKS=0; SET GLOBAL INNODB_FLUSH_LOG_AT_TRX_COMMIT=2;” mysql_stop_vars=“SET AUTOCOMMIT=1; SET UNIQUE_CHECKS=1; SET FOREIGN_KEY_CHECKS=1; SET GLOBAL INNODB_FLUSH_LOG_AT_TRX_COMMIT=1;”

Copy the DB from master branch

cat <(echo “$mysql_start_vars”) <(mysqldump –opt –quick -u “$mysql_user” –password="$mysql_pw" “$project”_master) <(echo “$mysql_stop_vars”) | mysql -u “$mysql_user” –password="$mysql_pw" $db_name >> $logfile &

Copy the files directories - all sites except “all”

cd $worktree_root/$project/master umask 002 if [ find ./sites -type d -not -path "*/sites/all/*" -name 'files' -print0 ]; then   find ./sites -type d -not -path “/sites/all/” -name ‘files’ -print0|xargs -0 -I{} cp -R –no-preserve=mode,ownership –parents “{}” $worktree_root/$project/$branch/ >> $logfile & wait   # Certain systems don’t respect no-preserve in copy, so this is a just-in-case to make sure file modes are OK   cd $worktree_root/$project/$branch   find ./sites -not -path “/sites/all/” -name ‘files’ -print0 |xargs -0 -i{} chmod -R ug+rw “{}” else   echo “WARN: No files directories found in Master branch checkout.” fi

wait for the previous commands to finish processing

wait

Send confirmation email

message=“Your new branch environment for $branch is ready to use. The database and files have been copied from the master branch, and code is checked out in place. You can access the new environment at $http_root/$project/$branch.”

echo $message | /usr/bin/mail -s “Environment is ready for $project branch: $branch” $email

// Git flow settings.php template

$databases = array (   ‘default’ =>   array (     ‘default’ =>     array (       ‘database’ => ‘placeholder’,       ‘username’ => ‘your_mysql_username_here’,       ‘password’ => ‘your_mysql_password_here’,       ‘host’ => ’localhost’,       ‘port’ => ‘’,       ‘driver’ => ‘mysql’,       ‘prefix’ => ‘’,     ),   ), );

5rings settings include. If you got custom settings, put em in here.

if (file_exists(‘sites/default/5rings.settings.php’)) {   include(‘sites/default/5rings.settings.php’); }

comments powered by Disqus