Skip to content

Instantly share code, notes, and snippets.

@bestlong
Last active August 29, 2015 14:23
Show Gist options
  • Save bestlong/84608b2825419d00aa26 to your computer and use it in GitHub Desktop.
Save bestlong/84608b2825419d00aa26 to your computer and use it in GitHub Desktop.
cap 執行過程
### lib/capistrano/tasks/touch_all_linked_files.rake
namespace :deploy do
namespace :check do
# desc 'Touches all linked files'
task :touch_all_linked_files do
on release_roles :all do
fetch(:linked_files, []).each do |file|
target = shared_path.join(file)
execute :touch, target
info "Touched : #{target}"
end
end
end
end
before 'deploy:check:linked_files', 'deploy:check:touch_all_linked_files'
end
### lib/capistrano/tasks/touch_all_linked_files.rake
### 增加判斷檔案不存在時才用 touch 指令產生
namespace :deploy do
namespace :check do
# desc 'Touches all linked files'
task :touch_all_linked_files do
on release_roles :all do
fetch(:linked_files, []).each do |file|
target = shared_path.join(file)
# execute :touch, target
# info "Touched : #{target}"
unless File.file?(target)
execute :touch, target
info "Touched : #{target}"
end
end
end
end
end
before 'deploy:check:linked_files', 'deploy:check:touch_all_linked_files'
end
$ cap production deploy:check:linked_files --trace
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke rvm:hook (first_time)
** Execute rvm:hook
DEBUG [df8c9512] Running /usr/bin/env [ -d ~/.rvm ] as [email protected]
DEBUG [df8c9512] Command: [ -d ~/.rvm ]
DEBUG [df8c9512] Finished in 0.864 seconds with exit status 0 (successful).
** Invoke rvm:check (first_time)
** Execute rvm:check
DEBUG [f91323a2] Running ~/.rvm/bin/rvm version as [email protected]
DEBUG [f91323a2] Command: ~/.rvm/bin/rvm version
DEBUG [f91323a2] rvm 1.26.11 (latest) by Wayne E. Seguin <[email protected]>, Michal Papis <[email protected]> [https://rvm.io/]
DEBUG [f91323a2] Finished in 0.459 seconds with exit status 0 (successful).
rvm 1.26.11 (latest) by Wayne E. Seguin <[email protected]>, Michal Papis <[email protected]> [https://rvm.io/]
DEBUG [556a0efa] Running ~/.rvm/bin/rvm current as [email protected]
DEBUG [556a0efa] Command: ~/.rvm/bin/rvm current
DEBUG [556a0efa] ruby-2.1.5
DEBUG [556a0efa] Finished in 0.461 seconds with exit status 0 (successful).
ruby-2.1.5
DEBUG [96a63a62] Running ~/.rvm/bin/rvm default do ruby --version as [email protected]
DEBUG [96a63a62] Command: ~/.rvm/bin/rvm default do ruby --version
DEBUG [96a63a62] ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]
DEBUG [96a63a62] Finished in 1.035 seconds with exit status 0 (successful).
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]
** Invoke deploy:set_rails_env (first_time)
** Execute deploy:set_rails_env
** Invoke deploy:set_rails_env
** Invoke deploy:check:linked_files (first_time)
** Invoke deploy:check:touch_all_linked_files (first_time)
** Execute deploy:check:touch_all_linked_files
INFO [a631c1ca] Running /usr/bin/env touch /home/railsfuntw/projects/gwsecurity/production/shared/.env as [email protected]
DEBUG [a631c1ca] Command: /usr/bin/env touch /home/railsfuntw/projects/gwsecurity/production/shared/.env
INFO [a631c1ca] Finished in 0.113 seconds with exit status 0 (successful).
INFO Touched : /home/railsfuntw/projects/gwsecurity/production/shared/.env
INFO [35434686] Running /usr/bin/env touch /home/railsfuntw/projects/gwsecurity/production/shared/config/database.yml as [email protected]
DEBUG [35434686] Command: /usr/bin/env touch /home/railsfuntw/projects/gwsecurity/production/shared/config/database.yml
INFO [35434686] Finished in 0.110 seconds with exit status 0 (successful).
INFO Touched : /home/railsfuntw/projects/gwsecurity/production/shared/config/database.yml
** Execute deploy:check:linked_files
DEBUG [52d0ee58] Running /usr/bin/env [ -f /home/railsfuntw/projects/gwsecurity/production/shared/.env ] as [email protected]
DEBUG [52d0ee58] Command: [ -f /home/railsfuntw/projects/gwsecurity/production/shared/.env ]
DEBUG [52d0ee58] Finished in 0.110 seconds with exit status 0 (successful).
DEBUG [8de1ccf0] Running /usr/bin/env [ -f /home/railsfuntw/projects/gwsecurity/production/shared/config/database.yml ] as [email protected]
DEBUG [8de1ccf0] Command: [ -f /home/railsfuntw/projects/gwsecurity/production/shared/config/database.yml ]
DEBUG [8de1ccf0] Finished in 0.107 seconds with exit status 0 (successful).
AllenMBP:GWSecurity allen
$
以上的 INFO Touched : /home/railsfuntw/projects/gwsecurity/production/shared/.env
就是用 info "Touched : #{target}" 所印出的內容
@bestlong
Copy link
Author

結論是:
File.file?() 是執行在本機的環境,所以實質檢查的本機專案目錄下的檔案。
所以要改用 sshkit 的命令 test() 來處理
程式可以改成
unless test("[ -f #{tagret} ]")
...
end

https://github.com/capistrano/sshkit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment