我花了很多时间调试这个,我似乎没有接近答案 . 我还考虑在capistrano存储库中创建一个问题,但我并不认为这是capistrano本身的问题 .
上下文:
我在Gitlab EE中托管的私有存储库中有一个rails 5项目 . 我的repo完全配置为使用Gitlab CI,使用capistrano,capistrano / rails和capistrano / rvm自动部署 . 我有一个部署工作,我执行 cap review deploy
(正在审查我要部署的环境)
一切都运行良好,直到我到达capistrano挂钩 deploy:migrate
,其中它打破了以下错误 .
错误:
(Backtrace restricted to imported tasks)
cap aborted!
NoMethodError: undefined method `already_invoked' for <Rake::Task deploy:migrating => [set_rails_env]>:Rake::Task
Tasks: TOP => deploy:migrate
(See full trace by running task with --trace)
The deploy has failed with an error: undefined method `already_invoked' for <Rake::Task deploy:migrating => [set_rails_env]>:Rake::Task
从我能够掌握的,似乎是rake任务设置rails_env(来自gem capistrano / rails)的问题 . 我已经跟踪了看是否capistrano / rvm没有从rvm设置正确的ruby,但事实并非如此 . cappistrano / rvm设置还可以 . 我想也许它与宝石版本或其他东西有关 .
我的弱补丁:
我没有添加任何新的迁移,使用capistrano / rails中的人员提供的可选参数修补它 .
set :conditionally_migrate, true
但它仍然失败(当有新的迁移时,大部分时间都是这样)
另外,从我的本地环境(使用 bundle exec cap review deploy
)使用它时,capistrano可以正常工作 .
提前致谢!希望你能帮我找到答案......
编辑:添加有关我的文件和堆栈的一些细节:
在我的Rails配置中,我使用git-ignored environment_variables.yml
文件,在其中设置capistrano配置文件中显示的所有 ENV['var']
配置 . 我还保证在正在执行部署作业的运行程序中正确设置了这些 . (使用Gitlab Variables) .
这些CI作业使用安装在docker runner内的DigitalOcean droplet上的gitlab-runner服务,每个作业指定的图像 .
.gitlab-ci.yml
stages:
- test
- deploy
variables:
MYSQL_DATABASE: "test_linting"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
DB_NAME: 'test_linting'
DB_USER: 'root'
DB_PASS: ''
DB_HOST: 'mysql'
services:
- mysql
testing:
image: heroku/ruby
stage: test
cache:
paths:
- vendor/cache
script:
- bundle install --without=development production --jobs $(nproc) --path=vendor/cache
- bundle exec rails db:create RAILS_ENV=test
- bundle exec rails db:migrate RAILS_ENV=test
- bundle exec rails test
artifacts:
paths:
- coverage/
dev-deploy:
image: ruby:2.3
stage: deploy
environment:
name: $CI_BUILD_REF_NAME
url: http://dev.linting.com
before_script:
- gem install capistrano -v '~> 3.7'
- gem install capistrano-rails
- gem install capistrano-rvm
- gem install slackistrano
script:
- cap $CI_BUILD_REF_NAME deploy
only:
- development
deploy:
image: ruby:2.3
stage: deploy
environment:
name: $CI_BUILD_REF_NAME
url: http://$CI_BUILD_REF_NAME.linting.com
before_script:
- gem install capistrano -v '~> 3.7'
- gem install capistrano-rails
- gem install capistrano-rvm
- gem install slackistrano
script:
- cap $CI_BUILD_REF_NAME deploy
only:
- qa
- staging
review_deploy:
image: ruby:2.3
stage: deploy
environment:
name: review
url: http://rev.linting.com
when: manual
before_script:
- gem install capistrano -v '~> 3.7'
- gem install capistrano-rails
- gem install capistrano-rvm
- gem install slackistrano
script:
- cap review deploy
except:
- development
- qa
- staging
- master
config / deploy.rb
lock '~> 3.7'
env_file = "./config/environment_variables.yml"
if File.exists?(env_file)
YAML.load_file(env_file)['capistrano'].each do |key, value|
ENV[key.to_s] = value
end
end
set :application, ENV['PROJECT_NAME']
set :repo_url, ENV['REPO_URL']
set :rvm_type, :user
set :rvm_ruby_version, '2.3.1'
set :conditionally_migrate, true
set :linked_files, %w{config/environment_variables.yml}
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :slackistrano, {
klass: Slackistrano::CustomMessaging,
channel: ENV['SLACK_CHANNEL'],
webhook: ENV['SLACK_HOOK']
}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
end
end
desc 'Reset environment (rake db:reset)'
task :db_reset do
on roles(:app) do
within "#{current_path}" do
with rails_env: "#{fetch(:rails_env)}" do
execute :rake, "db:migrate:reset"
execute :rake, "db:seed"
end
end
end
end
after :published, 'deploy:restart'
after :finishing, 'deploy:cleanup'
end
config / deploy / review.rb
# Server definiton: (define ip in local env, and pass in gitlab)
server ENV['DEV_DROPLET_IP'], user: 'deploy', roles: %w{web app db}, password: ENV["SSH_DEPLOY_PASS"]
# ONLY WORKS IF IT WAS RUN BY THE GITLAB CI RUNNER
set :branch, ENV['CI_BUILD_REF_NAME'] ? ENV['CI_BUILD_REF_NAME'] : 'development'
# Capistrano Variables
set :stage, 'review'
set :rails_env, 'review'
# Variables for rev
set :commit, ENV['CI_BUILD_REF'] ? ENV['CI_BUILD_REF'][0..7] : 'local'
set :user, ENV['GITLAB_USER_EMAIL'] ? ENV['GITLAB_USER_EMAIL']: 'local user'
# Capistrano Deployment Route
set :deploy_to, "/home/deploy/rev.#{ENV['PROJECT_NAME']}"
namespace :deploy do
desc 'Set review branch in the review server'
task :set_review_branch do
on roles(:app) do
within "#{current_path}" do
execute "echo '#{fetch(:branch)}@#{fetch(:commit)}' >> #{release_path.join('tmp/rev_branch')}"
execute "echo '#{fetch(:user)}' >> #{release_path.join('tmp/rev_user')}"
end
end
end
after :publishing, 'deploy:set_review_branch'
before :finishing, 'deploy:db_reset'
end
Capfile
require "capistrano/setup"
require "capistrano/deploy"
require 'capistrano/rvm'
require 'capistrano/rails'
require 'slackistrano/capistrano'
require_relative 'lib/custom_messaging'
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
gem 'puma', '~> 3.0'
gem 'sass-rails', '~> 5.0'
gem 'bootstrap-sass'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'mysql2'
gem 'haml-rails'
gem 'devise'
gem 'paperclip', git: 'git://github.com/thoughtbot/paperclip.git'
gem 'administrate', '~> 0.3.0'
gem 'bourbon'
gem 'rails_real_favicon', '~> 0.0.6'
gem 'listen', '~> 3.0.5'
group :development, :test do
gem 'byebug', platform: :mri
gem 'minitest-rails'
gem 'minitest-reporters'
gem 'simplecov'
gem 'simplecov-json', require: false
gem 'factory_girl_rails'
gem 'faker'
end
group :development do
gem 'annotate'
gem 'better_errors'
gem 'binding_of_caller'
# Capistrano for Deployments
gem 'capistrano', '~> 3.7'
gem 'capistrano-rvm'
gem 'capistrano-rails'
gem 'slackistrano'
gem 'web-console'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
1 回答
我的猜测是,因为您手动在CI环境中安装Capistrano Gems,所以版本不匹配 . 如果在CI服务器上运行
bundle install
和bundle exec
,或者固定已安装的版本,它可能会开始工作 .我用于此的一个解决方案是将所有必需的部署gem添加到我的Gemfile中的
:deployment
组 . 然后你需要将以下内容添加到deploy.rb
:然后将您的脚本更改为: