Slide 1

Slide 1 text

Deploying Applications with Capistrano! ! Andrew Turner, @galenandrew! March 11, 2014 - Dallas PHP

Slide 2

Slide 2 text

About Capistrano

Slide 3

Slide 3 text

– CapistranoRB.com “A remote server automation and deployment tool written in Ruby.” automation deployment

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Know your cap --version

Slide 9

Slide 9 text

Using Capistrano

Slide 10

Slide 10 text

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)

Slide 11

Slide 11 text

Installation / Setup $ gem install capistrano Install Capistrano gem $ cap install Initialize Capistrano in your app/code

Slide 12

Slide 12 text

Directories/Files: Local ├── Capfile ├── config │ ├── deploy │ │ ├── production.rb │ │ └── staging.rb │ └── deploy.rb └── lib └── capistrano └── tasks http://capistranorb.com/documentation/getting-started/preparing-your-application/

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

DEPLOY! $ cap production deploy:check Check deploy and set up server $ cap production deploy Deploy!

Slide 18

Slide 18 text

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()

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

ROLLBACK! $ cap production deploy:rollback Rollback

Slide 21

Slide 21 text

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()

Slide 22

Slide 22 text

Additional Info

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Tips / Tricks $ gem install require capistrano-nc Integrate with Mac OS X Notifications! https://github.com/capistrano/notification-center require 'capistrano-nc/nc'

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Thank you! Deploying Applications with Capistrano! ! Andrew Turner, @galenandrew! March 11, 2014 - Dallas PHP