所以假设我有一个git存储库https://github.com/jc/
,我有一个谷歌桶gs://acme-sales/
的位置。
有没有办法编写一个python程序来更新github中所做的更改,并在每次运行时将它们同步到google云?
我想我们必须使用gitpython
从github链接读取文件,但我如何不断更新文件到谷歌桶。
如果您不需要立即同步(即,在几秒钟内完成同步),您可以设置cron job以定期下拉您的仓库的.zip
存档,并将其上传到Google云端存储。
在你的app.yaml
:
runtime: python37
在你的cron.yaml
:
cron:
- description: "sync from git repo"
url: /tasks/sync
schedule: every 1 minute
在你的main.py
:
from urllib.request import urlopen
from zipfile import ZipFile
from flask import Flask
from google.cloud import storage
app = Flask(__name__)
client = storage.Client(project='your-project-name')
bucket = client.get_bucket('your-bucket-name')
# Path to the archive of your repository's master branch
repo = 'https://github.com/your-username/your-repo-name/archive/master.zip'
@app.route('/tasks/sync')
def sync():
with ZipFile(BytesIO(urlopen(repo).read())) as zipfile:
for filename in zipfile.namelist():
blob = storage.Blob(filename, bucket)
blob.upload_from_string(zipfile.read(filename))
部署:
$ gcloud app deploy
$ gcloud app deploy cron.yaml
添加到上一个答案[s]。
您可以选择"Automatically Mirror from GitHub or Bitbucket",这是自我描述性的。或者你可以直接使用"Automatic Build Trigger"和自定义build steps来执行某些操作。