Skip to content

ndench/symfony-capistrano-circleci-example

Repository files navigation

How to set up continuos deployment with Capistrano and CircleCI

The following instructions are complied from:

  1. install ruby
  2. install bundler
$ gem install bundler
  1. create Gemfile
# Gemfile
source 'https://rubygems.org' do
    gem 'capistrano',  '~> 3.9'
    gem 'capistrano-symfony', '~> 1.0.0.rc2'
end
  1. install capistrano
$ bundle install
  1. capify your project Set STAGES to a comma separated list of all your environments. This will create your deployment config files.
$ bundle exec cap install STAGES=prod,test,...
  1. add the capistrano-symfony plugin to you Capfile
# Capfile
require 'capistrano/symfony'
  1. update project deployment settings in config/deploy.rb
# config/deploy.rb

# Set your application name
set :application, "circlestrano"

# Set your repo url, capistrano will clone it for every deployment
set :repo_url, "git@github.com:ndench/symfony-capistrano-circleci-example.git"

# Set the location to deploy to on the remote server
set :deploy_to, "/srv/www/grishue"

# Create your own variables
set :sessions_path, fetch(:var_path) + "/sessions"

# Add parameters.yml to the linked files to keep it between deployments
append :linked_files, "app/config/parameters.yml"

# Link the sessions dir across deployments so it doesn't log users out
append :linked_dirs, fetch(:sessions_path)

# I like to use composer installed as a .phar in the project root
# If you like global composer, ignore this
SSHKit.config.command_map[:composer] = "php composer.phar"

# Use acl to set permission
set :permission_method, :acl

# Allow the web user to access the cache, log path, session, and anything else in var
set :file_permissions_users, ["www-data"]
set :file_permissions_paths, ["var", fetch(:cache_path), fetch(:log_path), fetch(:sessions_path)]
  1. update environment specific settings in config/deploy/*.rb
# config/deploy/prod.rb

# Configure the server to deploy to and user to deploy as
server "circlestrano.tk", user: "deploy"

# If you want a different branch deployed to each environment 
#set :branch prod
  1. copy parameters.yml to your server Make sure you copy up the correct parameters file, I'm just using the development one. This will be symlinked into app/config/parameters.yml during the deploy. Make sure you change the owner to the deploy user.
$ scp app/config/parameters.yml root@circlestrano.tk:/srv/www/circlestrano/shared/app/config/parameters.yml
$ ssh root@circlestrano.tk 'chown deploy:deploy /srv/www/circlestrano/shared/app/config/parameters.yml'
  1. deploy!
$ cap prod deploy
  1. set up circleci

    • log into circleci with your GitHub account.
    • go to the projects page and create a new project on your repo
  2. circlelify your project Create circle.yml with your circleci configuration. Now when you push to master it will automatically build and run tests!

# circle.yml
machine:
  php:
    version: 7.1.3

test:
  override:
    - vendor/bin/phpunit --coverage-text=coverage.txt
  1. create a webhook Create an (incoming-webhook)[https://api.slack.com/incoming-webhooks] in slack, and save the webhook url.

Put that webhook into a scripts/notify.sh script. This script will be run after the auto deployment, so you get notified of it's success.

#!/usr/bin/env bash

HERE='<!here>'
COMMITMSG=$(git log --format=%B -n 1 $CIRCLE_SHA1)
COVERAGE=""

if [ -f "build/coverage.txt" ]; then
  COVERAGE=$(grep Summary --after-context 3 grishue/coverage.txt | grep Lines | tr --squeeze-repeats ' ' | cut -d\  -f3)
  COVERAGE="\nTest Coverage: $COVERAGE"
fi

curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"$HERE Deployed branch \`$CIRCLE_BRANCH\` of \`$CIRCLE_PROJECT_REPONAME\` to update it to\n \`\`\`$COMMITMSG\`\`\` :tada: :rocket: $COVERAGE\"}" https://hooks.slack.com/services/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
  1. continuous deployment Configure CircleCi for auto deployment.
# circle.yml
deployment:
  master:
    branch: master
    commands:
      - bundle install
      - bundle exec cap prod deploy REVISION=$CIRCLE_SHA1
      - scripts/notify.sh
  1. add ssh key to circleci project

    • generate an ssh key: ssh-keygen
    • go to projects -> settings -> ssh permissions
    • click 'add ssh key'
    • paste in the contents of the private key you generated
    • on your server, add the contents of the public key to ~/.ssh/authorized_keys for the deploy user
  2. enjoy your continuous deployments!

About

An example for how to set up continuous deployment for a Symfony app from CircleCI using Capistrano.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published