在 Cloud Source Repositories 中创建代码库

在此快速入门中,您将在 Cloud Source Repositories 中创建一个代码库。然后,您提交一个小型 Python 应用的代码,然后将其部署到 App Engine。

Cloud Source Repositories 是托管在 Google Cloud 上的专用 Git 代码库。这些代码库使您可以在为代码提供协作和版本控制的空间中开发和部署应用或服务。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  5. 确保您的 Google Cloud 项目已启用结算功能

  6. 安装 Google Cloud CLI。
  7. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  8. 验证您是否拥有最新版本的 Git
  9. 如果您尚未在 Git 中配置用户凭据,请按照首次设置说明进行操作。
  10. 启用 Cloud Source Repositories API。
  11. 启用 Cloud Source Repositories API

创建代码库

在终端窗口中,使用 gcloud source repos create 命令创建名为 hello-world 的 Google Cloud 代码库:

gcloud source repos create hello-world

克隆代码库

使用 gcloud source repos clone 命令将 Google Cloud 代码库的内容克隆到本地 Git 代码库中:

gcloud source repos clone hello-world

创建“Hello, World!”脚本

创建一个在浏览器窗口中显示 Hello, World! 的 Python 脚本。

  1. 转到您的 hello-world 代码库。

  2. 使用文本编辑器创建一个名为 main.py 的文件,然后粘贴以下代码:

    #!/usr/bin/env python
    
    import webapp2
    
    class MainHandler(webapp2.RequestHandler):
        def get(self):
            self.response.write('Hello, World!')
    
    app = webapp2.WSGIApplication([
        ('/', MainHandler)
    ], debug=True)
    

创建 app.yaml 文件

创建一个 app.yaml 文件,其中包含将代码部署到 App Engine 所需的配置信息。

  1. 转到您的 hello-world 代码库。

  2. 使用文本编辑器创建一个名为 app.yaml 的文件,然后粘贴以下配置信息:

    runtime: python27
    api_version: 1
    threadsafe: yes
    
    handlers:
    - url: .*
      script: main.app
    
    libraries:
    - name: webapp2
      version: "2.5.2"
    

推送到 Cloud Source Repositories

将您刚创建的文件推送到 Cloud Source Repositories。

  1. 在终端窗口中,转到您的 hello-world 目录:

    cd hello-world
    
  2. 添加文件:

    git add .
    
  3. 使用描述此操作历史的注释将文件提交到代码库:

    git commit -m "Add Hello World app to Cloud Source Repositories"
    
  4. 使用 git push 命令,将本地 Git 代码库的内容添加到 Cloud Source Repositories:

    git push origin master
    

    Git 将文件从 master 分支推送到 origin 远程。显示类似以下内容的输出:

    Counting objects: 21, done.
    Delta compression using up to 6 threads.
    Compressing objects: 100% (20/20), done.
    Writing objects: 100% (21/21), 9.76 KiB | 0 bytes/s, done.
    Total 21 (delta 5), reused 0 (delta 0)
    remote: Storing objects: 100% (21/21), done.
    remote: Processing commits: 100% (6/6), done.
    To https://source.developers.google.com/p/example-project-1244/r/repo-name
     * [new branch]      master -> master
    

查看代码库中的文件

  1. 在 Google Cloud 控制台中,打开 Cloud Source Repositories。

    打开 Cloud Source Repositories

  2. 点击您创建的 hello-world 代码库的名称。

  3. 转到您推送到代码库的文件。

    GCP Console 在最近一次提交时在 master 分支中显示文件。

  4. 文件 列表中,点击一个文件以查看其内容。

    屏幕截图显示了 Cloud Source Repositories 中的“文件”列表

您还可以使用 Cloud Shell 查看文件。

清理

放置好 hello-world 代码库后,您可以继续探索 Cloud Source Repositories。如需了解可用的快速入门的完整列表,请参阅 快速入门

如果完成此代码库,您可以按照以下步骤将其删除。

  1. 在 GCP Console 中,为 Cloud Source Repositories 打开 所有代码库 页面。

    打开 Cloud Source Repositories

  2. 将指针放在要删除的代码库上,然后点击 设置

    常规设置 页面打开。

  3. 点击 删除此代码库

    移除代码库 对话框打开。

  4. 输入您要删除的代码库的名称。

  5. 点击删除

后续步骤