Automated deployments to Acquia. Cloud API

When you set up your Continuous Integration you really would like to set your deployments automatically. If you use Acquia hosting for your website it does make a lot of sense to use all environments in your workflow. But how you can automate deployments to these environments without touching UI (copying database, files, deploying code)?

The answer is in Cloud API


You can call them either with drush command or curl request. We will touch the drush commands approach in this post.

I personally was heavily involved in workflow called CIBox that uses separate from Acquia github repo.

I used 'master' branch to deploy to DEV environment. But both STAGING and PRODUCTION environments are tag based.

DEV environment deployment


First step of deployment for me was to sync the repositories.

cd /var/git/acquia
git pull github master
git push origin master 
 # Sleep for 30 seconds. We expect Acquia to update the code.
sleep 30

Little note: all these commands are run on CI server for me. So you will find plenty of ssh and scp to Acquia servers later.

Repository /var/git/acquia is a clone from hosting repo but with set up remote to our own private repo. If you use hosting repo as primary you probably won't need this step.

In Acquia UI I have set up DEV environment to follow master branch. So code gets deployed automatically.

In my CI set up, I keep copy of project's database on CI server to use it in all builds. So I deploy this db to DEV environment as next step. Workflow diagram looks like this.

Code snippet

# Copy db to DEV server
scp /var/www/backup/DBNAME.sql.gz PROJECT.dev@staging-XXXX.prod.hosting.acquia.com:/home/PROJECT/proddump.sql.gz
# Deploy db on DEV server
ssh -t PROJECT.dev@staging-XXXX.prod.hosting.acquia.com 'rm -rf /home/PROJECT/DBNAME.sql && gunzip /home/PROJECT/proddump.sql.gz && cd /var/www/html/PROJECT.dev/docroot && drush sql-drop -y && `drush sql-connect` < /home/PROJECT/proddump.sql'

Remember in order to run this operation you need to set up your ssh keys so jenkins user (I use Jenkins as CI tool) can go to Acquia servers without password being requested.

And the last step is run all the updates, registry rebuilds and whatever your project requires.

# Run registry rebuild and clear caches
ssh -t PROJECT.dev@staging-XXXX.prod.hosting.acquia.com 'cd /var/www/html/PROJECT.dev/docroot && drush php-eval "registry_rebuild();" && drush cc all -y'
 
# Run hook updates
ssh -t PROJECT.dev@staging-XXXX.prod.hosting.acquia.com 'cd /var/www/html/PROJECT.dev/docroot && drush -y updb'

These commands actually can be set up with drush aliases. But I used terminal approach as already using it for deploying database. Just consistency reasons.

Another part I skip here is copying files over. We don't do that. Instead we enable stage_file_proxy on DEV and STAGE environments and point them to PROD so files got copied over upon request. This saves plenty of space.

STAGE environment deployment


As staging environment uses tags we need to change our code deployment part.

In order to use Cloud API you need to set up special private key in Acquia UI. Please review https://docs.acquia.com/cloud/api/auth for more details.

After setting up the key, ssh to DEV server and run command 'drush ac-api-login' and provide your email and key to it. In this way you will set up all your credentials and be able to run Cloud API drush commands.

And now, we can deploy the code.

ssh -t PROJECT.dev@staging-XXXX.prod.hosting.acquia.com 'cd /var/www/html/PROJECT.dev/docroot && drush @PROJECT.dev ac-code-deploy test --verbose'
 
# Sleep for 30 seconds. We expect Acquia to update the code.
sleep 30

This will deploy the code from DEV environment to STAGE and adds the tag automatically. Basically it mimics the operation of dragging the code bar in Acquia UI.

All other steps (database deployment and cache clear) are the same as with DEV environment.

PROD environment deployments


Production deployment is going to be the same as STAGE with only difference we need to ssh to STAGE server to deploy the code. And of course we do not to deploy database anywhere.

I am sure in your projects you might need to add some more steps. Maybe reindexing solr, or clearing varnish caches. All these can be done with drush commands.

How do you do deployments? Please share your experience in comments.