Share
Embed
Deploying Applications with Capistrano
Introduction to deploying web applications with Capistrano v3.
Presentation given at Dallas PHP meet up
Andrew Turner
March 11, 2014
Transcript
27 slides
-
– CapistranoRB.com “A remote server automation and deployment tool written
in Ruby.” automation deployment -
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 -
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 -
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 -
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 -
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) -
Installation / Setup $ gem install capistrano Install Capistrano gem
$ cap install Initialize Capistrano in your app/code -
Directories/Files: Local ├── Capfile ├── config │ ├── deploy │
│ ├── production.rb │ │ └── staging.rb │ └── deploy.rb └── lib └── capistrano └── tasks http://capistranorb.com/documentation/getting-started/preparing-your-application/ -
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 -
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 -
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 -
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 -
DEPLOY! $ cap production deploy:check Check deploy and set up
server $ cap production deploy Deploy! -
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() -
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”. -
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() -
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 -
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 -
Tips / Tricks $ gem install require capistrano-nc Integrate with
Mac OS X Notifications! https://github.com/capistrano/notification-center require 'capistrano-nc/nc' -
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 -
Thank you! Deploying Applications with Capistrano! ! Andrew Turner, @galenandrew!
March 11, 2014 - Dallas PHP