Capistrano – PHP, deploy without stressful
Capistrano – PHP
Today, when everybody applies “Continuous Delivery” programmers must make changes frequently and delivery process can be stressful. Programmers try to make delivery process maximal simple and painless. Today have they already many tools to delivery. One of them is written in Ruby “Capistrano”. And team Capistrano – PHP work great!
Capistrano based on git and SSH. Nothing stands in the way of a use Capistrano to work with PHP projects.
If you need back to last revision, then you can make this effortless. Default on the server Capistrano storage 5 last versions. Actual version can you find in “current” folder.
Easy change the last version is not the only advantage of Capistrano. We can write special scripts (called Tasks) and run some scripts after (e.g. composer) or before (e.g. test) delivery. For this must we use Hooks.
What we need to work with Capistrano
Development environment – git, Capistrano.
Staging environment – git (We install Capistrano only on Development environment)
Installation
If you don’t develop in Ruby, then you’ll install first ruby and gem. Next, can you install Capistrano:
gem install capistrano
Are you ready? Now can you use Capistrano in your project:
cap install
Configure
To configure Capistrano need we 3 files:
config/deploy.rb -> settings for all environments
config/deploy/production.rb -> settings only for production environment (imports settings from config/deploy.rb)
config/deploy/staging.rb -> only for staging environment (imports settings from config/deploy.rb)
deploy.rb
In config/deploy.rb file must we set:
Project name:
set :application, "project name"
Git repository (np. Github or Bitbucket):
set :repo_url, "git@url:user/reponame.git"
Make sure that all servers have a generated SSH keys and have access to the git repository.
The path to deploy on staging and production servers:
set :deploy_to, "/path/to/your/project/catalog/"
staging.rb and production.rb
Next in config/deploy/staging.rb and config/deploy/production.rb files set SSH settings:
role :web, %w{username@111.222.333.444}
First deploy
Everything is ready. You can send changes to staging server:
cap staging deploy
Project in the latest version should be here:
/path/to/your/project/catalog/current/
If you want to send the changes on a production server, then you’ll use:
cap production deploy
Rollback
If you need back the changes, then you’ll use:
cap production/staging deploy:rollback
In the future will I write how to use Hooks for better automation.
No Comment