Rails asset fingerprints different in CI/dev vs production - ruby-on-rails

I want to precompile rails assets (in particular my js code via webpacker) as part of my docker build.
Currently, I build a docker container w/my rails app and run rails assets:precompile in the container as part of my deploy right before starting the rails server. Instead, I'd rather run rails assets:precompile as the final step of building the container so that the assets can be inside the container and available immediately upon deploy.
For some reason when I run rails assets:precompile inside the Dockerfile (or even on my local machine) I end up w/different asset fingerprints/filenames than I do in production.
I have tried copying as many environment vars as I can think of from prod to make sure the environments match as closely as possible, but nothing seems to work.
In my docker build:
RAILS_ENV=production NODE_ENV=production ASSET_HOST=my-prod-asset-host SECRET_KEY_BASE=dummy bundle exec rails assets:precompile
Produces output including:
Entrypoints:
application (3.48 MiB)
js/runtime~application-7f0b7ea84eeecb8cc7fc.js
css/vendors~application-66d66fb1.chunk.css
js/vendors~application-186ff851767460d85d4c.chunk.js
js/application-08e980516ae0c9453937.chunk.js
But when I run bundle exec rails assets:precompile in production I get:
Entrypoints:
application (3.54 MiB)
js/runtime~application-7f0b7ea84eeecb8cc7fc.js
css/vendors~application-66d66fb1.chunk.css
js/vendors~application-40fc8fbc9d713e9d5866.chunk.js
js/application-9e7161ae80584d3213ab.chunk.js
How can I go about determining what is different between prod and dev/docker/etc. so that I can figure out how to generate my production assets outside of production?

Related

Compiling Assets in Ruby does it need to be done in deployment? Why not before?

So this is just a question I've been wondering about lately, I seem to spend a significant amount of time when deploying waiting for assets to compile.
Why can we not compile the assets on our development machines and submit it in the git repo?
I am currently using ruby 2.5.x and rails 5.2.4.x using the assets pipeline with uglifier.
Is there a way to do this in development and thus disable all asset compilation during deployment?
many thanks,
Simon
The solution was pretty simple
1) disable asset compiling during deployment
I'm using elastic beanstalk so just had to set this in the environment variables
2) Either
Add the /public/assets folder to git
Or for elastic beanstalk create .ebignore file and copy over the .gitignore but remove the /public/assets entry
3) before deploying run
Linux -- RAILS_ENV=development bundle exec rake assets:precompile
Windows -- set RAILS_ENV=development && bundle exec rake assets:precompile
4) deploy code to server as normal
eb deploy
1st: Why can we not compile the assets on our development machines and submit it in the git repo?
The reason why we need to precompile are:
Compressed the assets resources then cached some static content like images, css and so on.
It's help us to generates two files (.css and .js) and compressed all our's css file event it's file from vendors:
<script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"></script>
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen"
rel="stylesheet" />
It's very helpful but It's take time and `And the resource it not live reload when you modify some code. You have to re-compile to apply the code.
-> So, That's why you should not compile the assets in DEVELOPMENT evironment.
2nd: Is there a way to do this in development and thus disable all asset compilation during deployment?
You also use precompile on DEVELOPMENT by run this command:
RAILS_ENV=development bundle exec rake assets:precompile
You can precompile assets in development environment by default using
config/development.rb
config.assets.debug = false
Thanks. Hope it's help
In my case, I precompile before deployment, commit, and push it to Github, as you mentioned.
Then, I deploy to production with capistrano gem: https://github.com/capistrano/capistrano
This is the command to precompile:
rails assets:precompile

How to precompile assets before running a Rails App on a ECS Fargate environment?

I have a CodePipeline set-up that tracks updates to a private GIT repo. When a change is detected, a CodeBuild task is run to turn the repo into a Docker Image and pushes it to ECR. After that I run a CodeDeploy task to update the ECS cluster by running rake db:migrate and restarting the tasks.
All fine so far. But when I try to access my Rails app, I'm seeing the following error:
ActionView::Template::Error occurred while GET </> was processed by statics#home
Exception
The asset "application.css" is not present in the asset pipeline.
It seems that I need to run rake assets:precompile somewhere. I don't know how to make that work, should I:
Define the precompilation in my Dockerfile?
run the command in my CodeBuild script?
Do something else?
Thanks in advance
In Dockerfile -
RUN bundle exec rake assets:precompile

Is it necessary to run the rake assets:precompile command?

Is there a situation where it is not necessary to run this command? i.e. I don't have static images in the assets directory
Heroku includes asset precompilation as a step in the deploy process. As I see you're using Heroku, that means that you don't have to run rake assets:precompile before committing or pushing. Just make sure that the code that needs to be preprocessed is included in the commit that you're pushing (i.e. the files in /app/assets/(javascripts|stylesheets) etc are up to date).
If you're only deploying to Heroku and working locally in the "development" environment, then you might consider adding /public/assets to your .gitignore and deleting that folder.
See the Heroku docs on this.

How to precompile Ruby on Rails website into Production Mode?

I have locally made this website on my local Linux Debian 6 under path /HOME/ADMIN/WWW/WEBSTUDIO and I need it to be published on my virtual server.
Is "precompiling" the actual word? I know about the command 'rails server' but that is clearly not the same thing as it makes no alterations on the PUBLIC folder.
I guess I have to first transfer my directory structure to server (has Apache2 and ISPCONFIG3 already) and I have done everything so far as how it is described here, but it doesn't tell how to put and precompile your site into Prod Mode.
So what's the procedure? ONLY the basic steps.
When you run rails server or rails s for short your start the rails server
The precompilation isn't for the code, it's for the assets (css,js,fonts, images, etc), rails compiles all css and js each to a single file to reduce the number of http requests needed to load the site.
Also if you are using scss, less or any of those files that need processing it would be done during the precompilation, and if any gems contain assets it would be copied to the public folder.
The precompilation command as mentioned in other answers/comments is
rake assets:precompile
On server terminal, from the root of the project, run:
RAILS_ENV=production rake assets:precompile

Deploying Rails 4.1 in a subdirectory to Heroku?

I have a directory structure like the following:
my-app/
.git/
db/ <-- Database stuff
lib/ <-- Business logic
spec/
web/ <-- Rails
Rails is in the web/ subdirectory of the repository. Heroku doesn't like this by default.
I have a partially working heroku buildpack. It basically calls Dir.chdir to change into the APP_SUBDIR environment variable (if it exists) before running any tasks.
I'd like to deploy to Heroku, but I can't get the asset pipeline task to work. When it comes time to precompile assets, the customized buildpack changes to the web/ directory and attempts to run the task.
However, I get this error, with the following important bit:
Could not detect rake tasks
ensure you can run `$ bundle exec rake -P` against your app with no environment variables present
and using the production group of your Gemfile.
Your Ruby version is 1.9.2, but your Gemfile specified 2.0.0 (Bundler::RubyVersionMismatch)
Which is weird since Heroku clearly says I'm running 2.0 in that very same pastie. In fact, when I run:
$ heroku run 'cd web; bundle exec rake -P'
it work just fine.
TL;DR (Summary)
Heroku thinks my ruby version is 1.9.2 when it's really the 2.0.0 shown in its own build process. Why might this be?
You can push only the subdirectory the Rails app lives in to Heroku:
git subtree push --prefix web heroku master
Where web is the subdirectory.

Resources