KrauseFx
KrauseFx

Reputation: 11751

Heroku (and similar services): clone, commit and push to Git Repository

I want my Rails application to commit and push git changes after the visitor entered some values on the web application. During development I used a Ruby Gem to commit and push changes, which clones the repository onto the filesystem. Changes are written into those files and are committed and pushed by the Gem afterwards.

Heroku only offers a read only file system.

Is there any way to either

Did I forget any obvious way to achieve changes on my remote git repository?

Upvotes: 0

Views: 122

Answers (2)

KrauseFx
KrauseFx

Reputation: 11751

I decided to use the Amazon cloud to solve this task. With the 'aws-sdk' gem it is easily possible to write file changes into the S3 online space.

I then use my CIS to download the latest files and commit them to the repository, which is a simple ruby script:

require 'git'
require 'aws-sdk'
require 'fileutils'


folder_name = "configs"

begin
    g = Git.open(folder_name)
rescue
    puts "Cloning repo..."
    system("mkdir #{folder_name}")
    g = Git.clone("git@url...", folder_name, :path => ".")
end


g.config("user.name", "GitBot")
g.config("user.email", "...")
g.pull

FileUtils.rm_rf(Dir.glob("#{folder_name}/*"))

s3 = AWS::S3.new
bucket = s3.buckets["bucket name"] if not bucket


bucket.objects.each do |obj|
    if obj.content_length > 0
        # only files
        puts "Writing #{obj.key}"

        File.write("#{folder_name}/#{obj.key}", obj.read)
    else
        puts "Creating folder #{obj.key}"
        FileUtils.mkdir_p "#{folder_name}/#{obj.key}"
    end
end

begin
    g.add(:all => true)
    g.commit("Automatic commit from GitBot")
    g.push
    puts "Successfully pushed updates"
rescue
    puts "Nothing to commit"
end

It's not a perfect solution, but works great for my use case.

Upvotes: 1

Damien MATHIEU
Damien MATHIEU

Reputation: 32629

I did something similar a few years ago, which worked on heroku at the time, and should still work today.

Basically: you can actually use the filesystem, in /tmp.
Therefore, you have to clone in that repository and do your changes there.

However, while this is definitely possible on heroku, you're going to end up with a lot of platform specific code, which might make moving out of heroku difficult.
Using the GitHub file CRUP API as recommended in the comments seems to be a much better solution.

Upvotes: 1

Related Questions