Gitpod
使用 Gitpod,只需单击一个按钮或链接即可在浏览器中获得完整的开发环境,并且可以立即编写代码。
Gitpod 是一个开源 Kubernetes 应用程序(GitHub 仓库地址 <https://github.com/gitpod-io/gitpod>),适用于可直接编写代码的开发环境,可为云中的每个任务提供全新的自动化开发环境,非常迅速。此外,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 应用程序为例,通过 URL https://gitpod.io/#targetFile=spring-jpa-hibernate_Makefile,targetMode=spring-jpa-hibernate/https://github.com/pingcap-inc/tidb-example-java 可以创建一个新工作区。
完成后,你将看到如下所示的页面。

页面中的这个场景使用了 TiUP 来搭建一个 TiDB Playground。你可以在终端的左侧查看进度。
一旦 TiDB Playground 准备就绪,另一个 Spring JPA Hibernate 任务将运行。 你可以在终端的右侧查看进度。
完成所有任务后,你可以看到如下所示的页面,并在左侧导航栏的 REMOTE EXPLORER 中找到你的端口 8080 URL(Gitpod 支持基于 URL 的端口转发)。

使用自定义的 Gitpod 配置和 Docker 镜像
自定义 Gitpod 配置
在项目的根目录中,参考示例 .gitpod.yml,创建一个 .gitpod.yml 文件用于配置 Gitpod 工作空间。
1. # This configuration file was automatically generated by Gitpod.
2. # Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
3. # and commit this file to your remote git repository to share the goodness with others.
5. # image:
6. # file: .gitpod.Dockerfile
8. tasks:
9. - name: Open Target File
10. command: |
11. if [ -n "$targetFile" ]; then code ${targetFile//[_]//}; fi
12. - name: TiUP init playground
13. command: |
14. $HOME/.tiup/bin/tiup playground
15. - name: Test Case
16. openMode: split-right
17. init: echo "*** Waiting for TiUP Playground Ready! ***"
18. command: |
19. gp await-port 3930
20. if [ "$targetMode" == "plain-java-jdbc" ]
21. then
22. cd plain-java-jdbc
23. code src/main/resources/dbinit.sql
24. code src/main/java/com/pingcap/JDBCExample.java
25. make mysql
26. elif [ "$targetMode" == "plain-java-hibernate" ]
27. then
28. cd plain-java-hibernate
29. make
30. elif [ "$targetMode" == "spring-jpa-hibernate" ]
31. then
32. cd spring-jpa-hibernate
33. make
34. fi
35. ports:
36. - port: 8080
37. visibility: public
38. - port: 4000
39. visibility: public
40. - port: 2379-36663
41. 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
1. FROM gitpod/workspace-java-17
3. RUN sudo apt install mysql-client -y
4. RUN curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
然后需要更新.gitpod.yml:
1. # This configuration file was automatically generated by Gitpod.
2. # Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
3. # and commit this file to your remote git repository to share the goodness with others.
5. image:
6. # 在这里导入你的 Dockerfile
7. file: .gitpod.Dockerfile
9. tasks:
10. - name: Open Target File
11. command: |
12. if [ -n "$targetFile" ]; then code ${targetFile//[_]//}; fi
13. - name: TiUP init playground
14. command: |
15. $HOME/.tiup/bin/tiup playground
16. - name: Test Case
17. openMode: split-right
18. init: echo "*** Waiting for TiUP Playground Ready! ***"
19. command: |
20. gp await-port 3930
21. if [ "$targetMode" == "plain-java-jdbc" ]
22. then
23. cd plain-java-jdbc
24. code src/main/resources/dbinit.sql
25. code src/main/java/com/pingcap/JDBCExample.java
26. make mysql
27. elif [ "$targetMode" == "plain-java-hibernate" ]
28. then
29. cd plain-java-hibernate
30. make
31. elif [ "$targetMode" == "spring-jpa-hibernate" ]
32. then
33. cd spring-jpa-hibernate
34. make
35. fi
36. ports:
37. - port: 8080
38. visibility: public
39. - port: 4000
40. visibility: public
41. - port: 2379-36663
42. onOpen: ignore
应用更改
完成对 .gitpod.yml 文件配置后,请保证最新的代码已在你对应的 GitHub 代码仓库中可用。
访问 https://gitpod.io/#<YOUR_REPO_URL> 以建立新的 Gitpod 工作区,新工作区会应用最新的代码。
访问 https://gitpod.io/workspaces 以获取所有建立的工作区。
总结
Gitpod 提供了完整的、自动化的、预配置的云原生开发环境。无需本地配置,你可以直接在浏览器中开发、运行、测试代码。

