Making the Build Server Build

Building on the Build Server

Let's get our build server to build and package the application completely before we deploy it.

We need to install a few things so it can build our application:

  1. Access to the Github repository
  2. PHP to pull in composer packages
  3. NodeJS Gulp packages

Git Repo

As our build server has the Github key for this repository, I can close it on the build server:

# Access available via ~/.ssh/id_github
#   and with help from ~/.ssh/config
# As "appuser"
cd ~/build-server
git clone git@github.com:Servers-for-Hackers/serialapp.git repo

Note: This server can access the Github repository because it contains SSH key ~/.ssh/id_github, whos public key was added to the Github repository in previous videos.

The ~/.ssh/config file used to make sure that key is used within Github for the appuser (deployment) system user looks like this:

Host github.com
    HostName github.com
    IdentityFile ~/.ssh/id_github
    User git
    IdentitiesOnly yes

This is automatically used when we clone, since the hostname used is github.com.

PHP Dependencies

We'll need PHP stuff to pull in Composer packages:

# As sudo user, admin

# Install PHP
sudo add-apt-repository -y ppa:ondrej/php5-5.6
sudo apt-get update
# Also install zip to zip up builds
sudo apt-get install -y \
    php5-cli php5-mcrypt \
    zip

# Install Composer
curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /usr/local/bin/composer

Node Dependencies

Our build server already has NodeJS, so when we build, we can install the application Node dependencies and run Gulp straight away:

npm install
./node_modules/.bin/gulp

We don't need to install anything special to make this happen.

Build It!

That should be it! We can now move a little bit of our build set to a local one. Here's a Bash script which outlines what that build process looks like:

#!/usr/bin/env bash

# Bail on first non-zero exit code
set -e

# Ensure this dir exists
mkdir -p ~/build-server/packages

# Get latest code and copy to releases
# Create new release and package directory
cd ~/build-server/repo
git checkout master
git pull origin master

RELEASE=`git rev-parse HEAD`
mkdir -p ~/build-server/releases/$RELEASE

git archive --worktree-attributes master | tar -x -C ~/build-server/releases/$RELEASE

# Build application
cd ~/build-server/releases/$RELEASE

npm install
./node_modules/.bin/gulp

composer install

# Package application
cd ~/build-server/releases
zip -r --exclude="*.git*" --exclude="*node_modules*" \
    ~/build-server/packages/${RELEASE}.zip ./${RELEASE}

Run the bash script! We also use the time command to give us some idea on time and memory usage:

time bash build.sh

We'll see a package built in our ~/build-server/packages directory now!

Resources