×

Build自定义资源 (CR) 中,您可以定义源、构建策略、参数值、输出、保留参数和卷来配置构建。Build资源可在命名空间内使用。

要配置构建,请创建一个Build资源 YAML 文件并将其应用于 OpenShift Container Platform 集群。

构建中可配置的字段

您可以在Build自定义资源 (CR) 中使用以下字段

表 1. Build CR 中的字段
字段 存在性 描述

apiVersion

必需

指定资源的 API 版本,例如shipwright.io/v1beta1

kind

必需

指定资源的类型,例如Build

metadata

必需

表示标识自定义资源定义实例的元数据,例如Build资源的名称。

spec.source

必需

表示源代码的位置,例如 Git 仓库或源代码包镜像。

spec.strategy

必需

表示用于Build资源的策略的名称和类型。

spec.output

必需

表示将推送生成的镜像的位置。

spec.output.pushSecret

必需

表示用于访问容器注册表的现有密钥。

spec.paramValues

可选

表示名称值列表,用于指定在构建策略中定义的参数的值。

spec.timeout

可选

定义自定义超时。默认值为十分钟。您可以在BuildRun资源中覆盖此字段值。

spec.output.annotations

可选

表示键值对列表,您可以使用它来注释输出镜像。

spec.output.labels

可选

表示键值对列表,您可以使用它来标记输出镜像。

spec.env

可选

定义您可以传递给构建容器的其他环境变量。可用的变量取决于构建策略使用的工具。

spec.retention.ttlAfterFailed

可选

指定失败的构建运行可以存在的时间长度。

spec.retention.ttlAfterSucceeded

可选

指定成功的构建运行可以存在的时间长度。

spec.retention.failedLimit

可选

指定可以存在的失败构建运行的数量。

spec.retention.succeededLimit

可选

指定可以存在的成功构建运行的数量。

源定义

您可以通过设置以下字段的值来配置Build自定义资源 (CR) 中构建的源详细信息

  • source.git.url:定义在 Git 仓库中可用的镜像的源位置。

  • source.git.cloneSecret:引用命名空间中包含私有 Git 仓库的 SSH 私钥的密钥。

  • source.git.revision:定义要从源 Git 仓库中选择的特定版本。例如,提交、标签或分支名称。此字段默认为 Git 仓库的默认分支。

  • source.contextDir:指定源代码不在根文件夹中的仓库的上下文路径。

构建控制器不会自动验证您指定用于拉取镜像的 Git 仓库是否存在。如果需要验证,请将build.shipwright.io/verify.repository注释的值设置为true,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: buildah-golang-build
  annotations:
    build.shipwright.io/verify.repository: "true"
spec:
  source:
    git:
      url: https://github.com/shipwright-io/sample-go
    contextDir: docker-build

构建控制器在以下情况下会验证 Git 仓库的存在:

  • 当您使用带有 HTTP 或 HTTPS 协议的端点 URL 时。

  • 当您已定义 SSH 协议(例如git@),但未引用密钥(例如source.git.cloneSecret)时。

以下示例显示了如何使用不同的源输入集配置构建。

示例:使用凭据配置构建

您可以通过指定凭据来配置具有源的构建,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: buildah-build
spec:
  source:
    git:
      url: https://github.com/sclorg/nodejs-ex
      cloneSecret: source-repository-credentials
示例:使用上下文路径配置构建

您可以配置一个具有源的构建,该源在 Git 仓库中指定上下文路径,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: buildah-custom-context-dockerfile
spec:
  source:
    git:
      url: https://github.com/userjohn/npm-simple
    contextDir: docker-build
示例:使用标签配置构建

您可以配置一个具有源的构建,该源为 Git 仓库指定标签v.0.1.0,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: buildah-golang-build
spec:
  source:
    git:
      url: https://github.com/shipwright-io/sample-go
      revision: v0.1.0
示例:使用环境变量配置构建

您还可以配置指定环境变量的构建,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: buildah-golang-build
spec:
  source:
    git:
      url: https://github.com/shipwright-io/sample-go
    contextDir: docker-build
  env:
    - name: <example_var_1>
      value: "<example_value_1>"
    - name: <example_var_2>
      value: "<example_value_2>"

策略定义

您可以在Build CR 中配置构建的策略。以下构建策略可用:

  • buildah

  • source-to-image

要配置构建策略,请在Build CR 中定义spec.strategy.namespec.strategy.kind 字段,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: buildah-build
spec:
  strategy:
    name: buildah
    kind: ClusterBuildStrategy

构建参数值定义

您可以在您的Build CR 中指定构建策略参数的值。通过指定参数值,您可以控制构建策略步骤的工作方式。您也可以覆盖BuildRun 资源中的值。

对于所有参数,您必须直接或使用来自配置映射或密钥的引用键来指定值。

参数在构建策略步骤中的使用限制了配置映射和密钥的使用。只有当参数用于命令、参数或环境变量时,才能使用配置映射和密钥。

Build CR 中使用paramValues 字段时,请避免以下情况

  • 指定与BuildStrategy CR 中定义的spec.parameters 不匹配的spec.paramValues 名称。

  • 指定与 Shipwright 保留参数冲突的spec.paramValues 名称。这些参数包括BUILDER_IMAGECONTEXT_DIR 和任何以shp- 开头的名称。

此外,请确保在Build CR 中定义paramValues 字段之前了解策略的内容。

定义参数值的示例配置

以下示例演示如何在构建策略中定义参数,并使用Build CR 为这些参数赋值。您也可以在您的Build CR 中为array类型的参数赋值。

示例:在ClusterBuildStrategy CR 中定义参数

以下示例显示一个定义了几个参数的ClusterBuildStrategy CR

apiVersion: shipwright.io/v1beta1
kind: ClusterBuildStrategy
metadata:
  name: buildah
spec:
  parameters:
    - name: build-args
      description: "The values for the args in the Dockerfile. Values must be in the format KEY=VALUE."
      type: array
      defaults: []
    # ...
    - name: storage-driver
      description: "The storage driver to use, such as 'overlay' or 'vfs'."
      type: string
      default: "vfs"
# ...
steps:
# ...
示例:在Build CR 中为参数赋值

上面的ClusterBuildStrategy CR 定义了一个storage-driver 参数,您可以在您的Build CR 中指定storage-driver 参数的值,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: <your_build>
  namespace: <your_namespace>
spec:
  paramValues:
  - name: storage-driver
    value: "overlay"
  strategy:
    name: buildah
    kind: ClusterBuildStrategy
  output:
  # ...
示例:创建ConfigMap CR 来集中控制参数

如果您想对多个构建使用storage-driver 参数并集中控制其使用,则可以创建一个ConfigMap CR,如下例所示

apiVersion: v1
kind: ConfigMap
metadata:
  name: buildah-configuration
  namespace: <your_namespace>
data:
  storage-driver: overlay

您可以将创建的ConfigMap CR 用作Build CR 中的参数值,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: <your_build>
  namespace: <your_namespace>
spec:
  paramValues:
  - name: storage-driver
    configMapValue:
      name: buildah-configuration
      key: storage-driver
  strategy:
    name: buildah
    kind: ClusterBuildStrategy
  output:
  # ...
示例:在Build CR 中为array类型的参数赋值

您可以为array类型的参数赋值。如果您使用buildah 策略,您可以定义一个registries-search 参数来搜索特定注册表中的镜像。以下示例显示如何为registries-search 数组参数赋值

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: <your_build>
  namespace: <your_namespace>
spec:
  paramValues:
  - name: storage-driver
    configMapValue:
      name: buildah-configuration
      key: storage-driver
  - name: registries-search
    values:
    - value: registry.redhat.io
  strategy:
    name: buildah
    kind: ClusterBuildStrategy
  output:
  # ...
示例:在Build CR 中引用密钥

您可以为registries-block 数组参数引用一个密钥,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: <your_build>
  namespace: <your_namespace>
spec:
  paramValues:
  - name: storage-driver
    configMapValue:
      name: buildah-configuration
      key: storage-driver
  - name: registries-block
    values:
    - secretValue: (1)
        name: registry-configuration
        key: reg-blocked
  strategy:
    name: buildah
    kind: ClusterBuildStrategy
  output:
  # ...
1 该值引用了一个密钥。

构建器或 Dockerfile 定义

在您的Build CR 中,您可以使用spec.paramValues 字段指定包含用于构建输出镜像的工具的镜像。以下示例在Build CR 中指定了一个Dockerfile 镜像

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: buildah-golang-build
spec:
  source:
    git:
      url: https://github.com/shipwright-io/sample-go
    contextDir: docker-build
  strategy:
    name: buildah
    kind: ClusterBuildStrategy
  paramValues:
  - name: dockerfile
    value: Dockerfile

您也可以在您的Build CR 中将builder 镜像用作source-to-image 构建策略的一部分,如下例所示

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: s2i-nodejs-build
spec:
  source:
    git:
      url: https://github.com/shipwright-io/sample-nodejs
    contextDir: source-build/
  strategy:
    name: source-to-image
    kind: ClusterBuildStrategy
  paramValues:
  - name: builder-image
    value: docker.io/centos/nodejs-10-centos7

输出定义

在您的Build CR 中,您可以指定一个输出位置来推送镜像。当使用外部私有注册表作为您的输出位置时,您必须指定一个密钥来访问镜像。您还可以为输出镜像指定注释和标签。

当您指定注释或标签时,输出镜像会被推送两次。第一次推送来自构建策略,第二次推送会更改镜像配置以添加注释和标签。

以下示例定义了一个公共注册表,镜像将被推送到该注册表

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: s2i-nodejs-build
spec:
  source:
    git:
      url: https://github.com/shipwright-io/sample-nodejs
    contextDir: source-build/
  strategy:
    name: source-to-image
    kind: ClusterBuildStrategy
  paramValues:
  - name: builder-image
    value: docker.io/centos/nodejs-10-centos7
  output:
    image: image-registry.openshift-image-registry.svc:5000/build-examples/nodejs-ex

以下示例定义了一个私有注册表,镜像将被推送到该注册表

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: s2i-nodejs-build
spec:
  source:
    git:
      url: https://github.com/shipwright-io/sample-nodejs
    contextDir: source-build/
  strategy:
    name: source-to-image
    kind: ClusterBuildStrategy
  paramValues:
  - name: builder-image
    value: docker.io/centos/nodejs-10-centos7
  output:
    image: us.icr.io/source-to-image-build/nodejs-ex
    pushSecret: icr-knbuild

以下示例为镜像定义了注释和标签

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: s2i-nodejs-build
spec:
  source:
    git:
      url: https://github.com/shipwright-io/sample-nodejs
    contextDir: source-build/
  strategy:
    name: source-to-image
    kind: ClusterBuildStrategy
  paramValues:
  - name: builder-image
    value: docker.io/centos/nodejs-10-centos7
  output:
    image: us.icr.io/source-to-image-build/nodejs-ex
    pushSecret: icr-knbuild
    annotations:
      "org.opencontainers.image.source": "https://github.com/org/repo"
      "org.opencontainers.image.url": "https://my-company.com/images"
    labels:
      "maintainer": "[email protected]"
      "description": "This is my cool image"

构建保留参数定义

您可以定义保留参数用于以下目的:

  • 指定已完成的构建运行可以存在多长时间

  • 指定对于一个构建可以存在多少个成功的或失败的构建运行

保留参数提供了一种自动清理您的BuildRun 实例或资源的方法。您可以在您的Build CR 中设置以下保留参数的值

  • retention.succeededLimit:定义对于一个构建可以存在多少个成功的构建运行。

  • retention.failedLimit:定义对于一个构建可以存在多少个失败的构建运行。

  • retention.ttlAfterFailed:指定失败的构建运行可以存在的时间长度。

  • retention.ttlAfterSucceeded:指定成功的构建运行可以存在的时间长度。

以下示例显示了在Build CR 中使用保留参数的情况

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: build-retention-ttl
spec:
  source:
    git:
      url: "https://github.com/shipwright-io/sample-go"
    contextDir: docker-build
  strategy:
    kind: ClusterBuildStrategy
    name: buildah
  output:
  # ...
  retention:
    ttlAfterFailed: 30m
    ttlAfterSucceeded: 1h
    failedLimit: 10
    succeededLimit: 20
  # ...

当您更改retention.failedLimitretention.succeededLimit 参数的值时,新的限制会在这些更改应用到您的构建后立即生效。但是,当您更改retention.ttlAfterFailedretention.ttlAfterSucceeded 参数的值时,新的保留持续时间仅对新的构建运行强制执行。旧的构建运行遵循旧的保留持续时间。如果您在BuildRunBuild CR 中都定义了保留持续时间,则BuildRun CR 中定义的保留持续时间优先。

构建卷定义

您可以在您的Build CR 中定义卷。定义的卷会覆盖BuildStrategy 资源中指定的卷。如果没有覆盖卷,则构建运行将失败。

以下示例显示了在Build CR 中使用volumes 字段的情况

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: <build_name>
spec:
  source:
    git:
      url: https://github.com/example/url
  strategy:
    name: buildah
    kind: ClusterBuildStrategy
  paramValues:
  - name: dockerfile
    value: Dockerfile
  output:
    image: registry/namespace/image:latest
  volumes:
    - name: <your_volume_name>
      configMap:
        name: <your_configmap_name>