Gitpod
使用 Gitpod,你只需点击一个按钮或链接,即可在浏览器中获得完整的开发环境,并立即开始编写代码。
Gitpod 是一个开源的 Kubernetes 应用(GitHub 仓库地址:https://github.com/gitpod-io/gitpod),用于直接到代码的开发环境,可以在云端为每个任务快速启动全新、自动化的开发环境,仅需几秒钟。它允许你将开发环境描述为代码,并可直接从浏览器或桌面 IDE 启动即时、远程和基于云的开发环境。
快速开始
Fork TiDB 应用开发示例代码仓库 pingcap-inc/tidb-example-java。
在浏览器地址栏中,将示例代码仓库的 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。
使用列出的任一提供商登录并启动工作区。例如,
Github。
使用默认的 Gitpod 配置和环境
完成 快速开始 步骤后,Gitpod 会花费一段时间来设置你的工作区。
以 Spring Boot Web 应用为例。你可以通过 https://gitpod.io/#targetFile=spring-jpa-hibernate_Makefile,targetMode=spring-jpa-hibernate/https://github.com/pingcap-inc/tidb-example-java 这个 URL 创建一个新的工作区。
之后,你会看到如下页面:

该页面中的场景使用 TiUP 构建 TiDB Playground。你可以在终端区域左侧查看进度。
当 TiDB Playground 就绪后,另一个 Spring JPA Hibernate 任务会运行。你可以在终端区域右侧查看进度。
所有任务完成后,你会看到如下页面。在该页面中,检查左侧导航栏的 REMOTE EXPLORER 区域(Gitpod 支持基于 URL 的端口转发),找到你的 8080 端口的 URL。

使用自定义 Gitpod 配置和 Docker 镜像
自定义 Gitpod 配置
参考 example.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(另见 Example .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:
# Import your Dockerfile here.
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 提供了一个完整、自动化且预配置的云原生开发环境。你可以在浏览器中直接开发、运行和测试代码,无需任何本地配置。
