Capistrano custom task calling git:create_release - ruby-on-rails

I have created a custom capistrano task located in the lib/capistrano/tasks directory.
I have placed the task under a namespace. When I run cap -T my task appears in the list.
I want to call the git:create_release task inside my task. Currently I have:
namespace :setup do
desc "Performs a setup"
task :run do
on roles(:all) do
git.create_release
end
end
end
However, the above does not work. It gives me the following error:
undefined local variable or method `git'
Now I assume the issue lies in not having some explicit import of the git submodules. I am not sure how to go about including them in the custom tasks module.

Instead of git.create_release I think you need invoke "git:create_release". See the Capistrano README.

Related

First API testing Framework with Rake

I am setting up my own API testing framework but I am using this guide as a foundation.
In the guide it uses rake to run the tests from the rake file by using the default rake command.
The command runs all tests under the spec folder; however, I want to create a rake task I can run from the command line and just pass in which file under that folder I would like to run.
I.E. I want to run one script at a time from the command line, instead of running every file with the default rake command.
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
task :run do |file|
:spec => file
end
You can see the default rake command will run all tests under spec folder; however, I just want to pass in the file I want to run. How can I achieve this?

enhancing the global environment task rails

On the application I am upgrading from Rails 3.2.22.4 to Rails 4.0.13, the following block of code for enhancing the global environment task has become a road-block by not working on the target Rails version:
Rails.application.class.rake_tasks do
Rake::Task["environment"].enhance do
...
end
end
This works fine on 3.2, but fails with Don't know how to build task 'environment' error message in 4.0.
In 3.2, Rails.application.class.rake_tasks returns a Proc object ( [#<Proc:0x007f978602a470#.../lib/rails/application.rb:301>] ) pointing to this line in the rails codebase. On 4.0, it returns an empty array.
The line referred to in the above Proc object seems to be removed in this commit.
What would the preferred way to enhance the environment rake task be in Rails 4.x?
The above piece of code is in the lib/subdomain/rake.rb file, and it is include with the following code in lib/subdomain/engine.rb:
module Subdomain
class Engine < Rails::Engine
...
rake_tasks do |_app|
require 'subdomain/rake'
end
...
end
end
Rake tasks can't be executed as the command fails with this error. rails server|console commands work ok.
Option 1
If I'm understanding the question properly, something like this should work by placing these tasks in a standard location like lib/tasks/environment.rake. Note: None of this is particularly Rails-specific.
# Re-opening the task gives the ability to extend the task
task :environment do
puts "One way to prepend behavior on the :environment rake task..."
end
task custom: :environment do
puts "This is a custom task that depends on :environment..."
end
task :environment_extension do
puts "This is another way to prepend behavior on the :environment rake task..."
end
# You can "enhance" the rake task directly too
Rake::Task[:environment].enhance [:environment_extension]
The output of this would be:
$ rake custom
This is another way to prepend behavior on the :environment rake task...
One way to prepend behavior on the :environment rake task...
This is a custom task that depends on :environment...
Option 2
However, the question remains as to why :environment needed to be extended. If it's to trigger something before, say, a db:migrate, you might be better off just re-opening the task in question and adding another dependency to that particular task. For example:
task custom: :environment do
puts "This is a custom task that depends on :environment..."
end
task :custom_extension do
puts "This is a new dependency..."
end
# Re-opening the task in question allows you to added dependencies
task custom: :custom_extension
The result of this is:
$ rake custom
This is a new dependency on :custom
This is a custom task that depends on :environment...
C-C-C-Combo Breaker!!
Combining everything, the output would look like this:
$ rake custom
This is another way to prepend behavior on the :environment rake task...
One way to prepend behavior on the :environment rake task...
This is a new dependency on :custom
This is a custom task that depends on :environment...

NoMethodError: undefined method `within' for main:Object

I'm trying to make Capistrano deploy script.
In my Capfile I make sure all rake tasks are included
# Load tasks
Dir.glob('config/capistrano_tasks/*.rake').each { |r| import r }
Next I have a 'migrations.rake' containing:
namespace :fileservice do
task :migrate do
within release_path do
info 'Doing migrations'
execute :php, fetch(:symfony_console_path), 'doctrine:migrations:migrate', '--no-interaction', fetch(:symfony_console_flags)
end
end
end
In my deploy.rb I call the task at the very end with:
after 'deploy:publishing', 'fileservice:migrate'
For some reason I keep getting an error saying:
NoMethodError: undefined method `within' for main:Object
I have no idea where to look or what might be wrong... When googling I get a lot of "NoMethodError" hits but none about the 'within' method and most are general Ruby errors.
Where should "within" be defined? I dat a ruby on rails thing? Or capistrano?
Hopefully someone knows where to start looking or which library / script to include!
UPDATE: I just discovered that none of the methods work. When removing lines I got the same error for "info" and "execute".... So I guess somewhere, something is missing.....
You need to tell Capistrano where (i.e. on what servers) to run your SSH commands. Do this using an on block, as follows:
namespace :fileservice do
task :migrate do
on roles(:db) do
within release_path do
info 'Doing migrations'
execute :php, fetch(:symfony_console_path), 'doctrine:migrations:migrate', '--no-interaction', fetch(:symfony_console_flags)
end
end
end
end
Replace roles(:db) as appropriate for your task, depending on where you want the commands to be run. The expression on roles(:all) do ... end, for example, will run the commands on all servers.
You can also follow the official documentation at http://capistranorb.com, or the Capistrano README, both of which show examples of the task/on/execute syntax.
As you already got the answer but Let me put the bulb from basic.
SSHKit was actually developed and released with Capistrano 3, and it’s basically a lower-level tool that provides methods for connecting and interacting with remote servers; it does all the heavy lifting for Capistrano, in other words.
There are four main methods you need to know about.
on(): specifies the server to run on
within(): specifies the directory path to run in
as(): specifies the user to run as
with(): specifies the environment variables to run with
Typically, you’ll start a task by using an on() method to specify the server on which you want your commands to run. Then you can use any combination of as(), within(), and with() methods, which are repeatable and stackable in any order.
Hope this help you

Capistrano 3, using upload! in a task in lib/capistrano/tasks

I'm using Capistrano 3 and I want to create my own task. So I created a file my_new_thing.rake in lib/capistrano/tasks and I can see the task when I run cap -T. But... some of the methods aren't available. When I try to use upload! I get
cap aborted!
NoMethodError: undefined method `upload!' for main:Object
But if I move the same task into config/deploy.rb then then upload! method is available.
So what's going on? How do I create new Capistrano tasks put them in separate file and have them work?
I had the same problem, I created my own recipe in a separate file which I loaded in deploy but couldn't get upload! to work.
What fixed it for me was adding a role filter inside the task making my final recipe look something like this:
namespace :figaro do
desc "Transfer Figaro's application.yml to shared/config"
task :upload do
on roles(:all) do
upload! "config/application.yml", "#{shared_path}/config/application.yml"
end
end
end
before "deploy:check", "figaro:upload"
I hope that helps!
You can create a folder config/recipes for your capistrano recipes if you want to keep them in separate files.
Use the .rb extension since this isnt a regular rake task.
In config/deploy.rb add this line
load File.expand_path('../recipes/my_new_thing.rb', __FILE__)
If you want to use the rake tasks then you will need to create a task in the deploy file which calls that rake tasks which is not that smart of a move. So as #Sharagoz suggested the best route will be to create your own recipe file and include that in.

vlad the deployer vlad:start_app with passenger issue

I'm trying to deploy a rails app using vlad the deployer.
I'm using nginx and passenger.
I have an issue with the vlad:start_app task.
When I deploy I get the following issue
touch: cannot touch `/var/www/mysite.com/releases/20100623130302/tmp/restart.txt': No such file or directory
rake aborted!
execution failed with status 1: ssh mysite.com touch /var/www/mysite.com/releases/20100623130302/tmp/restart.txt
The issue is obvious in that the 20100623130302 in releases does not exist.
I would rather use the following task but cant override the default vlad:start_app task by placing this in my config/deploy.rb file.
namespace :vlad do
desc 'Restart Passenger'
remote_task :start_app do
run "touch #{current_path}/tmp/restart.txt"
end
end
Any help appreciated. The options I though of are to either get the default vlad task to work or someway to override the default vlad task.
RAILS_ROOT/Rakefile is the file.
To override a task you have to remove the previous one first as defining the same task again just creates a second task that will run after the first one.
Here's an example from the vlad website how to replace a task:
namespace :vlad do
# Clear existing update task so that we can redefine instead of adding to it.
Rake.clear_tasks('vlad:update')
remote_task :update, :roles => :app do
#custom update stuff
end
end

Resources