Created
July 11, 2017 19:25
-
-
Save myles/a73f640d63b45dcaefb3026ba726d243 to your computer and use it in GitHub Desktop.
Fabric File for deploying a Laravel website
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from fabric.api import cd, env, task, run | |
from fabric.contrib.project import rsync_project | |
from fabric.utils import puts | |
env.hosts = ['127.0.0.1'] | |
env.user = 'serverpilot' | |
env.app_dir = '~/apps/laravel/' | |
env.releases_dir = '~/.releases/laravel' | |
env.db_backup_dir = '~/.backups/laravel' | |
env.now = datetime.datetime.now().isoformat().replace(':', '-') | |
@task() | |
def versions(): | |
""" | |
What is the current version of PHP running. | |
Used for testing access to the server. | |
""" | |
run('php -v') | |
run('composer -V') | |
@task | |
def backup(): | |
""" | |
Backup the current version. | |
""" | |
run('cp -r {app_dir} {releases_dir}/{now}'.format(**env)) | |
run('mysqldump -uuser -ppasswod database > ' | |
'{db_backup_dir}/{now}.sql'.format(**env)) | |
@task | |
def push(): | |
""" | |
Push the source code from local to the server. | |
""" | |
rsync_project(remote_dir=env.app_dir, | |
local_dir='.', | |
extra_opts="--filter=':- ./.gitignore'", | |
exclude=['.git', '.gitignore', '.gitattributes', | |
'fabfile.py', 'fabfile.pyc', 'bootstrap/cache/', | |
'storage/framework/sessions/', | |
'storage/framework/views/']) | |
@task | |
def composer_install(): | |
""" | |
Install the PHP dependecies with composer. | |
""" | |
with cd(env.app_dir): | |
run('composer install') | |
@task | |
def migrate(): | |
""" | |
Migrate the database to the latest version. | |
""" | |
with cd(env.app_dir): | |
run('php artisan migrate --force') | |
@task | |
def cleanup(): | |
""" | |
Cleanup cached files. | |
""" | |
with cd(env.app_dir): | |
run('php artisan view:clear') | |
run('composer dump-autoload') | |
@task | |
def ship_it(hard=False): | |
""" | |
Deploy the application. | |
""" | |
backup() | |
push() | |
if hard: | |
composer_install() | |
migrate() | |
cleanup() | |
# Draw a ship | |
puts(" | | | ") | |
puts(" )_) )_) )_) ") | |
puts(" )___))___))___)\ ") | |
puts(" )____)____)_____)\\ ") | |
puts(" _____|____|____|____\\\__ ") | |
puts(" ---------\ /--------- ") | |
puts(" ^^^^^ ^^^^^^^^^^^^^^^^^^^^^ ") | |
puts(" ^^^^ ^^^^ ^^^ ^^ ") | |
puts(" ^^^^ ^^^ ") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment