Rails 4 + Capistrano 3 - 从本地存储库部署到生产服务器

问题描述 投票:0回答:3

我正在使用 Windows 10 和 Cygwin,并且正在设置 Capistrano 3 以部署到我的生产环境。我相信我已正确设置所有内容,但我不知道如何将本地存储库推送到生产服务器。我在 GitHub 或其他此类网站上没有我的存储库,并且希望将其保留在本地。当我运行

cap production deploy
时,我收到以下错误消息。

SSHKit::Runner::ExecuteError:执行时出现异常 [电子邮件受保护]:git 退出状态:128 git stdout: 没有写任何内容 git stderr: fatal: 没有远程配置来列出引用 来自。

SSHKit::命令::失败:git 退出状态:128 git stdout:无 写入 git stderr: fatal: 没有远程配置来列出来自的引用。

任务:TOP => git:check (通过使用 --trace 运行任务查看完整跟踪) 部署失败并出现错误:执行时出现异常 [电子邮件受保护]:git 退出状态:128 git stdout: 没有写任何内容 git stderr: fatal: 没有远程配置来列出引用 来自。

这是我的

config\deploy.rb
文件:

# config valid only for current version of Capistrano
lock '3.4.0'

set :application, 'myappname
set :deploy_user, 'myappuser'
set :repo_url, "file:///C:/Users/me/Documents/repo/myappname.git"

# setup rvm.
set :rbenv_type, :user
set :rbenv_ruby, '2.1.5-p273'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}

set :assets_roles, [:app]

# Default value for :scm is :git
set :scm, :git

# Default value for :linked_files is []
set :linked_files, %w{config/database.yml config/application.yml}

# Default value for linked_dirs is []
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

# Default value for keep_releases is 5
set :keep_releases, 5

# which config files should be copied by deploy:setup_config
# see documentation in lib/capistrano/tasks/setup_config.cap
# for details of operations
set(:config_files, %w(
  nginx.conf
  application.yml
  database.yml
  unicorn.rb
  unicorn_init.sh
))

# which config files should be made executable after copying
# by deploy:setup_config
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. The full_app_name variable isn't
# available at this point so we use a custom template {{}}
# tag and then add it at run time.
set(:symlinks, [
  {
    source: "nginx.conf",
    link: "/etc/nginx/sites-enabled/{{full_app_name}}"
  },
  {
    source: "unicorn_init.sh",
    link: "/etc/init.d/unicorn_{{full_app_name}}"
  },
  {
    source: "log_rotation",
    link: "/etc/logrotate.d/{{full_app_name}}"
  }
])

namespace :deploy do
  # compile assets locally then rsync
  after :finishing, 'deploy:cleanup'
end

我找到的所有答案都没有解决方案或适用于 Capistrano 2,所以我想知道选项是否已更改。任何帮助将不胜感激!

编辑

我已经取得了一些进展,但它仍然没有读取我的本地存储库。这是错误消息:

SSHKit::命令::失败:git 退出状态:128 git stdout:无 写入 git stderr: fatal: '/C:/Users/me/Documents/repo/myappname.git' 似乎不是 git 存储库 致命:无法读取 远程存储库。

请确保您拥有正确的访问权限和存储库 存在。

我已经为我的仓库尝试了所有不同的路径组合。我的 git 存储库位于以下路径:

C:/Users/me/Documents/repo/myappname
。有没有具体的方法来引用我的本地存储库?

ruby-on-rails git capistrano3
3个回答
2
投票

不幸的是,这不是 Capistrano 的设计工作原理。 Capistrano 做出的基本假设是,您的项目驻留在服务器可以访问的存储库中(几乎总是托管在 Bitbucket 或 GitHub 上的 Git 存储库)。

Capistrano 通过在服务器上运行 git 命令来拉取您的源代码。服务器无法查看您的本地存储库,这就是它不起作用的原因。

有一些插件尝试更改 Capistrano 的行为,以允许您正在寻找的

push 风格部署。在 GitHub 上搜索“capistrano copy”并尝试一下。这可能就是您正在寻找的:

https://github.com/ydkn/capistrano-git-copy


1
投票
我一直在使用 capistrano-scm-copy 通过 SCP 从我的开发盒上传到 AWS 来进行部署。 请注意——这就像拿着剪刀奔跑。 很高兴能够在生产中进行快速修补程序部署并进行 git 提交。 但你需要非常小心,不要忘记提交部分。


0
投票
厌倦了 Capistrano,我决定继续实现一个基本的 Rake 任务,该任务完成我直接从本地部署小型应用程序所需的基本基本操作。这是一个草稿,但到目前为止似乎有效。

# Usage bundle exec rake "deploy:production[/path/to/ssh_key]" # Assuming you're bundling gems 'net-ssh' + 'net-scp' namespace :deploy do SERVER = '<server_url_or_ip' USER = '<user>' DEPLOY_TO = '/deploy/to/folder' SHARED_PATH = "#{DEPLOY_TO}/shared" CURRENT_PATH = "#{DEPLOY_TO}/current" RBENV_PATH = '<path_to_rbenv>' # ANSI color codes GREEN = "\e[32m" YELLOW = "\e[33m" BLUE = "\e[34m" RESET = "\e[0m" def log(message, color = BLUE) puts "#{color}#{message}#{RESET}" end def log_task(task_name) puts "\n#{YELLOW}#{'-' * 50}#{RESET}" log "Starting: #{task_name}", YELLOW end desc "Deploy the application to production (use quotes around argument in Zsh: \"deploy:production[/path/to/key]\")" task :production, [:ssh_key] => [:confirm] do |t, args| ssh_key = args[:ssh_key] || raise("Please provide the SSH key path: rake \"deploy:production[/path/to/ssh_key]\"") commit_hash = `git rev-parse --short HEAD`.strip release_name = "release-#{Time.now.strftime('%Y%m%d%H%M%S')}-revision-#{commit_hash}" log "Starting deployment of #{release_name}", GREEN Rake::Task["deploy:package"].invoke(release_name) Rake::Task["deploy:upload"].invoke(ssh_key, release_name) Rake::Task["deploy:extract"].invoke(ssh_key, release_name) Rake::Task["deploy:create_dirs"].invoke(ssh_key) Rake::Task["deploy:symlink"].invoke(ssh_key, release_name) ruby_version = get_ruby_version(ssh_key, release_name) Rake::Task["deploy:bundle_install"].invoke(ssh_key, release_name, ruby_version) Rake::Task["deploy:db_migrate"].invoke(ssh_key, ruby_version) Rake::Task["deploy:assets_precompile"].invoke(ssh_key, ruby_version) Rake::Task["deploy:restart"].invoke(ssh_key) Rake::Task["deploy:cleanup_remote"].invoke(ssh_key) Rake::Task["deploy:cleanup_local"].invoke(release_name) log "Deployment of #{release_name} completed successfully", GREEN end task :confirm do print "Are you sure you want to deploy to production? (y/n): " exit unless STDIN.gets.chomp.downcase == 'y' end task :package, [:release_name] do |t, args| log_task "Packaging application" sh "git archive --format=tar --output=./#{args[:release_name]}.tar HEAD" end task :upload, [:ssh_key, :release_name] do |t, args| log_task "Uploading package to server" Net::SCP.start(SERVER, USER, keys: [args[:ssh_key]]) do |scp| scp.upload!("./#{args[:release_name]}.tar", "#{DEPLOY_TO}/releases/#{args[:release_name]}.tar") end end task :extract, [:ssh_key, :release_name] do |t, args| log_task "Extracting package on server" release_path = "#{DEPLOY_TO}/releases/#{args[:release_name]}" ssh_exec("mkdir -p #{release_path} && tar -xf #{DEPLOY_TO}/releases/#{args[:release_name]}.tar -C #{release_path}", args[:ssh_key]) end task :create_dirs, [:ssh_key] do |t, args| log_task "Creating necessary directories" ssh_exec("mkdir -p #{SHARED_PATH}/tmp/{sockets,pids,log}", args[:ssh_key]) end task :symlink, [:ssh_key, :release_name] do |t, args| log_task "Symlinking current to new release" release_path = "#{DEPLOY_TO}/releases/#{args[:release_name]}" ssh_exec("ln -sfn #{release_path} #{CURRENT_PATH}", args[:ssh_key]) end task :bundle_install, [:ssh_key, :release_name, :ruby_version] do |t, args| log_task "Installing gems" release_path = "#{DEPLOY_TO}/releases/#{args[:release_name]}" ssh_exec("cd #{release_path} && #{RBENV_PATH} local #{args[:ruby_version]} && #{RBENV_PATH} exec bundle install --deployment --without development test", args[:ssh_key]) end task :db_migrate, [:ssh_key, :ruby_version] do |t, args| log_task "Running database migrations" ssh_exec("cd #{CURRENT_PATH} && #{RBENV_PATH} local #{args[:ruby_version]} && #{RBENV_PATH} exec bundle exec rake db:migrate RAILS_ENV=production", args[:ssh_key]) end task :assets_precompile, [:ssh_key, :ruby_version] do |t, args| log_task "Precompiling assets" ssh_exec("cd #{CURRENT_PATH} && #{RBENV_PATH} local #{args[:ruby_version]} && #{RBENV_PATH} exec bundle exec rake assets:precompile RAILS_ENV=production", args[:ssh_key]) end task :restart, [:ssh_key] do |t, args| log_task "Restarting services" %w(paint sidekiq nginx puma).each do |service| ssh_exec("sudo systemctl restart #{service}", args[:ssh_key]) end end task :cleanup_remote, [:ssh_key] do |t, args| log_task "Cleaning up old releases on server" ssh_exec("cd #{DEPLOY_TO}/releases && ls -1tr | head -n -2 | xargs -d '\\n' rm -rf --", args[:ssh_key]) end task :cleanup_local, [:release_name] do |t, args| log_task "Cleaning up local package" sh "rm -f ./#{args[:release_name]}.tar" end def get_ruby_version(ssh_key, release_name) release_path = "#{DEPLOY_TO}/releases/#{release_name}" ssh_exec("cat #{release_path}/.ruby-version", ssh_key).strip end def ssh_exec(command, ssh_key) Net::SSH.start(SERVER, USER, keys: [ssh_key]) do |ssh| puts "Executing: #{command}" output = ssh.exec!(command) puts output if output exit_code = ssh.exec!("echo $?").to_i raise "Command failed: #{command}" if exit_code != 0 output.strip end rescue => e puts "Error: #{e.message}" exit 1 end end
你觉得怎么样?

© www.soinside.com 2019 - 2024. All rights reserved.