Share
Embed
Deploying Web Applications with Capistrano
How do you currently publish updates to your code? How do you undo changes if you've made a mistake?
Manually uploading files via FTP can lead to mistakes or downtime and are difficult to roll back. Other techniques such as remote repositories or web/git hooks are better but can limit your ability to control the way updates are applied to the website.
Come learn how to streamline and automate the process of both deploying and rolling back updates to your web application with Capistrano, a remote server automation tool.
Andrew Turner
April 18, 2015
Transcript
30 slides
-
Common Methods • Server-side Editing • FTP Upload / File
Syncing • Remote Repositories • Webhooks / Git Hooks • Third Party Services • Package or Binary • Continuous Integration -
Challenges • Often a manual process • Varying levels of
risk for error • Potential downtime • Little or no history of deployments • Process can be hard to document or easily distribute • Difficult to manage for multiple environments • Updates can be difficult to undo …especially when a deployment crashes the live site! -
Capistrano is a remote multi-server automation tool …that provides repeatable
deployments, versioned releases and easy rollbacks -
About Capistrano • Installed as a Ruby Gem • Works
with any language/project • Easily extensible for custom deployment requirements • Distributable across developers/teams • Includes multi-server and multi-environment deployments -
How Capistrano Works • Deployment scripts are tracked in project
repository • Initiate deployment commands locally • Leverages SSH to connect to server • Deploys code from your remote repository (not your local code) -
How Capistrano Works • Keeps releases in separate directories on
server • Live site is a symlink to latest deployed release • Retains log of deployment meta data (who, what, when) • Provides ability to easily rollback releases -
local server repository local connects to server via SSH server
updates project code from repository deploy command is issued on local server issues a new release and logs deployment -
Set Up / Directory Structure: local cap install Initialize Capistrano
in your project ├── Capfile ├── config/ │ ├── deploy/ │ │ ├── production.rb │ │ └── staging.rb │ └── deploy.rb └── lib/ └── capistrano/ └── tasks/ Note: Run this command in the root directory of your project. -
Configuration Details # Load DSL and set up stages require
'capistrano/setup' # Include default deployment tasks require 'capistrano/deploy' # Include additional tasks from other gems or Capistrano plugins # # require 'capistrano/composer' # require 'capistrano/laravel' # require 'capistrano/npm' # require 'capistrano/grunt' # Load custom tasks from `lib/capistrano/tasks` if you have any defined Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } Define and load Capistrano dependencies Capfile Documentation: http://capistranorb.com/documentation/getting-started/configuration/ -
Configuration Details # Application Details # ================== set :application, 'my_app_name'
set :repo_url, '[email protected]:me/my_repo.git' set :deploy_to, '/var/www/my_app_name' # Shared Assets # ================== set :linked_files, fetch(:linked_files, []).push('.env') set :linked_dirs, fetch(:linked_dirs, []).push('tmp', 'vendor') # Deployment Details # ================== set :scm, :git set :keep_releases, 5 set :log_level, :debug Set global deployment options config/deploy.rb Documentation: http://capistranorb.com/documentation/getting-started/configuration/ -
Configuration Details # Environment Details # ================== set :stage, :production
set :branch, 'master' # Server Details # ================== server 'example.com', user: 'deploy', roles: %w{web app db} set :ssh_options, { auth_methods: %w(publickey password), keys: %w(~/.ssh/id_rsa.pub), forward_agent: false } Set environment specific deployment options config/deploy/production.rb Documentation: http://capistranorb.com/documentation/getting-started/configuration/ -
Set Up / Directory Structure: server Set up / confirm
server requirements (cold start) Note: This command is still run locally but must be run after you finish configuring the environment deployment scripts (e.g. config/deploy/production.rb). cap production deploy:check -
Set Up / Directory Structure: server current is a symlink
to the latest deployed release in the releases directory. ├── current ├── releases/ │ ├── 20150330134948/ │ ├── 20150405103040/ │ └── 20150417092653/ ├── repo/ ├── shared/ └── revisions.log IMPORTANT: You must configure your server’s web root to point to the current symlink, e.g. /var/www/my_app_name/current -
Usage Deploy Rollback IMPORTANT: When you issue the rollback command,
it reverts to the next latest release and deletes the latest (bad) release. cap production deploy:rollback cap production deploy -
Custom Tasks & Hooks # Simple Custom Task # ==================
namespace :example do task :simple_task_name do on roles(:all) do # Custom task actions info "simple task!” end end end Documentation: http://capistranorb.com/documentation/getting-started/tasks/ # Before/After Hooks (existing custom task) # ================== before :starting, :simple_task_name # Before/After Hooks (inline custom task) # ================== after :finishing, :simple_task_name do on roles(:all) do # Custom task actions info "simple task!” end end -
Default Tasks cap deploy # Deploy a new release cap
deploy:check # Check required files and directories exist cap deploy:check:directories # Check shared and release directories exist cap deploy:check:linked_dirs # Check directories to be linked exist in shared cap deploy:check:linked_files # Check files to be linked exist in shared cap deploy:check:make_linked_dirs # Check directories of files to be linked exist in shared cap deploy:cleanup # Clean up old releases cap deploy:cleanup_rollback # Remove and archive rolled-back release cap deploy:finished # Finished cap deploy:finishing # Finish the deployment, clean up server(s) cap deploy:finishing_rollback # Finish the rollback, clean up server(s) cap deploy:log_revision # Log details of the deploy cap deploy:published # Published cap deploy:publishing # Publish the release cap deploy:revert_release # Revert to previous release timestamp cap deploy:reverted # Reverted cap deploy:reverting # Revert server(s) to previous release cap deploy:rollback # Rollback to previous release cap deploy:set_current_revision # Place a REVISION file in the current release path cap deploy:started # Started cap deploy:starting # Start a deployment, make sure server(s) ready cap deploy:symlink:linked_dirs # Symlink linked directories cap deploy:symlink:linked_files # Symlink linked files cap deploy:symlink:release # Symlink release to current cap deploy:symlink:shared # Symlink files and directories from shared to release cap deploy:updated # Updated cap deploy:updating # Update server(s) by setting up a new release cap install # Install Capistrano, cap install STAGES=staging,production cap --tasks -
Customizations Use variables to make it DRY Deploy specific commit
or default to branch set :branch, ENV['REVISION'] || 'master' set :deploy_to, '/var/www/#{fetch(:application)}/#{fetch(:stage)}' REVISION=b92ec94d87786cc2f34f78e45e158a5391c7f912 cap production deploy Initialize multiple custom stages cap install STAGES=development,qa,staging,production -
Customizations Integrate with OS X Notification Center Notify Slack about
deployments require 'slackistrano' require 'capistrano-nc/nc' set :slack_webhook, 'https://hooks.slack.com/services/XXX/XXX/XXX' https://github.com/supremegolf/slackistrano https://github.com/capistrano/notification-center -
Additional Resources • Capistrano RB Documentation http://capistranorb.com • Capistrano GitHub
https://github.com/capistrano • Capistrano Plugins https://github.com/capistrano-plugins • Capistrano Laravel https://github.com/capistrano/laravel • Capistrano Composer https://github.com/capistrano/composer • Deploying WordPress with Capistrano https://roots.io/screencasts/deploying-wordpress-with-capistrano/ • Slides to this deck https://joind.in/13561 or https://speakerdeck.com/galenandrew