Unable to run ruby script to git pull from github

July 27, 2014 1.8k views


I have a rails server running in my Ubuntu 14.04 instance and I host my code in github.com. For user's pushing to github, I have setup a webhook to my app which listens to any POST call from github and tries to execute a ruby script gitscript.rb when the POST has all correct payloads.

Unfortunaltely the script doesn't do a git pull and I am stumped to run this perfectly. Any help would be appreciated.

Below is my code to listen to webhooks and it runs the script, but the commands in the script don't run, I have all the permission required for the ruby script.

def webhooks
    push = JSON.parse(request.body.read)
    if some_condition_that_should_be_true
      logger.info "Yes go ahead and pull"
      logger.info "Ran the git pull command" if `cd /home/server && git pull >> cat.txt`
      logger.info "Ran the ruby" if `ruby /home/server/gitscript.rb`
      logger.info "Last command"
      logger.info push
    render :json => { :code => 'Webhooks came good' }, :status => 200

And my ruby script is below and yes it has all permissions correct, for test sake I made them 777

#!/bin/env ruby
`cd /home/server && git pull`
1 Answer

Have you tried running the script manually to see if there are any errors?

One thing I notice is that, at least on Ubuntu, the shebang line is wrong.

/bin/env: bad interpreter: No such file or directory

You want to use:

#!/usr/bin/env ruby

Also, make sure you are pulling from GitHub using the https url and not the git@ one. You can check this by running:

git remote show origin

If the fetch url looks like:

Fetch URL: git@github.com:username/project.git

It might be failing as it is will try to connect over SSH. If that's what you want to do, you'll need to create an SSH key on the server without a password so that the script doesn't block waiting for you to enter it. You'll also need to add the public key to your GitHub account.

Have another answer? Share your knowledge.