Gitpod

使用 Gitpod,只需单击一个按钮或链接即可在浏览器中获得完整的开发环境,并且可以立即编写代码。

Gitpod 是一个开源 Kubernetes 应用程序(GitHub 仓库地址 https://github.com/gitpod-io/gitpod),适用于可直接编写代码的开发环境,可为云中的每个任务提供全新的自动化开发环境,非常迅速。此外,Gitpod 能够将你的开发环境描述为代码,并直接从你的浏览器或桌面 IDE 启动即时、远程和基于云的开发环境。

快速开始

  1. Fork 出 TiDB 应用开发的示例代码仓库 pingcap-inc/tidb-example-java

  2. 通过浏览器的地址栏,在示例代码仓库的 URL 前加上 https://gitpod.io/# 来启动你的 gitpod 工作区。

    • 例如,https://gitpod.io/#https://github.com/pingcap-inc/tidb-example-java

    • 支持在 URL 中配置环境变量。例如,https://gitpod.io/#targetFile=spring-jpa-hibernate_Makefile,targetMode=spring-jpa-hibernate/https://github.com/pingcap-inc/tidb-example-java

  3. 使用列出的提供商之一登录并启动工作区,例如,Github

使用默认的 Gitpod 配置和环境

完成快速开始 的步骤之后,Gitpod 会需要一段时间来设置你的工作区。

Spring Boot Web 应用程序为例,通过 URL https://gitpod.io/#targetFile=spring-jpa-hibernate_Makefile,targetMode=spring-jpa-hibernate/https://github.com/pingcap-inc/tidb-example-java 可以创建一个新工作区。

完成后,你将看到如下所示的页面。

playground gitpod workspace init

页面中的这个场景使用了 TiUP 来搭建一个 TiDB Playground。你可以在终端的左侧查看进度。

一旦 TiDB Playground 准备就绪,另一个 Spring JPA Hibernate 任务将运行。 你可以在终端的右侧查看进度。

完成所有任务后,你可以看到如下所示的页面,并在左侧导航栏的 REMOTE EXPLORER 中找到你的端口 8080 URL(Gitpod 支持基于 URL 的端口转发)。

playground gitpod workspace ready

你可以按照该指南测试 API。注意请将 URL http://localhost:8080 替换为你在 REMOTE EXPLORER 中找到的那个。

使用自定义的 Gitpod 配置和 Docker 镜像

自定义 Gitpod 配置

在项目的根目录中,参考示例 .gitpod.yml,创建一个 .gitpod.yml 文件用于配置 Gitpod 工作空间。

# This configuration file was automatically generated by Gitpod. # Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file) # and commit this file to your remote git repository to share the goodness with others. # image: # file: .gitpod.Dockerfile tasks: - name: Open Target File command: | if [ -n "$targetFile" ]; then code ${targetFile//[_]//}; fi - name: TiUP init playground command: | $HOME/.tiup/bin/tiup playground - name: Test Case openMode: split-right init: echo "*** Waiting for TiUP Playground Ready! ***" command: | gp await-port 3930 if [ "$targetMode" == "plain-java-jdbc" ] then cd plain-java-jdbc code src/main/resources/dbinit.sql code src/main/java/com/pingcap/JDBCExample.java make mysql elif [ "$targetMode" == "plain-java-hibernate" ] then cd plain-java-hibernate make elif [ "$targetMode" == "spring-jpa-hibernate" ] then cd spring-jpa-hibernate make fi ports: - port: 8080 visibility: public - port: 4000 visibility: public - port: 2379-36663 onOpen: ignore

自定义 Gitpod Docker 镜像

默认情况下,Gitpod 使用名为 Workspace-Full 的标准 Docker 镜像作为工作空间的基础。 基于此默认镜像启动的工作区预装了 Docker、Go、Java、Node.js、C/C++、Python、Ruby、Rust、PHP 以及 Homebrew、Tailscale、Nginx 等工具。

你可以提供公共 Docker 镜像或 Dockerfile。 并为你的项目安装所需的任何依赖项。

这是一个 Dockerfile 示例:示例 .gitpod.Dockerfile

FROM gitpod/workspace-java-17 RUN sudo apt install mysql-client -y RUN curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh

然后需要更新.gitpod.yml

# This configuration file was automatically generated by Gitpod. # Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file) # and commit this file to your remote git repository to share the goodness with others. image: # 在这里导入你的 Dockerfile file: .gitpod.Dockerfile tasks: - name: Open Target File command: | if [ -n "$targetFile" ]; then code ${targetFile//[_]//}; fi - name: TiUP init playground command: | $HOME/.tiup/bin/tiup playground - name: Test Case openMode: split-right init: echo "*** Waiting for TiUP Playground Ready! ***" command: | gp await-port 3930 if [ "$targetMode" == "plain-java-jdbc" ] then cd plain-java-jdbc code src/main/resources/dbinit.sql code src/main/java/com/pingcap/JDBCExample.java make mysql elif [ "$targetMode" == "plain-java-hibernate" ] then cd plain-java-hibernate make elif [ "$targetMode" == "spring-jpa-hibernate" ] then cd spring-jpa-hibernate make fi ports: - port: 8080 visibility: public - port: 4000 visibility: public - port: 2379-36663 onOpen: ignore

应用更改

完成对 .gitpod.yml 文件配置后,请保证最新的代码已在你对应的 GitHub 代码仓库中可用。

访问 https://gitpod.io/#<YOUR_REPO_URL> 以建立新的 Gitpod 工作区,新工作区会应用最新的代码。

访问 https://gitpod.io/workspaces 以获取所有建立的工作区。

总结

Gitpod 提供了完整的、自动化的、预配置的云原生开发环境。无需本地配置,你可以直接在浏览器中开发、运行、测试代码。

playground gitpod summary

下载 PDF
产品
TiDB
TiDB Cloud
© 2024 PingCAP. All Rights Reserved.
Privacy Policy.