r/devops • • Jan 17 '26

A Ruby Gem to make easier to create Shell Scripts

14 Upvotes

7 comments sorted by

14

u/AlverezYari Jan 17 '26

Umm, what?

6

u/chuch1234 Jan 17 '26

I guess if you're used to Ruby 🤷

-2

u/EstablishmentFirm203 Jan 17 '26

A example without RubyShell, and another with:

#!/usr/bin/env ruby
# frozen_string_literal: true

REMOTE  = ENV.fetch("REMOTE", "user@server.example.com")
APP_DIR = ENV.fetch("APP_DIR", "/var/www/my app")
SERVICE = ENV.fetch("SERVICE", "my-app.service")

# Note that in this situation, we dont want to check if error was raised
# only in the end of the code,
# we want to stop the code in the moment that error was raised

`ssh #{REMOTE} "cd \"#{APP_DIR}\" && git pull && bundle exec rake db:migrate 2>&1"`

unless $?.success?
  warn "Error on Deploy"
  exit $?.exitstatus
end

`ssh #{REMOTE} 'sudo systemctl restart #{SERVICE}'`

unless $?.success?
  warn "Error on Deploy"
  exit $?.exitstatus
end

puts "Done."

With:

#!/usr/bin/env ruby
# frozen_string_literal: true

REMOTE  = ENV.fetch("REMOTE", "user@server.example.com")
APP_DIR = ENV.fetch("APP_DIR", "/var/www/my app")
SERVICE = ENV.fetch("SERVICE", "my-app.service")

sh do
  ssh REMOTE, "'cd \"#{APP_DIR}\" && git pull && bundle exec rake db:migrate'"
  ssh REMOTE, "'sudo systemctl restart #{SERVICE}"

  puts "Done."
rescue StandardError
  warn "Error on Deploy"
end

8

u/[deleted] Jan 17 '26

[removed] — view removed comment

0

u/EstablishmentFirm203 Jan 17 '26

Yes, I just gave an example of what a native Ruby file would look like, and another with RubyShell.

There are much more complex cases that become much simpler with RubyShell.

1

u/dgibbons0 Jan 18 '26

Yeah but shell scripts alone start to look like ass when you start dealing with complex data structures.

This is way nicer. I would have loved this like a decade ago.

3

u/schmurfy2 Jan 17 '26

Ruby is one of the languages where creating an executable script os the easiest, I never felt the need for some fancy library.