How to implement real Continuous Delivery for Drupal

  • Last updated
  • 2 minute read

Goal

Fully automate code delivery to production through Code Studio

Prerequisites

  • Access to Acquia Code Studio

Overview

Imagine being able to release improvements to your Drupal application multiple times per day without fear of regressions. Code Studio makes that possible by offering the ability to implement real Continuous Delivery (CD). That means delivering improvements to production continuously, without toil or an excess of human process. How?

With Code Studio, every change can be tested automatically. Review environments can be served to you on a silver platter. Approval can be enforced and deployment can be fully automated. Your Drupal code will be higher quality, more stable, and more secure. When a new feature is ready, it can ship with a single button click. Merge the code and it goes straight to production.

Scary? Contrary to what you might think, going faster in the world of development actually reduces risk. Increasing automation means less human error. Making changes more often means that each change is smaller and lower risk. Reducing bugs and incidents in turn creates more time for more innovation and value. 

Code Studio enables you to embrace Continuous Delivery and create a virtuous cycle that lets evolve toward greater productivity.

  1. Understand the new release process

    Today, your release process probably looks something like this:

    There are many manual steps that need to be done by a person on your team before a change can be considered fully “released” to production. And frankly, there are more steps than this. A graphic can only be so wide. You may also export config, backup databases, close bug tracker  releases, send a Slack announcement, etc. All of those things can be automated with Acquia too. In the end, your process will look like this:

  2. Set it up

    As described in Code Studio’s AutoDevOps, Code Studio provides a zero-configuration-required CI/CD build that is optimized for Drupal. Each time you make a commit it will be automatically tested in a variety of ways (e.g., PHPCS, PHPStan, PHPUnit, etc.). When you create a merge request, Code Studio will both test your code and also spin up a review environment (CDE) that you can directly access and test in a browser. When you click merge, it will automatically generate an artifact and deploy the change to a corresponding branch on the Acquia Cloud Platform.

    That’s all great, but what about the release process?

    Traditionally, you would create a tag, manually deploy it somewhere, and run through some type of “launch checklist”. But! If you so choose, we can automate that too. Just set the ACQUIA_JOBS_DEPLOY_TAG_ARTIFACT CI/CD variable to true in your Code Studio project.

    Note the "Protect variable" checkbox. You should either:

    1. Uncheck this so that it applies to all tags.
    2. Check this AND configure Code Studio to protect your tags automatically.

    When you do this, Code Studio will execute the following steps automatically each time that you create a tag in Code Studio:

    1. Generate a deployment artifact
    2. Tag the artifact
    3. Push the tag to Acquia Cloud
    4. Deploy that tag to production.

    If you’d rather automatically deploy to an environment other than production, you can! Just set the ACQUIA_CLOUD_DESTINATION_ENVIRONMENT_ID CI/CD variable to be the environment ID of your target environment.

    Finally, you can automate tasks that will happen after you deploy your code by using Acquia Cloud Hooks.

    In detail, these are the Acquia features that will help you accomplish each step:

  3. Copy our example

    Actually, we use a very similar process for the release of dev.acquia.com itself. Here’s an example of the Cloud Hook that we use each time that code is changed on one of our Acquia environments:

    
    #!/bin/sh
    #
    # Cloud Hook: post-code-update
    #
    # The post-code-update hook runs in response to code commits.
    # When you push commits to a Git branch, the post-code-update hooks runs for
    # each environment that is currently running that branch.
    #
    site="$1"
    target_env="$2"
    
    #
    # Set up
    #
    VENDOR_DIR=/var/www/html/$site.$target_env/vendor/bin
    
    #
    # Clear Caches
    #
    echo "Clearing caches..."
    $VENDOR_DIR/drush @$site.$target_env cache:rebuild --no-interaction
    $VENDOR_DIR/drush @$site.$target_env config:import --no-interaction -y
    
    #
    # If database updates are required, run them and clear caches.
    #
    if ! $VENDOR_DIR/drush @$site.$target_env updatedb:status --no-interaction --format=string 2>&1 | grep -q 'No pending updates'; then
      echo "$site.$target_env: There are outstanding database updates. Running updates..."
      $VENDOR_DIR/drush @$site.$target_env updatedb --no-interaction
      $VENDOR_DIR/drush @$site.$target_env cache:rebuild --no-interaction
    fi