软件模板作为 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 kind: Template metadata: name: GitLab-template title: GitLab Template description: To create a template on GitLab spec: owner: user:guest type: service 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: kind: [Group , User ] - title: Choose a location required: - repoUrl properties: repoUrl: title: Repository Location type: string ui:field: RepoUrlPicker ui:options: allowedHosts: - 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 steps: - id: fetch-base name: Fetch Base action: fetch:template input: url: ./content values: name: ${{ parameters.name }} - id: publish name: Publish action: publish:gitlab input: allowedHosts: ['gitlabcode.xxxxxx.com.cn' ] description: This is ${{ parameters.name }} repoUrl: ${{ parameters.repoUrl }} - id: create-jenkins name: Create Jenkins job action: acme:jenkins:create input: name: ${{ parameters.name }} repoUrl: ${{ parameters.repoUrl }} - id: register name: Register action: catalog:register input: repoContentsUrl: ${{ steps['publish'].output.repoContentsUrl }} catalogInfoPath: '/catalog-info.yaml' 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 只写一项时,前端对应选择器会退化为不可操作的固定值