Auto-Deployments for your 'Vanilla Servers'

Ever felt envious about pushing to master then hitting your server to find your changes automatically deployed? but you don't wanna pay for a service like Envoyer?

Codeship is a Continuous Integration service, but they also offer auto-deployments too. Now, Codeship supports many services out-of-the-box like Heroku and Envoyer, but if you're using a "vanilla" server, you're out-of-luck and you'll have to write your own deployment script, unless...

Laravel Envoy to the Rescue

For this to work, you'll need to able to use Laravel Envoy (not Envoyer) on your projects.

if you don't know what Envoy is, it's a package from Laravel that lets you define your deployment scripts using Blade syntax and then execute them remotely over SSH.

this opens up a door for us to make use of Envoy to make Codeship deploy our code to the server.

Setting Up Codeship

If you don't have an account on Codeship you can go ahead and create one, they have a free plan and it doesn't require a credit card.

they'll ask you to create a new organization and a new project. after connecting codeship to your project on your git server, choose the Codeship Basic plan.

when choosing the Setup Commands choose [PHP] Laravel, this will auto-generate some basic scaffolding to use CI with Laravel, feel free to change this as you see fit for your project.

for me, I changed the phpenv local to 7.1 and deleted the php artisan migrate line since I'm using an in-memory database for my tests.

Note: If you don't have tests for your project, you can just add a single test with $this->assertTrue(true) in it to make it always pass.

now make a test commit to make sure everything is hooked up right, you should see a passing build in the Builds tab.

then in the project settings head to the Deploy tab, listen on branch master with the branch is exactly option, and create a custom script containing the following two lines.

composer global require laravel/envoy
envoy run production

this will wrap up setting up Codeship, next we'll need to create an Envoy file.

Setting Up Envoy

This will greatly depend on your specific project and server configuration, but I'll show you a simple "standard" deployment script.

create a file in your project's root called Envoy.blade.php, and define your script in there. if you need more info on Envoy you can read the docs here.

if you're feeling lazy or want an example to look at, you can find one here too.

Setting Up The Server

Server's configuration is fairly simple,

if you're setting up a server from scratch, I've written a post on that here

log in using a user with root privileges, and create a new user to make Codeship connect to using SSH

$ sudo adduser

complete the wizard and update the Envoy file accordingly, next we'll need to make sure Codeship can actually connect to our server but without exposing it to the public.

if you head to the General tab in the project settings on Codeship, scroll down and take the SSH Public Key, and update the authorized_keys for the deployment user

$ su [deployment-user]
$ cd ~
$ mkdir .ssh
$ touch .ssh/authorized_keys
$ echo '[copied-codeship-public-ssh-key]' > .ssh/authorized_keys
$ exit

then make sure the user has access to the project files.. one way is giving write access to the group and joining the user to that group.

$ sudo chmod -R g+w /path/to/project/
$ sudo pw groupmod [project-owning-group] -m [username]

This is pretty much it, just commit your changes to Envoy and push to master, you should see a build succeed and your changes deployed to your server automatically.

If you're having trouble feel free to hit me at [email protected]