Robert Cutajar
Robert Cutajar

Reputation: 3721

How to run a cron script with gitlab-rails

I have gitlab-rails with

I want to run a gitlab-rails script from Cron. The gitlab-rails runner seems to be broken as it doesn't recognize the file argument and tries to run it as a script instead, which fails. So even if the shebang line suggested in gitlab-rails runner -h worked (which it doesn't on my system because args are not separated), gitlab-rails runner complains that the file path is not a valid script.

I could include the ruby script in a shell script, pipe it to gitlab-rails, but sacrifices .rb file syntax highlighting and intelisense in editors.

I could pipe the .rb script from within a shell script into gitlab-rails. That would solve the previous, but now I have two scripts and shell is not reliable when it comes to referencing related scripts unless I give it the library path explicitly. That seems convoluted.

Someone folks have suggested workarounds. It involves running ruby as usual and then re-executing the script with rails (gitlab-rails in my case). This still fails because of the broken gitlab-rails runner which treats the file argument as a rails command...

Upvotes: 0

Views: 1591

Answers (1)

Robert Cutajar
Robert Cutajar

Reputation: 3721

Update 2024: It seems the bug has been fixed. Try simply:

#!/usr/bin/env -S gitlab-rails runner

# Do your GitLab work here. This is just a harmless trivial example.
Project.find_each do |project|
  puts "#{project.full_path}"
end

I've expanded on the workaround to pipe the script to gitlab-rails on standard input which does work:

#!/usr/bin/env ruby

#
# List GitLab projects
#
# Run this script with sudo

if not defined?(Rails) then
  # Running in plain rubby, invoke rails runner and pipe the script into it, passing args
  exec("/usr/bin/gitlab-rails", "runner", "-", *ARGV, :in=>[File.expand_path(__FILE__)])
end

# Do your GitLab work here. This is just a harmless trivial example.
Project.find_each do |project|
  puts "#{project.full_path}"
end

After making this script executable chown a+x I can put it in a cron job conveniently:

00 * * * * root /path/to/my/script.rb

Upvotes: 2

Related Questions