How do I skip a rake task - rake

Consider the following Rake tasks:
task deploy => [:package] do
end
task package => [:build] do
end
task build do
end
Is there a way to invoke Rake on the command line to execute the package and deploy tasks, but not the build task?

Short answer, no.
The way I usually go about this is instead of using the dependant task notion like you have above:
task deploy => [:package] do
end
I create an alias task for whatever action that is to be completed:
task all => [:build, :package, :deploy]
task fastDeploy => [:package, :deploy]
task deploy do
end
task package do
end
task build do
end
It's not very elegant, but I do find it to be more readable and you can visibly see the dependency of tasks on other tasks instead of the kind of spaghetti code structure the dependant notion can result in... when you have a lot of task it can be awkward to debug the logic to figure what's gone wrong and where at times.
Hope this helps.

Related

Why isn't environments/test.rb required when `rake test` is run via custom Rake task?

For $REASONS, I created a Rake task to paper over rake test. However, when I use the wrapper task, config/environments/test.rb is never required and, as a result, undesirable things happen (emails are sent out, the database is dropped, etc.).
(FWIW, Rails.env and ENV['RAILS_ENV'] are still set to test in the degenerate case.)
namespace :organization do
desc "Run unit tests."
task :unit do
puts Rails.env # test
Rake::Task["test"].invoke
end
end
you need to pass the :environment in rake tasks for it to work.
namespace :organization do
desc "Run unit tests."
task unit: [:environment] do
puts Rails.env # test
Rake::Task["test"].invoke
end
end

Rake like dependency functionality in Elixir Mix tasks?

In Rake, one can specify dependencies between tasks. The engine then build a dependencies tree and perform those tasks by the order of dependencies and only once each task.
Is there a similar mechanism for that in elixir/mix ?
task seed_users: [:seed_companies] do
# actions
end
task :seed_companies do
# actions
end
I don't think there's any inbuilt functionality for this, but you can use Mix.Task.run/2 to achieve this:
defmodule Mix.Tasks.SeedUsers do
def run(_args) do
IO.puts "started seed_users"
Mix.Task.run "seed_companies"
Mix.Task.run "seed_companies"
IO.puts "completed seed_users"
end
end
defmodule Mix.Tasks.SeedCompanies do
def run(_args) do
IO.puts "started seed_companies"
IO.puts "completed seed_companies"
end
end
Example run:
$ mix seed_users
started seed_users
started seed_companies
completed seed_companies
completed seed_users
Note that Mix.Task.run/2 does not run the task if it has already been run once, so if you call Mix.Task.run/2 twice, as in the example above, it's only run once. If you'd like to run a task more than once, you need to call Mix.Task.reenable/1 after every run.

Stop the pipeline when stage is unstable

I have a Jenkins build pipeline created using workflow plugin. At the beginning the pipeline runs a gulp build inside of the docker container and then archives test results using the following code
step([$class: 'JUnitResultArchiver', testResults: 'build/test-results/*.xml'])
In the following steps I package up the artifacts and ship them to the binary repository.
When unit tests are not passing Jenkins understands the build is unstable and marks it yellow. However it still continues with subsequent steps in the pipeline. Is there any way make the pipeline stop when unit tests are failing?
the JUnitResultArchiver will cause this condition to be true when the build is unstable:
currentBuild.result != null.
If I remember correctly it sets it to UNSTABLE, but it is enough to check that is different than null.
So you could do something like
step([$class: 'JUnitResultArchiver', testResults: 'build/test-results/*.xml'])
if (currentBuild.result == null) {
//contintue with your pipeline
} else {
//notify that the build is unstable. //or just do nothing
}
There is nothing to do at Jenkins side but at Gulp side. The call to gulp CLI needs to return a proper error value to have the sh step failing correctly.
Jenkins just interprets what the shell is returning, so you juts need to make Gulp to return a fail when tests fail (see this blog post, it seems to achieve exactly that).

Capistrano: disable db:migrate

How do you disable db:migrate when doing a cap deploy:cold with Capistrano?
In config/deploy.rb the only reference to deploy:migrate is commented out but it's still attempting to do:
bundle exec rake RAILS_ENV=production db:migrate
I got success by overriding the deploy:migrate method in my config/deploy.rb.
namespace :deploy do
desc "No ActiveRecord override"
task :migrate do
end
end
When re-defining a task in Capistrano v2, the original task was replaced. However the Rake DSL on which Capistrano v3 is built is additive. According to documentation.
Under most circumstances, you will simply want to use clear_actions, which removes the specified task’s behaviour, but does not alter it’s dependencies or comments:
namespace :deploy do
Rake::Task["migrate"].clear_actions
task :migrate do
puts "no migration"
end
end
I had the same problem. That's why I override it in the Rakefile. Like this:
namespace :db do
desc "db:migration fakes"
task :migrate => :environment do
p 'No. We will not migrate!'
end
end
Here you can add more logic if you like. You could for example trigger a real migration on certain environments.

Inherit roles from parent tasks in Capistrano callbacks

I have several tasks which all must check that the machines serving as roles have a certain file with certain contents. The logic is reasonable to separate into a prerequisite, or a callback.
task t1, :roles => [:r1] do
...
end
task t2, :roles => [:r2,:r3] do
...
end
before <what?> do
# must only run on :r1 when triggered by t1,
# and only on :r2 and :r3 when triggered by t2!
<ensure role given to parent task has a given file>
end
How do we do that in Capistrano?
It turns out that a before callback can invoke a regular def, in which case it runs for the roles of the parent task. If, however, you call a task there, and that task has no roles, all roles will be used to run it. The real question is where are the dependencies across tasks...