I'm having some issues with Elastic Beanstalk environment variables which I want to set from credstash.
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: SECRET_KEY_BASE
value: $(credstash --region eu-west-1 -t credstash get test.secret_key_base)
I have specified that credstash should be installed from Python:
packages:
python:
credstash: []
However, when I deploy asset precompilation fails (rake assets:precompile).
The EB health page shows that application deployment failed.
/opt/elasticbeanstalk/support/envvars: line 5: credstash: command not found
...
+ su -s /bin/bash -c 'bundle exec rake assets:precompile' webapp
`/home/webapp` is not a directory.
Bundler will use `/tmp/bundler/home/webapp' as your home directory temporarily.
rake aborted!
ArgumentError: `secret_key_base` for production environment must be a type of String`
secret_key_base is set to ENV['SECRET_KEY_BASE'] in secrets.yml, so it should contain the value from credstash. However, due to the credstash: command not found output, I'm guessing credstash was not installed or is not on the path for some reason.
Does anyone have any idea what could be going on here?
The answer was to set RAILS_SKIP_ASSET_COMPILATION to true. This will skip the /opt/elasticbeanstalk/hooks/appdeploy/pre/11_asset_compilation.sh hook, which runs before credstash is installed.
Instead I included this in a .config file:
container_commands:
01_assets_compile:
command: bundle exec rake assets:precompile RAILS_ENV=production
This will precompile the assets on each container, when credstash will be installed.
Related
I'm using Ruby 2.7 on Elastic Beanstalk, as described here in AWS docs. I'm running Rails 6.1.4.1. Rake is 13.0.6.
Whether or not Beanstalk executes db:migrate is supposed to depend on the value of RAILS_SKIP_MIGRATIONS, which defaults to false. I have made double-sure it is true by including it in my .ebextensions setup, which I see executing:
commands:
01_install_yarn:
command: npm install yarn -g
02_symlink_yarn:
command: ln -s -f "$(npm bin --global)"/yarn /usr/bin/yarn
03_install_dependencies:
command: yum install postgresql-devel
option_settings:
aws:elasticbeanstalk:application:environment:
RAILS_SKIP_MIGRATIONS: false
In fact, I know for sure the value is false, because I can see it on the Beanstalk instance when I connect to it.
If you ssh to the host and run sudo cat /opt/elasticbeanstalk/deployment/env, you see a dump of the environment -- all those config params, as well as injected RDS variables. Very handy for debugging:
$ sudo cat /opt/elasticbeanstalk/deployment/env
PATH=/opt/elasticbeanstalk/.rbenv/shims:/opt/elasticbeanstalk/.rbenv/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
RACK_ENV=production
RAILS_SKIP_MIGRATIONS=false
RAILS_SKIP_ASSET_COMPILATION=false
BUNDLE_WITHOUT=test:development
(etc)
However, /var/log/eb-engine.log consistently skips migrations and shows this:
2021/11/11 23:17:52.261630 [INFO] Running command /bin/su webapp -c bundle exec /opt/elasticbeanstalk/config/private/checkforraketask.rb db:migrate
2021/11/11 23:17:57.083890 [INFO] Found db:migrate task in Rakefile
2021/11/11 23:17:57.083955 [INFO] Skipping db:migrate task (RAILS_SKIP_MIGRATIONS=true).
2021/11/11 23:17:57.083968 [INFO] Executing instruction: configure log streaming
It is showing true instead of false! And I cannot for the life of me figure out why.
I may sidestep this with a container_command to force the task to execute, but I really want to know what's up. Is this a bug in Beanstalk? I can't seem to trace it any further; checkforraketask just looks like this:
require 'rake'
Dir.chdir '/var/app/staging'
Rake.application.init
Rake.application.load_rakefile
tasks = Rake.application.tasks.collect(&:name)
exit 0 if tasks.include? ARGV[0]
exit 1
And Rakefile like this:
require_relative "config/application"
Rails.application.load_tasks
There are no tasks in lib/tasks, so I assume this output comes from that last line? I'm stuck. How do I get Beanstalk to recognize the correct value and run the migration?
I too would love to know the answer to this question.
As a workaround I created the following ebextension to solve the problem:
.ebextensions/03_rake-db-migrate.config
container_commands:
rake_db_migrate:
command: "RAILS_ENV=production bundle exec rake db:migrate"
Are you skipping asset compilation (RAILS_SKIP_ASSET_COMPILATION=true)?
In my case, when is true, the migrations are skipping too.
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.
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
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"
I'm deploying Rails application to server. I can easily accomplish everything if I need only production. But I need staging as well.
I launch my unicorn server with the following command:
bundle exec /home/deployer/apps/myapp/shared/bundle/ruby/2.0.0/bin/unicorn_rails \
-D -c /home/deployer/apps/myapp/shared/config/unicorn.config.rb -E staging
But regardless of my command, the server launches application with production environment.
Is there another place in my application where I should specify environment to be staging?
Thanks!
Set the environment variable RAILS_ENV=staging before you run the command. You can do it on the same line even (in Bash):
$ RAILS_ENV=staging bundle exec unicorn...
You would have to have the environment set up in multiple files such as config/environments/ and config/database.yml.