cjgratacos
10/9/2017 - 12:25 AM

Deploy to Composer Drupal 8 to Pantheon on non Multidev environments. This is good for when pushing code from a release branch to Pantheon d

Deploy to Composer Drupal 8 to Pantheon on non Multidev environments. This is good for when pushing code from a release branch to Pantheon dev directly.

#!/usr/bin/env bash

# Deploy to Pantheon

# Variables needed for this script
#
# Project Variables
# $PROJECT_BASE_PATH: Base path for the D8 project
#
# Terminus Variables
# $TERMINUS_TOKEN: Pantheon Token for Terminus CLI
# $TERMINUS_SITE: Pantheon site name
# $TERMINUS_ENV: Pantheon site environment
# $TERMINUS_SITE_ID: Pantheon site UUID
#
# Commit and Git Info Variables
# $COMMIT_HASH: Travis Commit Hash
# $COMMIT_MESSAGE: Travis Commit Message
# $GIT_EMAIL: Git email
#
# SSH Variables
# $PUBLIC_SSH_KEY: Valid Public SSH Key configured with Pantheon
# $PRIVATE_SSH_KEY: Valid Private SSH Key 

echo "==========--- Begin Deployment Process ---=========="

echo "==========--- Pre Deployment Configuration ---=========="

echo "=========== Configure Travis Agent with SSH Certs for Managing Pantheon ==========="
# Adding github and pantheon's servers to non strict keychain
mkdir -p $HOME/.ssh    
echo -e "Host *\n\tStrictHostKeyChecking no\n" >> $HOME/.ssh/config
# echo public key to a file in ssh folder
echo -e $PUBLIC_SSH_KEY > ~/.ssh/pantheon_id_rsa.pub
# echo private key to a file in ssh folder
echo -e $PRIVATE_SSH_KEY > ~/.ssh/pantheon_id_rsa
# Change permissions of SSH Keys
chmod 600 ~/.ssh/pantheon_id_rsa*
# run ssh agent
eval `ssh-agent -s`
# run ssh add for adding the keys
ssh-add ~/.ssh/pantheon_id_rsa

echo "=========== Configure Variables ==========="
# Set Variable
TERMINUS=$HOME/vendor/bin/terminus
echo "=========== Install Composer Packages: Drush, Terminus, etc ==========="
# Installing prestissimo 
composer global require -n hirak/prestissimo
# Installing Terminus
composer --working-dir=$HOME require pantheon-systems/terminus
# Install Drush
composer --working-dir=$HOME require drush/drush "^8"
# Print Terminus version
$TERMINUS --version
# Create Terminus Directory
mkdir -p $HOME/.terminus/plugins
# Install Terminus Plugins
composer create-project -n -d $HOME/.terminus/plugins pantheon-systems/terminus-build-tools-plugin
# Install Terminus Secrets
composer create-project -n -d $HOME/.terminus/plugins pantheon-systems/terminus-secrets-plugin
# Install Terminus Composer Plugin
composer create-project -n -d $HOME/.terminus/plugins pantheon-systems/terminus-composer-plugin
# Install Terminus Drupal Console Plugin
composer create-project -n -d $HOME/.terminus/plugins pantheon-systems/terminus-drupal-console-plugin

echo "=========== Set git variables ==========="
# Set git User Email
git config --global user.email "$GIT_EMAIL"
# Set git User name
git config --global user.name "Travis CI"
# Set git core file mode to false
git config --global core.fileMode false

echo "=========== Configure Terminus ==========="
# Login Pantheon
$TERMINUS auth:login -n --machine-token="$TERMINUS_TOKEN"
# Set git as main connection
$TERMINUS connection:set $TERMINUS_SITE.$TERMINUS_ENV git
# Wake up the dev environment
$TERMINUS env:wake -n "$TERMINUS_SITE.$TERMINUS_ENV"


echo "==========--- Deployment ---=========="

echo "=========== Move to Project Base Directory ==========="
# move to Project Base Directory
cd $PROJECT_BASE_PATH

echo "=========== Composer Install ==========="
# Composer install
composer install --no-dev

echo "=========== Composer Build Assets ==========="
# Composer build assets | Remove the gitignore
composer build-assets

echo "=========== Git Add Pantheon remote, add files, commit and push ==========="
# Add pantheon git remote
git remote add pantheon ssh://codeserver.dev.$TERMINUS_SITE_ID@codeserver.dev.$TERMINUS_SITE_ID.drush.in:2222/~/repository.git
# Add all files to commit
git add -A
# Commit
git commit -m "Publishing Master to Master Branch from Github from commit [$TRAVIS_COMMIT], with message [$TRAVIS_COMMIT_MESSAGE]"
# Checkout to a new branch, in this moment HEAD is detached from origin master
git checkout -b pantheon-release
# Push to Pantheon
git push pantheon pantheon-release:master -u --force


echo "==========--- Post Deployment Configuration ---=========="

echo "=========== Update Pantheon environment ==========="
# Change to SFTP Mode
$TERMINUS connection:set $TERMINUS_SITE.$TERMINUS_ENV sftp
# Go to $PROJECT_BASE_PATH web directory, in theory where Drupal Core should be install
pushd $PROJECT_BASE_PATH/web
# Update DB
$TERMINUS -n drush "$TERMINUS_SITE.$TERMINUS_ENV" -- updatedb -y
# Import configs if config/system.site.yml doesn't exist
[ ! -f "config/system.site.yml" ] || $TERMINUS -n drush "$TERMINUS_SITE.$TERMINUS_ENV" -- config-import --yes
# Go back to previous directory
popd
# Change to Git Mode
$TERMINUS connection:set $TERMINUS_SITE.$TERMINUS_ENV git

echo "==========--- Terminated Deployment Process ---=========="