在Backstage中编写软件模板

在Backstage中编写软件模板

Zave Genshin Impact

软件模板作为 Backstage 的核心功能,掌握软件模板的编写将极大助力于使用本系统。

这里给出一个较为完整的例子,我在其中增加了一些注释作为解释,整体上算是比较容易看懂的。

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
apiVersion: scaffolder.backstage.io/v1beta3
# https://backstage.io/docs/features/software-catalog/descriptor-format#kind-template
kind: Template
metadata:
name: GitLab-template # 文件名称
title: GitLab Template # 展示在前端的文件名称
description: To create a template on GitLab # 展示在前端的文件描述
spec:
owner: user:guest # 此模板的拥有者
type: service # 此模板创建的Backstage项目类型 service、website....等

# 参数填写,这里填写的参数将用于后面基于模板生成项目及其衍生的一系列运维相关东西
parameters:
- title: Fill in some steps # 这里可以描述此小部分填写用于什么
required: # 下面要填写的参数中必填项
- name
- owner
properties: # 下面是各个参数的基本描述和获取方式
name:
title: Name # 参数名称
type: string # 参数类型
description: Unique name of the component
ui:autofocus: true # 是否开启自动聚焦(参数填写到这部分时是否自动聚焦到输入框)
ui:options:
rows: 5 # 最大输入行数
owner:
title: Owner
type: string
description: Owner of the component
ui:field: OwnerPicker # 内置的选择器,用于选择拥有者,选项值将通过下面的声明自动获取
ui:options:
catalogFilter:
# 单选框的值来源于声明地方,此处声明的 catalogFilter
# 表示来源于所有的 catalog 所属的 Group 和 User 的一个集合
kind: [Group, User]

- title: Choose a location
required:
- repoUrl
properties:
repoUrl:
title: Repository Location
type: string
ui:field: RepoUrlPicker # 内置的选择器,固定用于选择目标 Git 服务器链接
ui:options:
allowedHosts:
# 下面声明这个参数的可选项,此处若只有一项,
# 比如下面只有这个 gitlabcode.xxxxxx.com.cn,那么此处将变为不可选状态(不可操作)
- gitlabcode.xxxxxx.com.cn

- title: Fill in the Kubernetes information
required:
- Kubernetes_Env
- Kubernetes_Pod_CoreNum
- Kubernetes_Pod_Size
- Kubernetes_Pod_CopyNum
properties:
Kubernetes_Env:
title: Kubernetes Env
type: string
description: The environment where your project will be deployed
enum: # 下面声明的单项选择器的可选参数
- 'dev'
- 'uat'
- 'prod'
ui:widget: select
# 表示这里是选择器,与上面的选择器不同的是,
# 这个选择器的参数是可以自定义的且没有额外绑定固定的用途

# 此处将通过各个步骤以及相关的 scaffolder 依赖进行项目构建
steps:
# 每一步都依赖一个 scaffolder,这个脚手架可以是官方自带的,也可以是自己编写的
# 这一步是获取项目模板
- id: fetch-base # 每一步的 id,唯一的
name: Fetch Base # 展示在前端 UI 上的文字
action: fetch:template # 用到的 scaffolder id 值
input: # 下面声明传递到这个脚手架中的参数
url: ./content # 参数传递所要给到的初始化文件的相对位置
values:
name: ${{ parameters.name }} # 这里表示之前输入的 name

# 这一步用于在 GitLab 上创建并初始化项目,使用官方自带的 GitLab 脚手架
- id: publish
name: Publish
action: publish:gitlab # 调用了内置的 GitLab 项目创建脚手架
input:
allowedHosts: ['gitlabcode.xxxxxx.com.cn'] # 允许的 host
description: This is ${{ parameters.name }}
repoUrl: ${{ parameters.repoUrl }}

# 这一步用于在 Jenkins 中创建项目对应的 pipeline job,
# 此处的脚手架为自行编写的,前面会带上 acme(规范编程)
- id: create-jenkins
name: Create Jenkins job
action: acme:jenkins:create # 使用自定义的脚手架,用于创建 Jenkins Job
input:
name: ${{ parameters.name }}
repoUrl: ${{ parameters.repoUrl }}

# 这一步将在 Backstage 中注册 catalog
- id: register
name: Register
action: catalog:register
input:
repoContentsUrl: ${{ steps['publish'].output.repoContentsUrl }}
catalogInfoPath: '/catalog-info.yaml'

# Outputs are displayed to the user after a successful execution of the template.
output:
links:
- title: Repository
url: ${{ steps['publish'].output.remoteUrl }}
- title: Open in catalog
icon: catalog
entityRef: ${{ steps['register'].output.entityRef }}

要点小结

  • parameters 描述表单要收集哪些信息,ui:field 与 ui:widget 决定前端用哪种控件渲染
  • steps 是模板的执行流水线,每一步对应一个 Scaffolder Action,官方自带的和自己编写的可以混用
  • ${{ parameters.xxx }} 取表单输入,${{ steps['id'].output.xxx }} 取上一步骤的输出
  • allowedHosts 只写一项时,前端对应选择器会退化为不可操作的固定值
  • 标题: 在Backstage中编写软件模板
  • 作者: Zave
  • 创建于 : 2026-10-07 12:00:00
  • 更新于 : 2026-10-07 12:00:00
  • 链接: https://zavex.cn/post/52438.html
  • 版权声明: 版权所有 © Zave,禁止转载。
评论