Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Deploying Applications with Capistrano

Deploying Applications with Capistrano

Introduction to deploying web applications with Capistrano v3.

Presentation given at Dallas PHP meet up

Andrew Turner

March 11, 2014
Tweet

More Decks by Andrew Turner

Other Decks in Technology

Transcript

  1. Background • Originated in the Ruby/Rails community around 2006! •

    Written in Ruby but it can easily be used to deploy any language! • Installed as a Ruby gem! • Easy to get started on, but also provides advanced capabilities
  2. Background (cont) • Highly extensible for custom actions/tasks! • Multi-environment

    deployment support OOTB! • Provides ability to “rollback” to previous deployments! • Handles ignored dirs/files (e.g. uploads, cache, etc) well
  3. How Capistrano Works • Runs LOCAL…no “special” server configuration needed!

    • Connects directly to server(s) via SSH! • Automates tasks/commands on server! • Checks out code directly to server from remote! • Uses symlinks to achieve “nearly no downtime” deployments! • Keeps versioned “releases” for rolling back
  4. v2 ≠ v3 • v3 is a complete rewrite -

    not backwards compatible!! • v2 written in custom DSL! • v3 based on / extends Rake DSL! • v3 is…! • Faster! • More Modular! • Better built-in debugging! • Easier integration with provisioning tools – Puppet, Chef, etc
  5. Prerequisites • Local (your computer)! • Ruby 1.9 or newer!

    • Remote (your server)! • SSH access w/ permissions! • Git! • Optional: SSH key pair (used to access hosted repo)! • Repo (GitHub, Bitbucket, etc)! • Optional: Deploy Key (server needs pull access)
  6. Installation / Setup $ gem install capistrano Install Capistrano gem

    $ cap install Initialize Capistrano in your app/code
  7. Directories/Files: Local ├── Capfile ├── config │ ├── deploy │

    │ ├── production.rb │ │ └── staging.rb │ └── deploy.rb └── lib └── capistrano └── tasks http://capistranorb.com/documentation/getting-started/preparing-your-application/
  8. Capfile # Load DSL and Setup Up Stages require 'capistrano/setup'

    ! # Includes default deployment tasks require 'capistrano/deploy' ! # Loads custom tasks from `lib/capistrano/tasks' if you have any defined. Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r } Capfile
  9. Configure: Global # Application Details # ================== set :application, 'cap-demo'

    set :repo_url, '[email protected]:galenandrew/cap-demo.git' ! # Deployment Details # ================== set :scm, :git set :keep_releases, 3 ! # Global SSH Options (shared across all stages) # ================== set :ssh_options, { port: 22, forward_agent: false, auth_methods: %w(publickey), keys: %w(~/.ssh/id_rsa.pub) } Set global / shared deployment options config/deploy.rb
  10. Configure: Stages Set stage specific deployment options # Stage Details

    # ================== set :stage, :staging set :branch, 'master' ! # Server Details # ================== server 'staging.myapp.com', user: 'ubuntu', roles: %w{web app db} ! # Server Details (alt) role :app, %w{[email protected]} role :web, %w{[email protected]} role :db, %w{[email protected]} ! # Deployment Details # ================== set :deploy_to, "/var/www/site/staging" # Stage Details # ================== set :stage, :production set :branch, 'master' ! # Server Details # ================== server 'myapp.com', user: 'ubuntu', roles: %w{web app db} ! # Server Details (alt) role :app, %w{[email protected]} role :web, %w{[email protected]} role :db, %w{[email protected]} ! # Deployment Details # ================== set :deploy_to, "/var/www/site/prod" config/deploy/staging.rb config/deploy/production.rb
  11. 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: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
  12. DEPLOY! $ cap production deploy:check Check deploy and set up

    server $ cap production deploy Deploy!
  13. Task Flow: Deploy $ cap production deploy deploy:starting - start

    a deployment, make sure everything is ready deploy:started - started hook (for custom tasks) deploy:updating - update server(s) with a new release deploy:updated - updated hook deploy:publishing - publish the new release deploy:published - published hook deploy:finishing - finish the deployment, clean up everything deploy:finished - finished hook Note: Several “hook” tasks that you can link custom tasks into using after() and before()
  14. Directories/Files: Remote Server ├── current ├── releases │ ├── 20140311223313

    │ ├── 20140311223109 │ └── 20140311181949 ├── repo ├── shared └── revisions.log Note: “Current” is a symlink to the latest release. Make sure to point your virtual host web root to “current”.
  15. Task Flow: Rollback $ cap production deploy:rollback deploy:starting deploy:started deploy:reverting

    - revert server(s) to previous release deploy:reverted - reverted hook deploy:publishing deploy:published deploy:finishing_rollback - finish the rollback, clean up everything deploy:finished Note: Several “hook” tasks that you can link custom tasks into using after() and before()
  16. Tips / Tricks set :deploy_to, "/var/www/#{fetch(:application)}/#{fetch(:stage)}" Use variables to make

    it DRY # Default value for :linked_files is [] set :linked_files, %w{config/database.yml} ! # Default value for linked_dirs is [] set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system} Link shared files / dirs
  17. Tips / Tricks $ cap install STAGES=development,aws,staging,production Install more stages

    ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp } Dynamically deploy HEAD branch
  18. Tips / Tricks $ gem install require capistrano-nc Integrate with

    Mac OS X Notifications! https://github.com/capistrano/notification-center require 'capistrano-nc/nc'
  19. Resources • Capistrano RB Documentation
 http://capistranorb.com! • Capistrano v3 Release

    Announcement
 http://capistranorb.com/2013/06/01/release-announcement.html! • Capistrano GitHub
 https://github.com/capistrano/capistrano! • PHP & Capistrano 3: Notes to Self - Jeremy Kendall
 http://jeremykendall.net/2013/11/24/php-and-capistrano-3-notes-to-self/! • Using Capistrano v3 with Chef - Lee Hamby
 http://lee.hambley.name/2013/06/11/using-capistrano-v3-with-chef.html! • Slides to this Deck
 https://speakerdeck.com/galenandrew! • Sample Code
 https://github.com/galenandrew/cap-demo