Rails 6 API deployment error on Railway - rake assets:precompile - ruby-on-rails

I am trying to deploy my Rails 6 API app on Railway and keep getting this error:
#18 [stage-0 14/14] RUN bundle exec rake assets:precompile
#18 sha256:20d51ba84b3edc1879a0fe36e13a5cfe583beb59329a77a09bdbd9289c2403d7
#18 4.000 rake aborted!
#18 4.000 Don't know how to build task 'assets:precompile' (See the list of available tasks with `rake --tasks`)
#18 4.000 /usr/local/rvm/gems/ruby-2.7.2/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
#18 4.000 /usr/local/rvm/gems/ruby-2.7.2/bin/ruby_executable_hooks:22:in `eval'
#18 4.000 /usr/local/rvm/gems/ruby-2.7.2/bin/ruby_executable_hooks:22:in `<main>'
#18 4.000 (See full trace by running task with --trace)
#18 ERROR: executor failed running [/bin/bash -ol pipefail -c bundle exec rake assets:precompile]: exit code: 1
-----
> [stage-0 14/14] RUN bundle exec rake assets:precompile:
-----
executor failed running [/bin/bash -ol pipefail -c bundle exec rake assets:precompile]: exit code: 1
Error: Docker build failed
My application.rb file:
require_relative "boot"
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
# require "sprockets/railtie"
require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module AnimeAxisApi
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore
config.action_dispatch.cookies_same_site_protection = :none
# goes after cookie store
# , key: '_cookie_name'
end
end
Ive added this task in lib/tasks and it still does not stop it the assets:precompile task from running
Rake::Task["assets:precompile"].clear
namespace :assets do
task 'precompile' do
puts "Not pre-compiling assets..."
end
I know that this is because I don't have any assets to compile with an API-only app. I deployed on Heroku previously and did not have this error. I can't find any other posts providing an answer to this, either they mention it and don't provide a solution or it's something to do with Capistrano. I have a plain Rails app. How can I remove this task from being run?

Related

Heroku Deploy: URI::InvalidURIError: bad URI(is not URI?): ://user:pass#127.0.0.1/dbname

I have a project with a Vue frontend and a rails backend. It's just a very simple API on the backend, no database or anything. It's working fine locally but now I want to deploy it on Heroku.
However, when I run it I get the following error.
-----> Detecting rake tasks
!
! Could not detect rake tasks
! ensure you can run `$ bundle exec rake -P` against your app
! and using the production group of your Gemfile.
! rake aborted!
! URI::InvalidURIError: bad URI(is not URI?): ://user:pass#127.0.0.1/dbname
...
...
/activerecord-6.0.2.1/lib/active_record/railties/databases.rake
Based on various SO posts/Heroku documentation, I have already tried:
bundle exec rake -P RAILS_ENV=production - everything looks ok
adding the rake dependency in Gemfile
removing the sqlite dependency in Gemfile
removing the BUNDLED WITH from Gemfile.lock
But still the same error.
I guess it's related to my database config, but I don't have any database on my project so this seems like an unnecessary task anyway. I tried commenting out railties from my Gemfile but it's still there as a dependency for other gems. When I deploy after making this change, it still hits the same task and fails.
Link to repo branch
Instead of require 'rails/all' which requires all the Railties including ActiveRecord you need to explicitily require the railties you want to use:
require File.expand_path('../boot', __FILE__)
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
# require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
module Draw
class Application < Rails::Application
# You don't need this nonsense since you don't even have config/application.yml
# ENV.update YAML.load_file('config/application.yml')[Rails.env] rescue {}
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
# config.active_record.raise_in_transactional_callbacks = true
end
end
If you don't want to use a ActiveRecord you can just get rid of /db and /config/database.yml.
You also don't need to add have gem 'rake' in your Gemfile as rails depends on it anyways.
Needed to fully remove use of ActiveRecord from the project. As Max commented, in a new app this can be done by doing rails new app_name --skip-active-record --api, to do this for an existing project see this explanation

Deploy raise error "Don't know how to build task 'assets:precompile'" (Capistrano)

Does anyone ever got this error after run :
cap production deploy
I'm Using capistrano : Capistrano Version: 3.6.1 (Rake Version: 11.3.0)
Here is the log :
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy#54.169.99.36: rake exit status: 1
rake stdout: rake aborted!
Don't know how to build task 'assets:precompile' (see --tasks)
/home/deploy/microwave-api/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
.
.
.
/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-1.13.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-1.13.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/home/deploy/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
rake stderr: Nothing written
SSHKit::Command::Failed: rake exit status: 1
rake stdout: rake aborted!
Don't know how to build task 'assets:precompile' (see --tasks)enter code here
Sounds like you probably don't have Sprockets set up (an API only application?).
In your Capfile, you probably have something like:
require 'capistrano/rails'
That line actually requires a file which looks like:
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
So you can replace the former line with just:
require 'capistrano/rails/migrations'
and the asset precompilation will no longer be run.
As mentioned above, this assumes that you don't actually want to use the asset pipeline. If this isn't the case, the issue is that you aren't including Sprockets and you need to look into that. I'd generate a new rails app and compare your Gemfile and config/application.rb.
If you rails application is API only, do not add require 'capistrano/rails/assets' or require "capistrano/rails".
Add following:
require 'capistrano/bundler'
require 'capistrano/rails/migrations'
At the end your Capfile will look something like this:
require "capistrano/setup"
require "capistrano/deploy"
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
require "capistrano/rbenv"
set :rbenv_type, :user
set :rbenv_ruby, "2.5.1"
require 'capistrano/bundler'
require 'capistrano/rails/migrations'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

rake db:create issue

I have just started working on one project, and I have configured my system according to the requirement of the project i.e.
rails v => 2.3.11
ruby v => 1.8.7
When i run command "rake db:create" it comes up with error as
rake aborted!
no such file to load -- rake/rdoctask
/home/jeet/Desktop/Projects/myapp/Rakefile:8
here i am adding the content of Rakefile
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
I have checked the answer of this, but its not working for me.
Please suggest me a better solution
I have seen the same problem.
Possible solution: by adding gem 'rdoc' to my Gemfile and then run bundle install.

When I run "rake routes", or any rake task, it always loads my environment first

I'm trying to figure out why my non-environment rake tasks, like:
rake routes
takes so long to run. It takes over a minute as it loads the complex environment of my application. This is a Rails 2.3.12 application, and my Rakefile is as follows:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/switchtower.rake, and they will automatically be available to Rake.
require 'thread'
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rdoc/task'
require 'delayed/tasks'
require 'tasks/rails'
calling rake routes does depend on the rails stack and because auf this, the environment is loaded.
what you can do, is use a third party gem like sextant: https://github.com/schneems/sextant/

Installing gems tries to load the gems in my custom rake tasks before installing it

I'm developing an application using rails 2.3.5, gitorious and deploy with vlad, the OS is Linux Mint 9 Isadora.
vlad:setup and vlad:update are ok.
But when I vlad:migrate and have the same error than if I ssh on the server and try a rake gems:install
rake aborted!
no such file to load -- ya2yaml
/var/www/path/to/releases/20100622030150/Rakefile:10
(See full trace by running task with --trace)
My config/environment.rb is good:
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
config.gem "haml"
I have a custom task in lib/tasks/db_fixtures.rake that requires ya2yaml:
namespace :export do
desc 'Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override.'
require 'rubygems'
require 'ya2yaml'
task :fixtures => [:environment] do
When I rename this file it is not loaded by rake and I don't have the error anymore when I rake gems:install
So my guess is that it looks like rake gems:install tries to load the libs in my custom tasks before installing the gems for some reason and throw me an error.
What would be a solution?
Thanks,
this might work? delay the requires on your rake task by moving them into the task itself
task :fixtures => [:environment] do
require 'rubygems'
require 'ya2yaml'
# ...
by default running rake tasks 'loads' all of the rake files

Resources