1- # 应用
1+ # 应用(Apps)
22
3- ## 概念
3+ ## 1 概念
44
5- 应用在 devstream 中表示对一个服务的生命周期配置,包括对服务的项目脚手架,CI 流程以及 CD 流程的配置,在 devstream 中使用应用可以使用少数几行配置就构建出服务的整条 CI/CD 流水线配置。
5+ 应用在 DevStream 中表示对一个服务的生命周期配置,包括对服务的项目脚手架,CI 流程以及 CD 流程的配置,在 devstream 中使用应用可以使用少数几行配置就构建出服务的整条 CI/CD 流水线配置。
66
7- ## 应用配置
7+ ### 1.1 应用(Apps)
88
9- 应用的示例配置如下:
9+ 有时候,你需要为一个应用/微服务定义多个 工具。例如,对于一个 web 应用程序,你可能需要指定以下工具:
10+
11+ - 仓库脚手架
12+ - 持续集成
13+ - 持续部署
14+
15+ 如果你有多个应用需要管理,你需要在配置中创建多个 工具,这可能会很繁琐,而且配置文件难以阅读。
16+
17+ 为了更容易地管理多个应用/微服务,DevStream 提供了另一层抽象,称为 应用。你可以在一个应用中定义所有内容(例如,上面提到的仓库脚手架、持续集成、持续部署等),只需要几行配置,就可以使配置文件更容易阅读和管理。
18+
19+ 在底层,DevStream 仍然会将你的 应用 配置转换为 工具 的定义,但你不需要关心它。
20+
21+ ## 1.2 流水线模板(pipelineTemplates)
22+
23+ 一个 DevStream 应用 可以引用一个或多个 流水线模板,这些模板主要是 CI/CD 定义,这样 应用 的定义可以更短,以在多个微服务之间共享公共的 CI/CD 流水线。
24+
25+ ## 2 配置方式
26+
27+ ### 2.1 应用
28+
29+ 应用在配置中由 ` apps ` 来定义,它是一个数组,每个元素的字段如下:
30+
31+ - name: 应用的名称,必须是唯一的
32+ - spec: 配置应用特定的信息
33+ - repo: 代码仓库信息
34+ - repoTemplate: 可选。结构和 repo 一致。若非空,DevStream 将使用项目脚手架来创建仓库。
35+ - ci: 可选,一个 CI 流水线的数组,每个元素包含以下字段:
36+ - type: 类型。值为 ` template ` 或某个插件的名称。
37+ - templateName: 可选。当 type 为 ` template ` 时,指定使用的模板名称。
38+ - vars: 可选。传入模板的变量,仅当 type 为 ` template ` 时有效。
39+ - options: 可选。
40+ - 当 type 是某个插件名时,就是该插件的 Options
41+ - 当 type 为 ` template ` 时,覆盖模板中的 Options。你需要写清想要覆盖的选项的完整路径,例如 ` options.docker.registry.type ` 。
42+ - cd: 与 ` ci ` 类似,是 CD 流水线列表。dtm 会先执行 ci 流水线,再执行 cd 流水线。
43+
44+ ### 2.2 流水线模板
45+
46+ 流水线模板在配置中由 ` pipelineTemplates ` 来定义,它是一个数组,每个元素的字段如下:
47+
48+ - name: 流水线模板的名称,必须是唯一的
49+ - type: 对应插件的名称
50+ - options: 插件的 Options (可能和原始的插件 Options 不同)
51+
52+ ### 2.3 局部变量
53+
54+ DevStream 已经有了全局变量,所有的 工具 和 应用 都可以直接引用。
55+
56+ 但有时,我们想多次引用某个 DevOps 工具,但他们只有一些细微的差别。例如,除了项目名称不同,其他的都相同。
57+
58+ 在这种情况下,我们可以定义一个流水线模板,在其中定义一个局部变量,然后在 应用 引用该流水线模板时,传入不同的值。
59+
60+ ``` yaml hl_lines="13 15 23 30" title="流水线模板的使用与局部变量"
61+ apps :
62+ - name : my-app
63+ spec :
64+ language : java
65+ framework : springboot
66+ repo :
67+ url : https://github.com/testUser/testApp.git
68+ branch : main
69+ ci :
70+ - type : github-actions # 不定义 pipelineTemplates,直接使用插件
71+ cd :
72+ - type : template # 使用流水线模板
73+ templateName : my-cd-template # 对应 pipelineTemplates 中的 name
74+ vars :
75+ appName : my-app # 传入模板的变量
76+
77+ pipelineTemplates :
78+ cd :
79+ - name : my-cd-template
80+ type : argocdapp
81+ options :
82+ app :
83+ name : [[ appName ]] # 定义一个局部变量,在引用使用模板时传入
84+ namespace : argocd # argocd 的命名空间
85+ destination :
86+ server : https://kubernetes.default.svc # 部署的 kubernetes 服务地址
87+ namespace : default # 应用要部署的命名空间
88+ source :
89+ valuefile : values.yaml # 项目中的 helm 变量文件名
90+ path : charts/[[ appName ]] # 项目中的 helm 配置路径
91+ ` ` `
92+
93+ ## 3 完整配置示例
94+
95+ 应用 的真实示例配置如下:
1096
1197` ` ` yaml
1298apps :
13- - name : testApp # 应用名称
99+ - name : testApp # 应用名称
14100 spec : # 该配置项用于配置应用特定的信息
15101 language : java # 应用所使用的编程语言
16102 framework : springboot # 应用所使用的编程框架
17103 repo : # 该配置项用于应用的代码仓库信息
18104 url : https://github.com/testUser/testApp.git
19105 branch : main
20- repoTemplate : # 该配置用于创建应用的脚手架
106+ repoTemplate : # 可选,用于创建应用的脚手架。不为空时,会使用该脚手架创建应用的代码仓库
21107 url : https://github.com/devstream-io/dtm-repo-scaffolding-java-springboot.git
22108 vars :
23109 imageRepoOwner : repoOwner # 用于渲染脚手架模版的变量
@@ -39,45 +125,49 @@ apps:
39125 - type : github-actions
40126 options :
41127 imageRepo :
42- owner : repoOwner
128+ owner : repoOwner # 覆盖 插件/模板 原有的 Options,YAML 路径要写完全
43129 cd : # 配置应用的 cd,如果为使用 argocdapp 运行应用的 cd 流程
44130 - type : argocdapp
45131` ` `
46132
47- 使用该配置就会在 ` gitlab` 仓库中创建两个应用,项目的脚手架均为 devstream 官方提供的 [springboot ](https://github.com/devstream-io/dtm-repo-scaffolding-java-springboot.git) 项目。应用 `testApp` 会在每次代码提交后使用 `github-actions` 运行测试。应用 `testApp2` 会在每次代码提交后使用 `github-actions` 运行测试并构建镜像推送到 `repoOwner` 的镜像仓库中,最后使用 argocd 将应用部署到集群中。
133+ 使用该配置就会在 ` gitlab` 仓库中创建两个应用,项目的脚手架均为 DevStream 官方提供的 [SpringBoot ](https://github.com/devstream-io/dtm-repo-scaffolding-java-springboot.git) 项目。应用 `testApp` 会在每次代码提交后使用 `github-actions` 运行测试。应用 `testApp2` 会在每次代码提交后使用 `github-actions` 运行测试并构建镜像推送到 `repoOwner` 的镜像仓库中,最后使用 Argo CD 将应用部署到集群中。
48134
49135# ## repo/repoTemplate 配置
50136
51- 应用配置中的 `repo` 和 `repoTemplate` 均表示一个代码仓库,支持以下两种配置代码仓库的方式 :
137+ 应用配置中的 `repo` 和 `repoTemplate` 均表示一个代码仓库,支持通过 url 或 详细字段来配置 :
52138
53- # ### 使用 url 来配置代码仓库:
139+ !!! note "两种配置代码仓库的方式"
54140
55- ` ` ` yaml
56- repo:
57- url: [email protected] :root/myapps.git # url 表示仓库的地址,支持 git 地址和 http 地址 58- apiURL: https://gitlab.example.com # 非必填,如果使用 gitlab 而且 url 使用的是 git 地址,则需要配置该字段用于表示 gitlab 的 api 请求地址
59- branch: "" #非必填,github 默认为 main 分支,gitlab 默认为 master 分支
60- ` ` `
61-
62- 该配置表示使用的仓库为 `gitlab`, 要使用 `[email protected] :root/myapps.git` 来克隆代码,devstream 会使用使用 `https://gitlab.example.com` 和 gitlab 进行交互,仓库的主分支为 `master` 分支。 63-
64- # ### 使用仓库的详细字段来配置仓库:
65-
66- ` ` ` yaml
67- repo:
68- org: "" # 非必填,仓库的拥有组织名称,如果使用 github 的组织则需要填写此字段
69- owner:"test_user" # 如果仓库是非组织的,则需要填写该字段表示仓库拥有者
70- name: "" # 非必填,默认为应用名
71- baseURL: https://gitlab.example.com # 非必填,如果使用 gitlab 则需要填写该字段表示 gitlab 的域名
72- branch: master #非必填,github 默认为 main 分支,gitlab 默认为 master 分支
73- type: gitlab #必填,表示该仓库的类型,目前支持 gitlab/github
74- ` ` `
75-
76- 该配置表示代码仓库使用的是 `gitlab` 且其地址为 `https://gitlab.example.com`,仓库名称为应用名,仓库的所有者为 `test_user`,主分支为 `master` 分支。
141+ === "使用 url 来配置"
142+
143+ ` ` ` yaml title=""
144+ repo:
145+ url: [email protected] :root/myapps.git # url 表示仓库的地址,支持 git 地址和 http 地址 146+ apiURL: https://gitlab.example.com # 非必填,如果使用 gitlab 而且 url 使用的是 git 地址,则需要配置该字段用于表示 gitlab 的 api 请求地址
147+ branch: "" # 非必填,github 默认为 main 分支,gitlab 默认为 master 分支
148+ ` ` `
149+
150+ 该配置表示使用的仓库为 `gitlab`, 要使用 `[email protected] :root/myapps.git` 来克隆代码,devstream 会使用使用 `https://gitlab.example.com` 和 gitlab 进行交互,仓库的主分支为 `master` 分支。 151+
152+ === "使用仓库的详细字段来配置"
153+
154+ ` ` ` yaml title=""
155+ repo:
156+ org: "" # 非必填,仓库的拥有组织名称,如果使用 github 的组织则需要填写此字段
157+ owner:"test_user" # 如果仓库是非组织的,则需要填写该字段表示仓库拥有者
158+ name: "" # 非必填,默认为应用名
159+ baseURL: https://gitlab.example.com # 非必填,如果使用 gitlab 则需要填写该字段表示 gitlab 的域名
160+ branch: master #非必填,github 默认为 main 分支,gitlab 默认为 master 分支
161+ type: gitlab #必填,表示该仓库的类型,目前支持 gitlab/github
162+ ` ` `
163+
164+ 该配置表示代码仓库使用的是 `gitlab` 且其地址为 `https://gitlab.example.com`,仓库名称为应用名,仓库的所有者为 `test_user`,主分支为 `master` 分支。
77165
78166# ## ci 配置
79167
80- 应用配置中的 `ci` 目前支持 `github-actions`/`gitlab-ci`/`jenkins-pipeline`/`template` 4 种类型,前 3 种类型分别对应了 `github` 中的 actions 流水线,`gitlab` 中 ci 流水线和 `jenkins` 中的 pipeline,它们的具体配置如下:
168+ 应用配置中的 `ci` 目前支持 `github-actions`/`gitlab-ci`/`jenkins-pipeline`/`template` 4 种类型。
169+
170+ 其中 `template` 类型表示使用 流水线模板 来运行流水线,前 3 种类型分别对应了 `github` 中的 actions 流水线,`gitlab` 中 ci 流水线和 `jenkins` 中的 pipeline,它们的具体配置如下:
81171
82172` ` ` yaml
83173 ci:
@@ -132,7 +222,7 @@ apps:
132222 - type: template # 表示该 ci 流程使用模版
133223 templateName: ci-pipeline # ci 使用的模版名称
134224 vars:
135- dingdingAccessToken: tokenForProject2 #用于渲染 ci 模版的变量
225+ dingdingAccessToken: tokenForProject2 # 设置传入模版 "ci-pipeline" 的变量
136226 dingdingSecretValue: secretValProject2
137227
138228pipelineTemplates: # 即配置的 ci/cd 模版
@@ -149,7 +239,7 @@ pipelineTemplates: # 即配置的 ci/cd 模版
149239 name: dingTalk
150240 webhook: https://oapi.dingtalk.com/robot/send?access_token=[[ dingdingAccessToken ]] # 用于被 app 渲染的模版,这样就可以实现不同应用使用同一个模版发送通知到不同的钉钉群
151241 securityType: SECRET
152- securityValue: [[ dingdingSecretValue ]]
242+ securityValue: [[ dingdingSecretValue ]] # 局部变量,应用 在引用此模板时通过 vars 来传入
153243 sonarqube:
154244 url: http://sonar.example.com
155245 token: sonar_token
0 commit comments