Search code examples
rubyruby-on-rails-3nginxcapistranounicorn

Capistrano 3: After deployment is not "refreshed" code (website stays as prior the deploy)


I was doing some modification on the website (pure HTML+CSS), deployed it on the server and after refreshing the browser the content was the same.

So I logged in on the server, killed unicorn, started it manually and the new content finally appeared.

How do I do this automatically?

Currently, I have this deploy.rb setup:

# config valid only for current version of Capistrano
lock "3.8.1"

set :application, "project"
set :repo_url, "[email protected]:username/project.git"
set :branch, "master"
set :tmp_dir, '/home/deployer/tmp'

set :deploy_to, "/home/deployer/apps/project"
set :keep_releases, 5

set(:executable_config_files, %w(
  unicorn_init.sh
))

# files which need to be symlinked to other parts of the
# filesystem. For example nginx virtualhosts, log rotation
# init scripts etc.
set(:symlinks, [
  {
    source: "nginx.conf",
    link: "/etc/nginx/sites-enabled/default"
  },
  {
    source: "unicorn_init.sh",
    link: "/etc/init.d/unicorn_#{fetch(:application)}"
  },
  {
    source: "log_rotation",
   link: "/etc/logrotate.d/#{fetch(:application)}"
  },
  {
    source: "monit",
    link: "/etc/monit/conf.d/#{fetch(:application)}.conf"
  }
])


namespace :deploy do   
  desc 'Restart application'
  task :restart do
    task :restart do
      invoke 'unicorn:reload'
    end
  end
  after :publishing, :restart    

  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:web) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end
  before "deploy", "deploy:check_revision"
end

What do I need to add yet in order to don't need to manually restart server?

Thank you


Solution

  • You can create a task that does this restarting step for you and call it after the deployment process. Maybe it can run a shell script with the required commands to restart Unicorn. Put the commands you use in the script and call it via the Capistrano task. Something like this:

    desc 'Restarts the application calling the appropriate Unicorn shell script.'
    task :restart_unicorn do
      on roles(:app) do
        execute '/etc/init.d/restart_unicorn.sh'
      end
    end
    
    after 'deploy:published', 'restart_unicorn'
    

    More details here. Don't forget to modify the shell file permissions to allow execution. The task code can be in your deploy.rb file, but I recommend moving it to a specific Capistrano tasks file to keep your code organized. Hope this helps!

    PS.: Take a look at the Capistrano flow too. Actually, you can create tasks to run before or after any part of the process.