Skip to content

Instantly share code, notes, and snippets.

@sergiojovanig
Last active August 31, 2016 09:57
Show Gist options
  • Save sergiojovanig/01b0a35d6d08dddfcc66389c702dfa64 to your computer and use it in GitHub Desktop.
Save sergiojovanig/01b0a35d6d08dddfcc66389c702dfa64 to your computer and use it in GitHub Desktop.
Capistrano Magento 2 tasks
require "capistrano/setup"
require "capistrano/deploy"
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
set :application, 'PROJECT_NAME'
set :repo_url, 'git@DOMAIN.org:USER/REPO.git'
append :linked_files, 'app/etc/env.php'
append :linked_dirs, 'pub/media', 'var/backups', 'var/composer_home', 'var/export', 'var/import', 'var/log', 'var/report', 'var/tmp'
set :keep_releases, 3
server 'DOMAIN.com:PORT', user: 'USER', roles: %w{app db web}
set :branch, 'develop'
set :tmp_dir, '/var/www/tmp'
set :deploy_to, '/var/www/app'
SSHKit.config.command_map[:php] = '/usr/local/php/7.0/bin/php'
before 'deploy:symlink:release', 'magento:db-backup'
before 'deploy:symlink:release', 'magento:deploy'
after 'deploy:failed', 'magento:db-rollback'
task "magento:db-backup" do
on roles(:app) do
if test("[ -d #{current_path} ]")
within current_path do
execute :php, 'bin/magento', 'setup:backup', '--db', '-n', '-q'
end
end
end
end
task "magento:deploy" do
on roles(:app) do
if test("[ -d #{current_path} ]")
within current_path do
execute :php, 'bin/magento', 'maintenance:enable', '-n', '-q'
end
end
if test("[ -d #{release_path} ]")
within release_path do
execute 'find', '.', '-type', 'f', '-exec', 'chmod 755 {} \\;';
execute 'find', '.', '-type', 'd', '-exec', 'chmod 755 {} \\;';
execute 'chmod', '775', 'var';
execute 'chmod', '775', 'pub';
execute 'mkdir', '-p', 'pub/static'
execute 'chmod', '-R', '775', 'pub/static';
execute 'mkdir', '-p', 'pub/media'
execute 'chmod', '-R', '775', 'pub/media';
execute 'chmod', '775', 'app/etc';
execute 'chmod', '775', 'app/etc/env.php';
execute 'chmod', '775', 'app/etc/config.php';
execute :php, 'bin/magento', 'setup:di:compile', '-n', '-q'
execute :php, 'bin/magento', 'cache:enable', '-n', '-q'
execute :php, 'bin/magento', 'cache:flush', '-n', '-q'
execute :php, 'bin/magento', 'deploy:mode:set', 'production', '--skip-compilation', '-n', '-q'
execute :php, 'bin/magento', 'setup:upgrade', '--keep-generated', '-n', '-q'
execute :php, 'bin/magento', 'setup:db-schema:upgrade', '-n', '-q'
execute :php, 'bin/magento', 'setup:db-data:upgrade', '-n', '-q'
execute :php, 'bin/magento', 'setup:static-content:deploy', 'es_ES', '-n', '-q'
execute :php, 'bin/magento', 'indexer:reindex', '-n', '-q'
end
end
if test("[ -d #{current_path} ]")
within current_path do
execute :php, 'bin/magento', 'maintenance:disable', '-n', '-q'
end
end
end
end
task "magento:db-rollback" do
on roles(:app) do
if test("[ -d #{current_path} ]")
within current_path do
execute :php, 'bin/magento', 'setup:rollback', '-d', '`ls var/backups -t | head -1`', '-n', '-q'
execute :php, 'bin/magento', 'maintenance:disable', '-n', '-q'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment