rubyforgood/babywearing

View on GitHub
deploy/deploy.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

require 'mina/rails'
require 'mina/git'
require 'mina/rbenv' # for rbenv support. (https://rbenv.org)
require 'mina/puma'

# Basic settings:
#   domain       - The hostname to SSH to.
#   deploy_to    - Path to deploy into.
#   repository   - Git repo to clone from. (needed by mina/git)
#   branch       - Branch name to deploy. (needed by mina/git)

set :local_rails_root, '.'
set :application_name, 'babywearing'

set :server_name, ENV['server_name']
ensure!(:server_name)

set :domain, lambda {
  s = fetch(:server_name)
  s == 'prod' ? 'babywearing.exchange' : "#{s}.babywearing.exchange"
}

set :deploy_to, '/home/babywearing/app'
set :repository, 'https://github.com/rubyforgood/babywearing.git'
set :branch, ENV['branch'] || ENV['TRAVIS_BRANCH'] || 'master'
set :force_migrate, true

set :user, 'babywearing' # Username in the server to SSH to.
set :identity_file, ENV['DEPLOY_IDENTITY_FILE'] || "~/.ssh/babywearing-#{fetch(:server_name)}"

#   set :port, '30000'           # SSH port number.
#   set :forward_agent, true     # SSH forward_agent.

# Shared dirs and files will be symlinked into the app-folder by the 'deploy:link_shared_paths' step.
# Some plugins already add folders to shared_dirs like `mina/rails` add `public/assets`, `vendor/bundle` and many more
# run `mina -d` to see all folders and files already included in `shared_dirs` and `shared_files`
# set :shared_dirs, fetch(:shared_dirs, []).push('public/assets')
# set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml')

set :shared_dirs, fetch(:shared_dirs, []).push('log', 'tmp/pids', 'tmp/sockets')
set :shared_files, fetch(:shared_files, []).push('config/master.key')

# This task is the environment that is loaded for all remote run commands, such as
# `mina deploy` or `mina rake`.
task :remote_environment do
  # If you're using rbenv, use this to load the rbenv environment.
  # Be sure to commit your .ruby-version or .rbenv-version to your repository.
  invoke :'rbenv:load'
end

# Put any custom commands you need to run at setup``
# All paths in `shared_dirs` and `shared_paths` will be created on their own.
task :setup do
end

desc 'Deploys the current version to the server.'
task :deploy do
  # uncomment this line to make sure you pushed your local branch to the remote origin
  # invoke :'git:ensure_pushed'
  run(:local) do
    command "scp -i #{fetch(:identity_file)} #{fetch(:local_rails_root)}/config/master.key "\
                      "#{fetch(:user)}@#{fetch(:domain)}:/home/babywearing"
  end

  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    command "mv /home/babywearing/master.key #{fetch(:shared_path)}/config/master.key"
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'

    on :launch do
      command 'sudo service sidekiq restart'
      command 'sudo service puma restart'
    end
    # on :launch do
    #   in_path(fetch(:current_path)) do
    #     command %{mkdir -p tmp/}
    #     command %{touch tmp/restart.txt}
    #   end
    # end
  end
  # you can use `run :local` to run tasks on local machine before of after the deploy scripts
  # run(:local){ say 'done' }
end

desc 'Seed data to the database'
task seed: :remote_environment do
  command 'cd /home/babywearing/app/current'
  command 'bundle exec rails db:seed RAILS_ENV=production'
end
# For help in making your deploy script, see the Mina documentation:
#
#  - https://github.com/mina-deploy/mina/tree/master/docs