Continuous Deployment

Using Bitbucket Pipelines with Docker

 My deployment process has evolved a great deal from dragging and dropping to Filezilla. I'm now using a Git repository hosted on Bitbucket.org which automatically deploys every time code is merged into master. This post describes how I was able to accomplish this and is divided into the following sections:

Process & Context

Configure Server

 Our server needs to be configured to allow a user to connect via SSH, to have access to our repo, and to execute necessary commands (git pull, php artisan migrate, composer install, etc...).

            # CREATE A USER
            adduser binarywhisperer

            # GIVE USER SUDO PERMISSIONS
            usermod -aG sudo username

            # BECOME THE USER
            su - binarywhisperer

            # CREATE AND READ SSH ACCESS KEY FOR BITBUCKET REPO
            ssh-keygen
            cat ~/.ssh/id_rsa.pub

            # GIVE USER FILE PERMISSIONS FOR REPO DIRECTORY
            sudo chown -R binarywhisperer:www-data /path/to/repo/binary-whisperer/
            sudo usermod -a -G www-data binarywhisperer
        

Create Docker Image

Docker Hub offers free accounts which allow you to host free Docker images. This section is focused on the code in your Docker image.

Docker image creation overview:

Dockerfile:

            FROM alpine:3.8

            RUN apk update && apk add openssh-client bash

            WORKDIR /

            ADD deployment.sh /

            RUN ["chmod", "+x", "/deployment.sh"]
        

deployment.sh:

            #!/bin/bash
            ssh binarywhisperer@45.76.253.75 'bash -s' << EOF
                cd /var/www/binary-whisperer
                git stash
                git pull origin master
                composer install --no-dev
                npm install --only=production
            EOF
        

Build, tag and push image:

            # BUILD
            docker build -t continuousdeployment .

            # TAG
            docker tag continuousdeployment brycebryce/continuous-deployment:latest

            # PUSH
            docker push brycebryce/continuous-deployment
        

Configure Bitbucket Pipelines

Bitbucket offers free private git repos with 50 minutes of free Bitbucket Pipelines up time.

Configuring Bitbucket Pipelines overview:

bitbucket-pipelines.yml:

            image: brycebryce/continuous-deployment
            pipelines:
              branches:
                master:
                - step:
                    script:
                    - /deployment.sh
        

Server user access

  Copy the access key from the Configure Server sections cat ~/.ssh/id_rsa.pub command and add it in your repo's Settings->Access Keys.

Enable pipelines:

  Enabling pipelines requires that you have a bibucket-pipelines.yml in your project and then you simply click enable in your repo's Settings->Pipelines->Settings.

Configure SSH access for Docker

  Creating SSH Access for your Docker image is 2 simple steps in your repo's Settings->Pipelines->SSH Keys. First generate a new SSH key and copy the key to the ~/.ssh/authorized_keys of the user your created on your server. Next add your server to the Docker image's Known Hosts by typing your IP address in the Host Address field, click the Fetch button, and then click the Add host button.

Benefit from Continuous Deployment

  If you've done everything correctly the next time you merge code into origin master, pipelines will automatically trigger and push your changes directly to your server. You can see the pipelines execution in your repo's Pipelines section.