Making local environment and deployments easier using Docksal and Bitbucket Pipelines

About us

Taras Tsiuper - Senior php/Drupal developer

Nikita Pogrebnyak - Senior php/Drupal developer

  • Founded in 2006, with headquarters in Vancouver, Canada
  • Named #1 Drupal Agency by clutch.co
  • Active Drupal Community Participant
  • Working with clients such as Apple, Samsung, Disney, Warner Brothers, Adobe, and multiple universities.

Plan

  • What is Docksal
  • How to install and setup first project
  • Examples how to add different tools
  • What is Bitbucket pipelines
  • What we can do using pipelines
  • How we implement delivery integration using pipelines

What is Docksal

Docksal is an open-source tool created by FFW for defining and managing development environments. It brings together common tools (drush, Drupal Console, composer, PHP Code Sniffer), minimizes time spent on configuration, and ensures environment consistency throughout your continuous integration workflow.

 

Docksal uses Docker to create fully containerized environments. It also uses VirtualBox  to support MacOS and Windows.

How to install Docksal

curl -fsSL get.docksal.io | sh

fin vm start

With Docksal you also get fin, the command line tool used to manage your projects. Fin has built-in commands for every day tasks, like starting and stopping services, importing and exporting databases, initializing projects, executing bash commands inside the containers, and adding ssh keys. Fin is also easily extended with custom commands that can be universal to your computer, or unique for each project.

Setup new project

// See the list of commands.
fin help

// Create new project from scratch.
fin project create

Rebuild project

// Rebuild project.
fin init
#!/usr/bin/env bash

# Project initialization steps
echo -e "${green_bg} Step 1 ${NC}${green} Initializing local project configuration...${NC}"
echo-green "Making site directory 
chmod 755 "${SITEDIR_PATH}"

copy_settings_file "${SITEDIR_PATH}/example.settings.local.php" 
    "${SITEDIR_PATH}/settings.local.php"

if [[ $DOCKER_RUNNING == "true" ]]; then
	echo -e "${green_bg} Step 2 ${NC}${green} Recreating services...${NC}"
	fin reset -f
else
	echo -e "${green_bg} Step 2 ${NC}${green} Creating services...${NC}"
	fin up
fi

echo "Waiting 10s for MySQL to initialize...";
sleep 10

echo -e "${green_bg} Step 3 ${NC}${green} Installing site...${NC}"
fin exec "PHP_OPTIONS="'"-d sendmail_path=`which true`"'" 
    drush site-install -y --site-name='My Drupal 7 Site'"

if is_windows; then
	echo-green "Add ${VIRTUAL_HOST} to your hosts file (/etc/hosts), e.g.:"
	echo-green "192.168.64.100 ${VIRTUAL_HOST}"
	echo
fi

echo -en "${green_bg} DONE! ${NC} "
echo -e "Open ${yellow}http://${VIRTUAL_HOST}${NC} in your browser to verify the setup."
#!/usr/bin/env bash

site_install ()
{
    cd "$PROJECT_ROOT"

    # Download db.
    echo-green "Download db..."
    scp -i ./files/ci_server_key -o StrictHostKeyChecking=no 
        example@example.com:/var/backups/backup.sql.gz ./backup/prod.sql.gz

    # Import db.
    echo-green "Import db..."
    fin exec "gunzip < backup/dump.sql.gz | mysql -hdb -uuser -ppasswword database_name"

    cd "$DOCROOT_PATH"
    # Reset admin password
    echo-green "Reset admin password"
    fin drush upwd --password='123' 'admin'

    # Update db.
    echo-green "Run update db"
    fin drush updb -y

    # Clear cache.
    echo-green "Clear caches"
    fin drush cc all
}

Examples of how to add different tools

version: "2.1"
services:
  web:
    hostname: web
    image: docksal/web:1.0-apache2.2
    volumes:
      - project_root:/var/www:ro
    labels:
      - io.docksal.virtual-host=${VIRTUAL_HOST}
      - io.docksal.project-root=${PROJECT_ROOT}
    environment:
      - VIRTUAL_HOST=${VIRTUAL_HOST}
      - APACHE_DOCUMENTROOT=/var/www/${DOCROOT}
    depends_on:
      - cli

  # DB node
  db:
    hostname: db
    image: docksal/db:1.0-mysql-5.5
    ports:
      - ${MYSQL_PORT}
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}

  # CLI node
  cli:
    hostname: cli
    image: docksal/cli:1.0-php7
    volumes:
      - project_root:/var/www:rw
  

Bitbucket Pipelines

a continuous delivery service in Bitbucket's UI that lets you build, test and deploy your code.

Alternative to

  • Jenkins
  • Bamboo
  • TeamCity
  • Travis
  • ...

Main things you can do using Bitbucket Pipelines:

  • Build
  • Test
  • Deploy

Builds

Bitbucket Pipelines runs all your builds in Docker containers using an image that you specify at the beginning of your configuration file.

pipelines:
  branches:
    master:
      - step:
          script:
            - ./files/dev-deploy.sh
    staging:
      - step:
          script:
            - ./files/stage-deploy.sh
    production:
      - step:
          script:
            - ./files/prod-deploy.sh

Builds

#!/bin/bash

git remote add live repo@example:repo.git
git fetch live
git checkout live_dev
git merge master
git push live live_dev

# Wait for updating acquia server.
sleep 30

# Download backup from prod.
echo "Download backup."
scp -i ./files/ci_server_key -o StrictHostKeyChecking=no 
    prod@example.acquia.com:/var/backups/prod.sql.gz ./backup/prod.sql.gz

# Upload backup on dev.
echo "Upload backup."
ssh -i ./files/ci_server_key -o StrictHostKeyChecking=no dev@example.acquia.com 
    "rm /tmp/prod.sql.gz"
scp -i ./files/ci_server_key -o StrictHostKeyChecking=no ./backup/prod.sql.gz 
    dev@example.acquia.com:/tmp

echo "Import db."
ssh -i ./files/ci_server_key -o StrictHostKeyChecking=no 
tcrc.dev@staging-5808.prod.hosting.acquia.com "cd /tmp && gunzip -f prod.sql.gz 
    && cd /var/www/html/dev/docroot/ && drush sql-drop -y && \`drush sql-connect\` < /tmp/prod.sql"

# Connect to server and run updates.
ssh -i ./files/ci_server_key -o StrictHostKeyChecking=no 
    dev@example.acquia.com "cd /var/www/html/dev/docroot 
    && drush cc all && drush updb -y && drush upwd --password='123' 'admin'"

Builds

Questions ?

Made with Slides.com