如何 "skip" rake 中的任务

How to "skip" a task in rake

当使用 rake 构建文件时,构建系统足够智能,可以判断它是否需要实际 运行 和 task,例如该文件已经存在并且依赖项不是最新的。

是否有跳过其他任务的标准方法?我在想类似的东西

task :containers do
  sh "docker-composer up"
end

# the following doesn't exist
task :containers, if: `docker ps | grep mycontainer`.empty?

您可以随时使用 next 关键字来 "skip out" 任务,例如

task :containers do
  next if `docker ps | grep mycontainer`.empty?
  sh "docker-composer up"
end

并且不会中断队列中其他任务的流程。

或者,您可以将任务代码包装在任务定义中的 if 语句中,如果条件失败,则可能打印出一些内容。