Skip to main content

发现 GitLab

GitLab 集成有一个特殊的实体提供程序,用于从 GitLab 中发现目录实体。 实体提供程序会抓取 GitLab 实例并注册与配置路径匹配的实体。 这可以替代静态位置或手动添加到目录中。

要使用发现提供程序,您需要与 GitLab 集成设立token然后就可以在目录配置中为每个组添加一个提供程序配置:

catalog:
providers:
gitlab:
yourProviderId:
host: gitlab-host # Identifies one of the hosts set up in the integrations
branch: main # Optional. Used to discover on a specific branch
fallbackBranch: main # Optional. Fallback to be used if there is no default branch configured at the Gitlab repository. It is only used, if `branch` is undefined. Uses `master` as default
skipForkedRepos: false # Optional. If the project is a fork, skip repository
group: example-group # Optional. Group and subgroup (if needed) to look for repositories. If not present the whole instance will be scanned
entityFilename: catalog-info.yaml # Optional. Defaults to `catalog-info.yaml`
projectPattern: '[\s\S]*' # Optional. Filters found projects based on provided patter. Defaults to `[\s\S]*`, which means to not filter anything
schedule: # optional; same options as in TaskScheduleDefinition
# supports cron, ISO duration, "human duration" as used in code
frequency: { minutes: 30 }
# supports ISO duration, "human duration" as used in code
timeout: { minutes: 3 }

由于该提供程序不是默认提供程序之一,因此首先需要安装 gitlab 目录插件:

# From your Backstage root directory
yarn add --cwd packages/backend @backstage/plugin-catalog-backend-module-gitlab

完成后,您还需要将以下段落添加到packages/backend/src/plugins/catalog.ts:

packages/backend/src/plugins/catalog.ts
import { GitlabDiscoveryEntityProvider } from '@backstage/plugin-catalog-backend-module-gitlab';

const builder = await CatalogBuilder.create(env);
/** ... other processors and/or providers ... */
builder.addEntityProvider(
...GitlabDiscoveryEntityProvider.fromConfig(env.config, {
logger: env.logger,
// optional: alternatively, use scheduler with schedule defined in app-config.yaml
schedule: env.scheduler.createScheduledTaskRunner({
frequency: { minutes: 30 },
timeout: { minutes: 3 },
}),
// optional: alternatively, use schedule
scheduler: env.scheduler,
}),
);

替代处理器

catalog:
locations:
- type: gitlab-discovery
target: https://gitlab.com/group/subgroup/blob/main/catalog-info.yaml

作为实体提供者的替代GitlabDiscoveryEntityProvider您仍然可以使用GitLabDiscoveryProcessor.

注意gitlab-discovery类型,因为它不是常规的url处理器。

目标由三部分组成:

  • 基本 URL,本例中为 https://gitlab.com * 组路径,本例中为 group/subgroup。这是可选项:如果省略该路径,处理器将扫描整个 GitLab 实例。 * 每个版本库中查找目录 YAML 文件的路径。通常是 /blob/main/catalog-info.yaml/blob/master/catalog-info.yaml,或者是存储在每个版本库根目录中的目录文件的类似变体。 如果要使用版本库的默认分支,请使用 * 通配符,例如: /blob/*/catalog-info.yaml

最后,您需要在后端目录初始化代码中添加处理器。

packages/backend/src/plugins/catalog.ts
import { GitLabDiscoveryProcessor } from '@backstage/plugin-catalog-backend-module-gitlab';

export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
const builder = await CatalogBuilder.create(env);
builder.addProcessor(
GitLabDiscoveryProcessor.fromConfig(env.config, { logger: env.logger }),
);

// ..
}

如果不想在项目中不存在包含组件定义的文件的情况下创建位置对象,可以设置skipReposWithoutExactFileMatch这样可以减少向 gitlab 提出的 404 状态代码请求的数量。

如果不想在项目分叉的情况下创建位置对象,可以设置skipForkedRepos选择。