Deploying Web Applications with Capistrano

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.

F77c2737b0ec6c9f3a388881f7e78afc?s=128

Andrew Turner

April 18, 2015
Tweet

Transcript

  1. 3.

    Common Methods • Server-side Editing • FTP Upload / File

    Syncing • Remote Repositories • Webhooks / Git Hooks • Third Party Services • Package or Binary • Continuous Integration
  2. 4.

    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!
  3. 5.

    Capistrano is a remote multi-server automation tool …that provides repeatable

    deployments, versioned releases and easy rollbacks
  4. 6.

    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
  5. 7.

    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)
  6. 8.

    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
  7. 9.

    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
  8. 13.

    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.
  9. 14.

    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/
  10. 15.

    Configuration Details # Application Details # ================== set :application, 'my_app_name'

    set :repo_url, 'git@example.com: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/
  11. 16.

    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/
  12. 18.

    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
  13. 19.

    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
  14. 21.
  15. 22.

    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
  16. 23.
  17. 25.

    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
  18. 26.

    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
  19. 27.

    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
  20. 28.

    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
  21. 29.

    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