One-liner to cd and run bundle (rails)

I’m trying to run a rails console with one line. Can’t figure out how:

Running this:
fly ssh console -C "cd /app/ && bundle ex app/bin/rails console"
I get
exec: "cd": executable file not found in $PATH

Running this directly from the docs:
fly ssh console -C "app/bin/rails console"
I get

Traceback (most recent call last):
        55: from app/bin/rails:4:in `<main>'
        54: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        53: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        52: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/commands.rb:18:in `<main>'
        51: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/command.rb:48:in `invoke'
        50: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/command/base.rb:87:in `perform'
        49: from /app/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
        48: from /app/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
        47: from /app/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
        46: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/commands/console/console_command.rb:101:in `perform'
        45: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/command/actions.rb:15:in `require_application_and_environment!'
        44: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/command/actions.rb:28:in `require_environment!'
        43: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/application.rb:348:in `require_environment!'
        42: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.5.3/lib/zeitwerk/kernel.rb:35:in `require'
        41: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        40: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        39: from /app/config/environment.rb:5:in `<main>'
        38: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/application.rb:372:in `initialize!'
        37: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/initializable.rb:60:in `run_initializers'
        36: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:205:in `tsort_each'
        35: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:226:in `tsort_each'
        34: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:347:in `each_strongly_connected_component'
        33: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:347:in `call'
        32: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:347:in `each'
        31: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:349:in `block in each_strongly_connected_component'
        30: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:415:in `each_strongly_connected_component_from'
        29: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:415:in `call'
        28: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/initializable.rb:50:in `tsort_each_child'
        27: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/initializable.rb:50:in `each'
        26: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
        25: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:431:in `each_strongly_connected_component_from'
        24: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
        23: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        22: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/tsort.rb:228:in `block in tsort_each'
        21: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/initializable.rb:61:in `block in run_initializers'
        20: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/initializable.rb:32:in `run'
        19: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/initializable.rb:32:in `instance_exec'
        18: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/engine.rb:561:in `block in <class:Engine>'
        17: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/engine.rb:561:in `each'
        16: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/engine.rb:562:in `block (2 levels) in <class:Engine>'
        15: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.5.3/lib/zeitwerk/kernel.rb:35:in `require'
        14: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        13: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        12: from /app/config/environments/production.rb:3:in `<main>'
        11: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/railtie.rb:251:in `configure'
        10: from /app/vendor/bundle/ruby/2.7.0/gems/railties-7.0.1/lib/rails/railtie.rb:251:in `instance_eval'
         9: from /app/config/environments/production.rb:80:in `block in <main>'
         8: from /app/config/environments/production.rb:80:in `new'
         7: from /app/vendor/bundle/ruby/2.7.0/gems/activesupport-7.0.1/lib/active_support/logger.rb:81:in `initialize'
         6: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/logger.rb:389:in `initialize'
         5: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/logger.rb:389:in `new'
         4: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/logger/log_device.rb:18:in `initialize'
         3: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/logger/log_device.rb:86:in `set_dev'
         2: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/logger/log_device.rb:95:in `open_logfile'
         1: from /usr/lib/fullstaq-ruby/versions/2.7.1-jemalloc/lib/ruby/2.7.0/logger/log_device.rb:95:in% 

Please note the error log just ends. I can’t see where the actual error is because there appears to be no way of looking at all the logs.

Anyone know what’s up with these two issues?

I think that first line is an issue because we don’t send a command through bash, we just execute it directly.

Try running fly ssh console and then executing cd /app/ and the rails console command directly. That should give you the whole stderr.

Thanks @kurt . I was able to figure this out because of your advice.

@wiznaibus Question: Were you trying to use those commands for CI/CD? If so, what was your solve? I’m currently trying to do the same thing for Github Actions and I’m unable to figure out how to cd into another directory to run commands.

Since that question was posted, the Dockerfile produced by fly launch has new instructions to add cd commands to all of the files in the bin directory, so the following will now work:

fly ssh console -C '/app/bin/rails console'

If you want to run a different command (or set of commands) what you will need in general is to create a script that does a cd followed by the desired command(s), and then run that script.

On github actions you can create a job that does that

example


   - name: migration
        working-directory: backend/migration // this cd into the given path
        run: |
          ls 
          rustc --version
          cargo run

This worked for me without opening an interactive session, or adding any commands to the bin directory:

fly ssh console -C "/app/bin/rails --directory /app [task:name]"
1 Like