Skip to main content

有什么新消息?

那么,我们到了! 🚀

在不同的插件和系统中,Backstage 已经有了多种形式的模板语言。 我们已经有了cookiecutter语法,我们还在模板中使用了handlebars模板中的kind: Template然后,我们想移除对cookiecutter因此,我们引入了nunjucks作为fetch:template动作,该动作基于jinja2语法,因此它们非常相似。 为了减少混淆并统一使用一种模板语言,我们将正式弃用对handlebars模板中的kind: Template具有以下特征的实体apiVersion``scaffolder.backstage.io/v1beta3并转而使用nunjucks而不是

这为我们提供了许多内置的filters(handlebars帮助器),作为模板作者,这将为您提供更多的灵活性,同时还可以在实体和实际的skeleton此外,还消除了两种语言之间的细微差别。

我们还删除了许多与handlebars因为他们现在作为一等公民得到以下两个方面的支持nunjucks或新的scaffolder当使用scaffolder.backstage.io/v1beta3``apiVersion

迁移路径非常简单,我们已经消除了编写handlebars我们来看看有哪些新功能以及如何升级。

将处理器添加到 plugin-catalog-backend 后端

一个重要的更改是将所需的处理器添加到您的packages/backend/src/plugins/catalog.ts

packages/backend/src/plugins/catalog.ts
import { ScaffolderEntitiesProcessor } from '@backstage/plugin-scaffolder-backend';

export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
const builder = await CatalogBuilder.create(env);
builder.addProcessor(new ScaffolderEntitiesProcessor());
const { processingEngine, router } = await builder.build();

// ..
}

backstage.io/v1beta2 -> scaffolder.backstage.io/v1beta3

最重要的变化是,您需要将apiVersion模板中的新模板。

kind: Template
apiVersion: backstage.io/v1beta2
apiVersion: scaffolder.backstage.io/v1beta3

${{ }} 而不是 "{{ }}"

一个很大的可读性问题和造成混乱的原因在于handlebarsyaml中的引号来封装模板字符串。yaml这样它就不会尝试将其解析为一个json这很令人讨厌,因为这意味着所有东西看起来都像字符串。 现在情况不再是这样了,你现在可以删除""并利用书写漂亮的yaml能正常工作的文件。

spec:
steps:
input:
allowedHosts: ['github.com']
description: 'This is {{ parameters.name }}'
description: This is ${{ parameters.name }}
repoUrl: '{{ parameters.repoUrl }}'
repoUrl: ${{ parameters.repoUrl }}

不再有 eqnot 助手

有了表现力更强的apinunjucks您只需使用内置的nunjucksjinja2式运算符。

spec:
steps:
input:
if: '{{ eq parameters.value "backstage" }}'
if: ${{ parameters.value === "backstage" }}

然后为not

spec:
steps:
input:
if: '{{ not parameters.value "backstage" }}'
if: ${{ parameters.value !== "backstage" }}

好多了,对吧?

不再有 json 助手

现在不再需要这个辅助函数了,因为我们已经添加了对复杂值的支持,并支持额外的基元值,而不是所有值都是一个string这意味着,现在您可以通过parameters所有操作都应如期进行,并保持输入模式中声明的类型。

spec:
parameters:
test:
type: number
name: Test Number
address:
type: object
required:
- line1
properties:
line1:
type: string
name: Line 1
line2:
type: string
name: Line 2

steps:
- id: test step
action: run:something
input:
address: '{{ json parameters.address }}'
address: ${{ parameters.address }}
test: '{{ parameters.test }}'
test: ${{ parameters.test }} # this will now make sure that the type of test is a number 🙏

parseRepoUrl现在是一个过滤器

所有呼叫parseRepoUrl现在是jinja2``filter这意味着您需要更新语法。

spec:
steps:
input:
repoUrl: '{{ parseRepoUrl parameters.repoUrl }}'
repoUrl: ${{ parameters.repoUrl | parseRepoUrl }}

现在我们也支持复杂值了,希望这个filter将在未来版本中消失,而RepoUrlPicker将返回一个对象,因此parameters.repoUrl就已经是{ host: string; owner: string; repo: string }🚀

应使用链接而不是命名输出

以前,可以使用命名输出提供指向前台的链接entityRefremoteUrl这些应移至links根据output对象。

output:
remoteUrl: {{ steps['publish'].output.remoteUrl }}
entityRef: {{ steps['register'].output.entityRef }}
links:
- title: Repository
url: ${{ steps['publish'].output.remoteUrl }}
- title: Open in catalog
icon: catalog
entityRef: ${{ steps['register'].output.entityRef }}

注意 "破折号"。

nunjucks 编译器会遇到问题,如果id字段使用破折号字符,因为这些 ID 在作为输出访问时会直接转换为 JavaScript 对象属性。 一种可能的迁移路径是使用camelCase的操作 ID。

steps:
id: my-custom-action
...

id: publish-pull-request
input:
repoUrl: {{ steps.my-custom-action.output.repoUrl }} # Will not recognize 'my-custom-action' as a JS property since it contains dashes!

steps:
id: myCustomAction
...

id: publishPullRequest
input:
repoUrl: ${{ steps.myCustomAction.output.repoUrl }}

另外,还可以保留dash-case语法,并像在 JavaScript 中一样使用括号来访问属性:

input:
repoUrl: ${{ steps['my-custom-action'].output.repoUrl }}

摘要

当然,我们随时可以通过discord如果您遇到问题或某些功能未达到预期效果,您还可以提出问题反馈或错误!