【问题标题】:Rails 4 app deployment using mina on webfaction在 webfaction 上使用 mina 部署 Rails 4 应用程序
【发布时间】:2014-12-12 18:06:08
【问题描述】:

我有一个我想在 webfaction 上部署的 rails 4 应用程序。该应用程序驻留在 bitbucket 上。此外,根据 webfaction 文档,该应用程序必须部署在“/home/username/webapps/app”路径中。我的所有 database.yml 设置(适用于所有环境)都在 secrets.yml 中,已复制到 shared_pa​​th/config。

根据http://docs.webfaction.com/software/rails.html 指定的文档,必须设置一些环境变量。 Webfaction 需要您设置 ssh 会话结束后销毁的 env 变量。我已经尝试在“set_env_variables”任务中设置它们。

目录结构如下:

$ pwd
/home/username/webapps/appname
$ ls
bin  gems  hello_world  lib  nginx  releases  scm  shared  src  tmp
$ls gems/
bin  build_info  cache  doc  extensions  gems  specifications
$ ls gems/gems/
actionmailer-4.1.8         bundler-1.7.9               jbuilder-2.2.5      rack-test-0.6.2        sass-rails-4.0.5       tilt-1.4.1 ...
$ cd hello_world/
[hello_world]$ bundle
-bash: bundle: command not found
$ cd ..
$ pwd
/home/username/webapps/appname
$ export PATH=$PWD/bin:$PATH
$ export GEM_HOME=$PWD/gems
$ export RUBYLIB=$PWD/lib
$ cd hello_world/
$ bundle
Using rake 10.4.2
...
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

它指定我们需要设置环境变量。

这里的 'hello_world' 是 webfaction 提供的示例 rails 应用程序。我会删除它。也提供了 nginx-passenger。 我的部署脚本是

require 'mina/bundler'
require 'mina/rails'
require 'mina/git'

set :domain, 'webabc.webfaction.com'
set :deploy_to, '/home/username/webapps/appname'
set :repository, 'git@bitbucket.org:username/appname.git'
set :branch, 'master'

set :shared_paths, ['config/secrets.yml', 'log', 'tmp']
set :user, 'ssh-username'    # Username in the server to SSH to.

task :environment do
end

task :setup => :environment do
  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/tmp"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]

  queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"]
  queue  %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/secrets.yml'."]
end

desc "Deploys the current version to the server."
task :deploy => :environment do
  deploy do
    invoke :'set_env_variables'
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'

    to :launch do
      queue "mkdir -p #{deploy_to}/#{current_path}/tmp/"
      queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt"
      invoke :restart
    end
  end
end

desc "Set the environment variables."
task :set_env_variables => :environment do
  queue! "cd /home/username/webapps/appname"
  queue! "export GEM_HOME=$PWD/gems"
  queue! "export RUBYLIB=$PWD/lib"
  queue! "export PATH=$PWD/bin:$PATH"
  queue! "export LD_LIBRARY_PATH=$HOME/lib/"
end

当我执行“mina deploy --verbose --trace”时,我得到了

** Invoke deploy (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute deploy
** Invoke set_env_variables (first_time)
** Invoke environment 
** Execute set_env_variables
** Invoke git:clone (first_time)
** Execute git:clone
** Invoke deploy:link_shared_paths (first_time)
** Execute deploy:link_shared_paths
** Invoke bundle:install (first_time)
** Execute bundle:install
** Invoke rails:db_migrate (first_time)
** Execute rails:db_migrate
** Invoke rails:assets_precompile (first_time)
** Execute rails:assets_precompile
** Invoke deploy:cleanup (first_time)
** Execute deploy:cleanup
** Invoke restart (first_time)
** Invoke environment 
** Execute restart

-----> Creating a temporary build path
   $ touch "deploy.lock"
   $ mkdir -p "$build_path"
   $ cd "$build_path"

   Setting environment variables
   $ cd /home/username/webapps/appname
   $ export GEM_HOME=$PWD/gems
   $ export RUBYLIB=$PWD/lib
   $ export PATH=$PWD/bin:$PATH
   $ export LD_LIBRARY_PATH=$HOME/lib/

-----> Fetching new git commits
   $ (cd "/home/username/webapps/appname/scm" && git fetch "git@bitbucket.org:username/appname.git" "master:master" --force)

-----> Using git branch 'master'
   $ git clone "/home/username/webapps/appname/scm" . --recursive --br       Cloning into '.'...
   $ git clone "/home/username/webapps/appname/scm" . --recursive --branch "master"
   done.

-----> Using this git commit

   $ git --no-pager log --format='%aN (%h):%n> %s' -n 1
   Prasad Surase (ce1654d):
   > Some git commit message
   $ rm -rf .git

-----> Symlinking shared paths
   $ mkdir -p "./config"
   bash: line 130: bundle: command not found
   $ mkdir -p "."
   $ rm -rf "./config/secrets.yml"
   $ ln -s "/home/username/webapps/appname/shared/config/secrets.yml" "./config/secrets.yml"
   $ rm -rf "./log"
   $ ln -s "/home/username/webapps/appname/shared/log" "./log"
   $ rm -rf "./tmp"
   $ ln -s "/home/username/webapps/appname/shared/tmp" "./tmp"

-----> Installing gem dependencies using Bundler
   $ mkdir -p "/home/username/webapps/appname/shared/bundle"
   $ mkdir -p "./vendor"
   $ ln -s "/home/username/webapps/appname/shared/bundle" "./vendor/bundle"
   $ bundle install --without development:test --path "./vendor/bundle" --deployment
!     ERROR: Deploy failed.

-----> Cleaning up build
   $ rm -rf "$build_path"
   Unlinking current
   $ rm -f "deploy.lock"
   OK

!     Command failed.
   Failed with status 19

https://community.webfaction.com/questions/5186/capistrano-cant-find-bundle 链接指定 capistrano 部署脚本的解决方案。我该如何为 mina 修复它?

【问题讨论】:

    标签: ruby-on-rails-4 deployment webfaction


    【解决方案1】:

    不要使用:set_env_variables 任务,而是尝试使用您的 ENV 参数添加 :bundle_bin 变量:

    set :bundle_bin, %{PATH="#{deploy_to}/bin:$PATH" GEM_HOME="#{deploy_to}/gems" RUBYLIB="#{deploy_to}/lib" RAILS_ENV=#{env} #{deploy_to}/bin/bundle}
    

    这样,mina 每次使用bundle 命令时都会使用正确的变量。我认为将其添加为任务并仅导出变量会使 mina 忘记它们。订单也可能是个问题。


    这是我的完整deploy.rb供参考:

    require 'mina/bundler'
    require 'mina/rails'
    require 'mina/git'
    # require 'mina/rbenv'  # for rbenv support. (http://rbenv.org)
    # require 'mina/puma'
    
    # Basic settings:
    set :domain, 'USERNAME.webfactional.com'
    set :repository, 'git@bitbucket.org:GIT_USERNAME/REPO.git'
    set :branch, 'master'
    
    case ENV['to']
    when 'production'
      set :deploy_to, '/home/USERNAME/webapps/PRODUCTION_APP'
      set :env, 'production'
    else
      set :deploy_to, '/home/USERNAME/webapps/STAGING_APP'
      set :env, 'staging'
    end
    
    # Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
    # They will be linked in the 'deploy:link_shared_paths' step.
    set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log', 'public/uploads']
    
    # Optional settings:
    set :user, 'USERNAME'    # Username in the server to SSH to.
    # set :port, '7331'     # SSH port number.
    
    set :bundle_bin, %{PATH="#{deploy_to}/bin:$PATH" GEM_HOME="#{deploy_to}/gems" RUBYLIB="#{deploy_to}/lib" RAILS_ENV=#{env} #{deploy_to}/bin/bundle}
    
    # This task is the environment that is loaded for most commands, such as
    # `mina deploy` or `mina rake`.
    task :environment do
      # If you're using rbenv, use this to load the rbenv environment.
      # Be sure to commit your .rbenv-version to your repository.
      # invoke :'rbenv:load'
    end
    
    # Put any custom mkdir's in here for when `mina setup` is ran.
    # For Rails apps, we'll make some of the shared paths that are shared between
    # all releases.
    task :setup => :environment do
      queue! %[mkdir -p "#{deploy_to}/shared/log"]
      queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]
    
      queue! %[mkdir -p "#{deploy_to}/shared/config"]
      queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]
    
      queue! %[touch "#{deploy_to}/shared/config/database.yml"]
      queue  %[echo "-----> Be sure to edit 'shared/config/database.yml'."]
    
      queue! %[touch "#{deploy_to}/shared/config/secrets.yml"]
      queue  %[echo "-----> Be sure to edit 'shared/config/secrets.yml'."]
    
      queue! %[mkdir -p "#{deploy_to}/shared/public/uploads"]
      queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/public/uploads"]
    end
    
    desc "Deploys the current version to the server."
    task :deploy => :environment do
      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'
        invoke :'bundle:install'
        invoke :'rails:db_migrate'
        invoke :'rails:assets_precompile'
    
        to :launch do
          queue %{#{deploy_to}/bin/restart}
        end
      end
    end
    
    namespace :nginx  do
      task :start => :environment do
        queue %{#{deploy_to}/bin/start}
      end
    
      task :stop => :environment do
        queue %{#{deploy_to}/bin/stop}
      end
    
      task :restart => :environment do
        queue %{#{deploy_to}/bin/restart}
      end
    end
    
    task :logs do
      queue 'echo "[ TAIL CONTENTS OF LOG FILE ]"'
      queue %{tail -f #{deploy_to}/current/log/#{env}.log}
    end
    
    task :console => :environment do
      queue 'echo "[ STARTING EXTERNAL RAILS CONSOLE ]"'
      queue %{#{bundle_bin} exec rails c}
    end
    

    【讨论】:

    • 谢谢。我从 webfaction 转到 digitalocean,因为我需要在几天内启动并运行该应用程序。使用 mina 部署,它运行良好。
    • @Yoeran 与您的配置一切正常,只有服务器继续指向默认 Webfaction 安装的问题(例如:/home/myusername/webapps/myapp/defaultwebfactioninstallationfolder)我已经更改nginx 配置指向 /home/myusername/webapps/myapp/current。你觉得我错过了什么?
    • @albertopriore 您应该将/public 附加到您的 nginx 服务器根目录。因此,您的完整服务器根目录变为:/home/USERNAME/webapps/APPNAME/current/public
    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多