将Git回购同步到Google云

2024-04-23 14:31:22 发布

您现在位置:Python中文网/ 问答频道 /正文

所以假设我有一个git存储库https://github.com/jc/ 我有一个googlebucket的位置gs://acme-sales/。在

有没有一种方法可以编写一个python程序来更新github中所做的更改,并在每次运行时将其同步到google云?在

我想我们必须使用gitpython从github link读取文件,但是我如何将文件更新到googlebucket。在


Tags: 文件方法httpsgit程序githubcomgs
3条回答

如果您不需要立即同步(即,以秒为单位),您可以设置一个cron job来定期下拉回购的.zip存档,并将其上载到Google云存储。在

在您的app.yaml中:

runtime: python37

在您的cron.yaml中:

^{pr2}$

在您的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

加上前面的答案。在

您可以选择"Automatically Mirror from GitHub or Bitbucket",这是自描述性的。或者您可以直接将"Automatic Build Trigger"与自定义build steps一起使用来执行某些操作。在

我建议您使用Google Cloud Build。它允许您同步您的repo,并使用gsutil自动更新存储,即each commits上的gs://acme-sales到GitHub存储库:

steps:
- name: gcr.io/cloud-builders/gsutil
  args: ['cp', '/workspace', 'gs://acme-sales']

您可以在GitHub上通过connecting your repositoryhttps://github.com/jc/<repo_name>到Google Cloud Build,使用下面的链接:

https://github.com/apps/google-cloud-build

Automatically build containers or non-container artifacts on commits to your GitHub repository.

相关问题 更多 >