简体   繁体   English

Heroku(和类似的服务):克隆,提交和推送到Git Repository

[英]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. 我希望我的Rails应用程序在访问者在Web应用程序上输入一些值后提交并推送git更改。 During development I used a Ruby Gem to commit and push changes, which clones the repository onto the filesystem. 在开发过程中,我使用Ruby Gem来提交和推送更改,这会将存储库克隆到文件系统上。 Changes are written into those files and are committed and pushed by the Gem afterwards. 更改将写入这些文件,然后由Gem提交和推送。

Heroku only offers a read only file system. Heroku仅提供只读文件系统。

Is there any way to either 有没有办法

  • commit and push files without accessing the file system? 提交和推送文件而不访问文件系统?
  • clone the repository into a temporary folder and commit there? 将存储库克隆到临时文件夹并提交到那里?
  • use an other host similar to Heroku, that allows writing onto the filesystem? 使用类似于Heroku的其他主机,允许写入文件系统?

Did I forget any obvious way to achieve changes on my remote git repository? 我是否忘记了在远程git存储库上实现更改的任何明显方法?

I did something similar a few years ago, which worked on heroku at the time, and should still work today. 几年前我做了类似事情 ,当时在heroku工作,今天仍然应该工作。

Basically: you can actually use the filesystem, in /tmp . 基本上:您实际上可以在/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. 然而,虽然这在heroku上肯定是可能的,但是你最终会得到许多特定于平台的代码,这可能会使heroku难以移出。
Using the GitHub file CRUP API as recommended in the comments seems to be a much better solution. 使用评论中建议的GitHub文件CRUP API似乎是一个更好的解决方案。

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. 使用'aws-sdk'gem,可以轻松地将文件更改写入S3在线空间。

I then use my CIS to download the latest files and commit them to the repository, which is a simple ruby script: 然后我使用我的CIS下载最新文件并将它们提交到存储库,这是一个简单的ruby脚本:

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. 它不是一个完美的解决方案,但对我的用例非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM