How can I precompile assets using Travis CI? - ruby-on-rails

In the rails guides they have an example on how to precompile assets with Capistrano. It is as simple as adding load 'deploy/assets' to the Capfile. I simply want to achieve the same effect, precompiling assets, while using Travis CI instead. This is what I've done so far:
script/travis.sh:
run "bundle exec rake assets:precompile"
.travis.yml:
before_install:
- chmod +x script/travis.sh
script: script/travis.sh
language: ruby
rvm:
- 2.2
deploy:
provider: heroku
When this is built on Travis it fails and I get this from the log:
$ script/travis.sh
script/travis.sh: line 1: run: command not found
The command "script/travis.sh" exited with 127.
I also want to add that my shell script knowledge is very limited.

I'm not entirely sure, but I'm leaning towards the run command not being available in the travis shell, as against Capistrano. I'd say you should probably just leave your travis.sh as:
bundle exec rake assets:precompile
And try again. Let me know the results of that

Related

Travis CI Ruby exited with 1

I built a new rails app and when I have configured the .travis.yml file it exits with one all the time.
Here is my travis configuration:
language: ruby
node_js:
- 12
cache:
bundler: true
yarn: true
services:
- redis-server
sudo: required
before_install:
- sudo apt-get update
- sudo apt-get install google-chrome-stable
addons:
postgresql: '9.6'
before_script:
- psql -c 'create database product_hunt_clone_test;' -U postgres
script:
- bundle install
- bundle exec rake db:schema:load
- bundle exec rake db:test:prepare
- SECRET_KEY_BASE=a RAILS_ENV=production bundle exec rake assets:precompile
- bundle exec rake test
- bundle exec rake test:system
and here are the errors travis is giving me:
screenshot from travis console
You have apparently create the initial application using the --skip-sprockets parameter. As such, you have disable the use of the assets pipeline in your application and with that have disable the need (and possibility) to run rake assets:precompile.
If you do need assets pipeline (and thus the rails-sprockets gem), you can re-enable it by adding
gem "sprockets-rails"
to your Gemfile and uncommenting the line
# require "sprockets/railtie"
in your config/application.rb file. You would also need to create the asset files as necessary. Please refer to the documentation for the assets pipeline for details.
If you actually want to skip the assets pipeline, you can just remove the call to the assets:precompile task from your .travis.yml.
Are you correctly installing your gems (with bundle install) before executing your rake commands? Seems like you would need sprockets before compiling the assets.

How to run a bash script to update a Rails app from outside its directory?

I have this script that should be running from the /~ directory:
#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt
As you can see, it pulls new commits and updates the assets and restarts Apache. The problem is when it runs the line rake assets:precompile RAILS_ENV=production, It says:
Could not find proper version of rake (12.1.0) in any of the sources
Run `bundle install` to install missing gems.
Which is strange because I am supposed to be inside the app's folder (/root/apps/monitoring) when this command is executed. What I am doing wrong?
You may need to load rvm in the script (https://rvm.io/workflow/scripting) and may be select proper ruby/gemset.
Also you can consider using wrapper for bundle created with rvm wrapper
Please try
#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
bundle exec rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt
With bundle exec it should work.
I run rake tasks using bash script wrappers. The trick is to use the source command to load in the rvm environment after the task is started
example.sh
#!/bin/bash
# change the directory
cd /home/ubuntu/GSeries
# load rvm ruby
source /home/ubuntu/.rvm/environments/ruby-2.4.2#mygemset
bundle exec rake db:prune_logs RAILS_ENV="production" &>> /home/ubuntu/GSeries/log/prune_logs.log

images are missing in heroku when rails project is deployed using travis

I am trying to deploy a rails project into heroku using travis. Build is successful and tests are also passing. Deployment is also successful. but when I test the heroku app, I find that image files in my app/assets/images directory are not getting displayed in the deployed application.
Here is my .travis.yml file.
language: ruby
rvm:
- 2.2.3
gemfile:
- gemfiles/Gemfile.rails-4.2.5
env:
- DB=sqlite
script:
- RAILS_ENV=test bundle exec rake db:migrate --trace
- bundle exec rake db:test:prepare
- RAILS_ENV=test bundle exec rspec -f d
install:
- bundle install
deploy:
provider: heroku
skip_cleanup: true
app: <my app name>
api_key:
secure: <proper key>
on:
gemfile: gemfiles/Gemfile.rails-4.2.5
strategy: git
run:
- "rake db:migrate"
Can somebody help me understand why image files are missing. I can see them in my github application folder. but they are not getting deployed properly by travis to heroku.
thanks
I see you are usign Rails 4, are you sure to be setting serve_static_assets = true in production.rb?
Refer to Rails 4 images not loading on heroku.
Also, make sure to precompile your assets as you deploy from Travis CI. So add under deploy: run:
deploy:
...
run:
- "rake db:migrate"
- "RAILS_ENV=production bundle exec rake assets:precompile"

Running bash script on server through ssh, unable to call Rails commands through script

I have a script that commits my code to GitHub and I modified it to also run a script on the web server that is supposed to pull the new code, which it does successfully, but then is unable to run the necessary Rails commands like Rake or Bundle. I'm confused because I change to the project directory at the top of the script and git pull runs fine. I even tried putting the Rails command calls inside a subshell with cd /home/rails/ at the top but that still didn't work and neither did specifying the full path to each Rails script. Am I going about this the wrong way or is there a better way to automate these two processes?
Commit script:
git add -A
git commit -m "$1"
git push
ssh root#example.com sh /home/rails/update_script.sh
Update script on server:
service unicorn stop
cd /home/rails/
git pull
rake db:migrate RAILS_ENV=production
rake assets:precompile RAILS_ENV=production
bundle install
service unicorn start
exit
Edit: Oops, forgot the output. Here is the output from the server:
* Stopping Unicorn web server unicorn
...done.
From https://github.com/my_name/example
7e0fee4..17fd564 master -> origin/master
Updating 7e0fee4..17fd564
Fast-forward
fresh.sh | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
/usr/bin/env: ruby: No such file or directory
/usr/bin/env: ruby * Starting Unicorn web server unicorn
: No such file or directory
/usr/bin/env: ruby: No such file or directory
...done.
Maybe you have to add /usr/local/rvm/rubies/ruby-2.1.5/bin to $PATH.
And I think you should run bundle install before running rake tasks.
Try this:
service unicorn stop
cd /home/rails/
git pull
export PATH=$PATH:/usr/local/rvm/rubies/ruby-2.1.5/bin
bundle install
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake assets:precompile
service unicorn start
exit

Ruby on Rails fails on bundle install when using capistrano

I have not much knowledge of Ruby. I have to host a Ruby website. When I make deploy setup and check, is everything ok. But when I cap deploy I get the following error.
*** [deploy:update_code] rolling back
* executing "rm -rf /var/www/lpavisit/releases/20131009211837; true"
servers: ["x.xx.xx.xxx"]
[5.56.61.183] executing command
command finished in 209ms
failed: "sh -c 'cd /var/www/lpavisit/releases/20131009211837 && bundle install --without development test'" on x.xx.xx.xxx
Thanks!!
Setting up a Capistrano-based deploy system is a bit complex. You might want to give Heroku a try until you're more familiar with Ruby. But to continue with Capistrano, first ask it if it has everything it needs to deploy:
$ cap deploy:check
You want to see it say, You appear to have all necessary dependencies installed. Yours probably won't. In that case, you should look for a good capistrano book or online tutorial.

Resources