what's the differences between these two tasks, why i need to add RAILS_ENV=production when cap deploy?
thanks!
You need to specify RAILS_ENV=production environment variable so that your config/environments/production.rb configuration file is used when precompiling assets. It usually contains production configuration for assets pipeline:
config.assets.js_compressor = :uglifier
config.assets.digest = true
If you omit RAILS_ENV=production then development configuration will be used (config/environments/development.rb).
The first one will precompile your assets on your local dev box (development environment) and the other will precompile your assets on your production environment. Your settings in your config files are most likely different and so it will go of what is configured off what is in the environment config for whatever you set RAILS_ENV to.
Was going to write as a comment but too long...
--
Production vs Local
Something you also need to consider with this, is if you're precompiling for the production environment, it essentially compiles & configures your files for that environment
Simply, this means if you have any special conditions / dependencies for production only, using RAILS_ENV=production will use these over your local setup. This is why you'll have this setup in your Gemfile:
#Gemfile
group :production do
gem 'xxxx'
end
--
SHELL VARIABLES
Something else you need to appreciate is RAILS_ENV is a SHELL VARIABLE. This means that whenever you run a shell session (I.E load cmd), these variables can be set to provide specific functionality.
In relation to RAILS_ENV, it means you'll be able to tell Rails to run in production mode for the time being; as opposed to running in development, testing or staging modes
Related
For my Rails 3.2/Capistrano 3 application I configured 2 deploy environments: staging and production.
I don't want to spend time on assets compilation when deploy to staging, since it should be the same as local development.
In my Capfile I have
require 'capistrano/rails/assets'
which is needed for production, but with this line Capistrano also compiles assets for all other environments.
How can I configure it to compile assets(or skip compilation) for specific environments?
Try adding this line to config/environments/staging.rb file
config.assets.compile = false
I have rails 4.1 app and multistage (staging, production) deployment with capistrano3.
I want to deploy it to one stage server (which use rvm) and one production server (which use ruby env)
By default everything works nice on production server, but without rvm1-capistrnao3 gem installed and included in Capefile I cannot deploy to staging.
Is there a way to require 'rvm1/capistrano3' in Capefile, only if I deploy to staging like that
cap staging deploy
Here is what I've done to fix it
I made default capistrano multistage setup, like Doug Hall said!
The tricky part is the way to include rvm1-capistrano3 in Capefile
See deepak's workaround here https://github.com/capistrano/rvm/issues/49
So instead of just require 'rvm1/capistrano3' in Capefile, do it like that
task :use_rvm do
require 'rvm1/capistrano3'
end
task 'staging' => [:use_rvm]
When you run cap install, it creates a file called config/deploy.rb and two files in the config/deploy directory: production.rb and staging.rb. Use the config/deploy.rb file for all settings that both the production and staging servers have in common. Use the other two for the respective settings on those machines. I would require 'capistrano/rvm' in your Capfile, but only use it in the config/deploy/staging.rb file. Capistrano executes the common config/deploy.rb FIRST, then calls the proper staging.rb/production.rb file, so all set values from config/deploy.rb are available in the staging.rb/production.rb file.
In my vhost, I have:
<Directory /var/www/prod/myapp/myapp/public>
...
RailsEnv production
...
</Directory>
and while any code dependent on it being production is correctly running there in the app itself (ex: display the Google Analytics code if Rails.env.production?), when I run rake about from /var/www/prod/myapp/myapp, I get:
Application root /var/www/prod/myapp/myapp
Environment development
Database adapter mysql2
which means that I have to prefix any deployment related rake stuff with RAILS_ENV=production. Granted, it's all in a deployment script at this point so it doesn't matter much, but why isn't Rake aware that it's production? Shouldn't the Passenger setting be enough? And if not, how do I fix it so I won't need to specify the environment manually?
Side-note: I am running the development instance of the app on the same box, with Environment set to development in its corresponding vhost configuration.
EDIT: Phusion Passenger version 4.0.20
rake is entirely unaware of passenger's configuration. It doesn't even know that you're using passenger. Since it isn't launched by passenger, it would have to (assuming it knew you were using apache/passenger) parse the apache config files to find this out, which would get pretty complicated, especially in the presence of multiple apps.
You could set this in one of your shell's startup files, however that doesn't sound like a good idea if you have multiple environments on the same machine.
You could stick
ENV['RAILS_ENV']='production'
At the top of one of Rails' startup files - boot.rb seems to do the trick. This would make passenger's setting ineffective though, and obviously you would only want to do this on the production deploy of your script.
Personally (especially on a machine with multiple environments in action) I'd stick to typing RAILS_ENV=production.
but why isn't Rake aware that it's production?
Because rake and all rails related commands internally do this
ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
which means that if you didnt have RAILS_ENV or RACK_ENV environment, then it will use development environment by default.
Shouldn't the Passenger setting be enough?
NO. Passenger setting is just for making your app up/live in desire or say virtual environment. It doesn't change any system configuration.
And if not, how do I fix it so I won't need to specify the environment manually?
That's very easy. Just set environment variable. If you are using bash (normally people do) you can simple do this
echo "export RAILS_ENV=production" >> ~/.bashrc ; source ~/.bashrc
This will make your server as rails production server, so if you run rails c or rake db:migrate etc or any rails or rake command it will run in production server for all available applications.
I am running the development instance of the app on the same box, with Environment set to development in its corresponding vhost configuration.
This is a problem. If you set the above environment variable, then whenever you run any code like rake db:migrate inside this app ..that will run in production environment. To avoid this either you dont set environment variable or specify environment on every command. There is one hack available . Include this line at the top of config/boot.rb
ENV["RACK_ENV"] = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
Future reading : How can I make a custom environment in rails a default environment?
what's the differences between these two tasks, why i need to add RAILS_ENV=production when cap deploy?
thanks!
You need to specify RAILS_ENV=production environment variable so that your config/environments/production.rb configuration file is used when precompiling assets. It usually contains production configuration for assets pipeline:
config.assets.js_compressor = :uglifier
config.assets.digest = true
If you omit RAILS_ENV=production then development configuration will be used (config/environments/development.rb).
The first one will precompile your assets on your local dev box (development environment) and the other will precompile your assets on your production environment. Your settings in your config files are most likely different and so it will go of what is configured off what is in the environment config for whatever you set RAILS_ENV to.
Was going to write as a comment but too long...
--
Production vs Local
Something you also need to consider with this, is if you're precompiling for the production environment, it essentially compiles & configures your files for that environment
Simply, this means if you have any special conditions / dependencies for production only, using RAILS_ENV=production will use these over your local setup. This is why you'll have this setup in your Gemfile:
#Gemfile
group :production do
gem 'xxxx'
end
--
SHELL VARIABLES
Something else you need to appreciate is RAILS_ENV is a SHELL VARIABLE. This means that whenever you run a shell session (I.E load cmd), these variables can be set to provide specific functionality.
In relation to RAILS_ENV, it means you'll be able to tell Rails to run in production mode for the time being; as opposed to running in development, testing or staging modes
I was compiling my asset pipeline for my production environment and it did for all my environments. How can I uncompile my asset pipeline for my development environment?
I have checked my config/development environment and cannot find a fix.
Thanks in advance for any help...
To remove precompiled assets use:
rake assets:clean
What this basically does is remove the public/assets directory. You may need to include the RAILS_ENV variable if you need to run it for a certain environment.
Try using
rake assets:clobber
worked for me in rails 4
When you run the compile task locally (on your development machine) the assets are compiled in the Rails production environment, but are written to the public folder.
This means that even when you run in development mode it'll use the compiled assets instead of sending requests to the pipeline. This is normal behavor - requests only go to the pipeline if the file does not exists in public/assets.
The compile task should generally only be used when deploying, and on the remote (production) machine.
If you have compiled locally, you can delete all the files in the public/assets folder and development will behave as before. If you checked these files into source control you'll need to remove them.
Once removed things should work fine.
s
One final tip: if this is an upgraded app check your config settings against those in the last section of the Rails asset pipeline guide.
For Rails 5:
$ RAILS_ENV=development bin/rake assets:clobber