Home » Web Dev » Developing a CI Process for Laravel with Codeship and Forge

About Taylor Jones

Taylor Jones
Taylor Jones is a software chef at IZEA

Developing a CI Process for Laravel with Codeship and Forge

Codeship empowers developers to efficiently test and deploy web applications with a basic command. Today, I want to demonstrate some of the platform’s features through testing and deploying a Laravel application.

To go through this tutorial, you’re going to need a few things:

  1. A local instance of Docker Community Edition
  2. A Codeship account
  3. A Laravel Forge account – requires subscription
  4. A DigitalOcean or Amazon Web Services account
  5. Some sort of source-control hosting account (eg, GitHub, Bitbucket, GitLab)
  6. A local download of our application found here
  7. A hosted (forked) instance of this application on the source-control hosting account of your choice

Our Laravel Application

In this article, we’ll be testing and deploying an existing To Do list Laravel application whose source code can be found here.

Our application is fairly bare-bones and only has a few tests. The focus here isn’t necessarily to show a deep dive of Laravel. Rather, we’re more focused on how to continuously test and deploy new changes to our application. Once you have a feel for the process outlined here, I encourage you to try the same series of steps with your own personal Laravel application!

Before we move on, there are a few points of interest about our application:

  1. PostgreSQL is our database of choice.
  2. We’re running PHP 5.6.
  3. We’re testing our application with PHP Unit.

These three factors will influence how we set up our deployment and testing pipelines. Anything outside of this stack may require additional steps.

With this in mind, let’s move on to setting up our project on Codeship!

Setting up our Codeship project

If you don’t have a Codeship account, you can create one here. Otherwise, log into your account and follow the initial steps to create a new project.

Choose the source-control hosting of your choice and connect the application to Codeship. When prompted, select the Codeship Basic option. On the Configure Your Tests step, choose the [PHP] Laravel option. This will add in basic PHP setup and testing commands within our application. The default commands provide us with just about everything we would need, but we need another addition:

setup commands

# We support all major PHP versions. Please see our documentation for a full list.
# https://documentation.codeship.com/basic/languages-frameworks/php/
#
# By default we use the latest PHP version from the 5.5 release branch, but Laravel
# requires at least version 5.6.4
phpenv local 5.6

# Prepare cache directory and install dependencies
mkdir -p ./bootstrap/cache

# NEW ADDITION - Create Postgresql Database
psql -c "CREATE DATABASE todos"

composer install --no-interaction

# Prepare the test database
php artisan migrate

This simple code edition will allow us to create a PostgreSQL database for running our tests on.

If you look below at your test pipeline, you should see that PHP Unit is your default option. If you want to use another testing framework, you’re absolutely free to do that! However, we’re going to continue forward using PHP Unit. Our call to PHP Unit should look like this:

test pipeline

vendor/bin/phpunit

This will call your test runner once your application is set up on Codeship’s servers.

Finally, there’s one more step that we need to complete before moving onto Laravel Forge: setting the Codeship build environment.

Under Project Settings, select the Environment tab. Within this tab, you should be able to edit and create various environmental variables for your build environment. Our application will use the following environmental variables:

DB_CONNECTION=psql
DB_DATABASE=todos
DB_USERNAME=postgres
DB_PASSWORD=passw0rd
DB_HOST=127.0.0.1
APP_ENV=development

With these variables set, we should now be able to complete a successful build! Push new changes or force-push your existing application to trigger a new build on Codeship. Once you find that the build succeeds, we can move on to configuring Laravel Forge.

Setting up Laravel Forge

As I mentioned earlier, you’re going to need a Laravel Forge account. Unfortunately, Forge isnt a free service, so you’re going to have to subscribe to it before continuing.

When you create your account, you have a few options for creating a server. In this article, we’re going to use DigitalOcean. However, as usual, you’re free to blaze your own pathway here!

You’ll want to create a server that has at least 1 GB of RAM within it. Our bare-bones To Do list application works fine with this amount of RAM. However, many larger, more complex applications may not be able to handle only 1GB RAM. With this in mind, pick your server preferences and create the server. The creation process should take a few minutes.

Once the server is created, we’ll connect our Laravel application from source control. Follow the instructions for the source-control provider of your choice. If you’re using the To Do list application, our deploy will fail on the first try. This is because we haven’t set the environmental variables yet.

Under the Site Details tab, there should be an option entitled Environment. We’ll want to employ a similar strategy that we implemented in our Codeship configuration. Here’s how it should look:

APP_ENV=production
APP_KEY=base64:SngFlWlyl3m2YRIVH6OFD9alCJ2WA+B5KGP+4JZXWKc=
APP_DEBUG=false
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=forge
DB_USERNAME=forge
DB_PASSWORD=[randomly generated password]

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1

With this configuration, we should be able to press the Deploy Now button and successfully deploy our application on Forge.

Connecting Forge and Codeship

So far, we’ve got two awesome but independent processes for testing and deploying code. However, we want to connect these two processes so that they do something like this:

  1. Code changes are pushed to a deployable branch.
  2. New code changes are tested on Codeship.
  3. If the Codeship build passes, send a deploy request to Laravel Forge.
  4. Laravel Forge pulls in the tested code changes and attempts to deploy them in a production environment.

This sounds pretty neat, but how do we do this?

Under your App Details in Laravel Forge, look for a section entitled Deployment Trigger URL. This should contain a URL with a token at the end of it. Take that URL and copy it.

Now, log back into your Codeship project and look for the Deployment tab in your Project Settings. Select the Custom Script option and enter the following:

wget [your copied Laravel forge url]

This command will send the signal to Laravel Forge to deploy your application. Now, all we have to do is trigger another build on Codeship and we’ll see these two services talk together!

Wrapping Up

With all of these elements tied together, we’ve created a efficient CI testing and deployment pipeline that allows us to simply push code changes to a deployment branch, and have them tested and deployed upon success.

While awesome processes like these allow you to deploy clean code faster, they’re merely just the beginning of a robust CI process. With many of the other features that Codeship offers, you can find even more ways to automatically test and deploy your application!

Do you want to know how to develop your skillset to become a Web Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. Building web apps with Node.js

2. HTML5 Programming Cookbook

3. CSS Programming Cookbook

4. AngularJS Programming Cookbook

5. jQuery Programming Cookbook

6. Bootstrap Programming Cookbook

 

and many more ....

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Want to take your WEB dev skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • PHP Programming Cookbook
  • jQuery Programming Cookbook
  • Bootstrap Programming Cookbook
  • Building WEB Apps with Node.js
  • CSS Programming Cookbook
  • HTML5 Programming Cookbook
  • AngularJS Programming Cookbook