使用定制脚本定制管道

定制脚本是在管道中添加的定制。 采用者,团队和用户提供脚本来运行定制任务,以确保策略的持续集成和持续部署。

定制脚本控制管道阶段。 您可以使用配置文件 (.pipeline-config.yaml) 来配置阶段的行为,脚本内容以及运行脚本的基本映像。 管道阶段的脚本和配置是从 Git 存储库 (存储库) 装入的,该存储库可以是应用程序 (应用程序) 存储库 (类似于 .travis.ymlJenkinsfile) 或定制存储库。

启动任何自定义脚本时,都会在管道日志开头打印自定义脚本文件的完整 URL,包括文件名和提交哈希值,如下所示:The custom script can be viewed using the following link: 'https://<source repo url>/<organization name>/<repository name>/blob/<commit hash>/.pipeline-config.yaml'. 此定位可提高可跟踪性。

如需了解更多信息,请参阅《 面向初学者的DevSecOps管道定制 》。

.pipeline-config.yaml 中的配置

使用 .pipeline-config.yaml 配置文件来扩展管道行为。

文件位置

对于拉取请求和持续集成管道,请以与 .travis.ymlJenkinsfile 文件相同的方式将 .pipeline-config.yaml 配置文件存储在应用程序存储库中。 对于连续部署管道,请将此文件存储在专用存储库中。

对于所有管道,您可以使用以下管道 UI 参数定制 .pipeline-config.yaml 文件的位置和源:

  • pipeline-config,用于设置配置文件的路径。 默认值为 .pipeline-config.yaml
  • pipeline-config-repo,用于设置要从中拉取配置和脚本的存储库。 缺省值为持续集成应用程序存储库。
  • pipeline-config-branch 以用作配置库中配置的分支。 缺省值为持续集成应用程序存储库分支,以及持续部署中的 master 分支。

配置参数

.pipeline-config.yaml 文件中的配置支持以下参数:

这些设置不是管道参数,它们必须是 .pipeline-config.yaml 文件的一部分。

  • image: 您可以使用管道工作程序可访问的任何 Docker 映像作为任务的基本映像。

  • script: 要在阶段中运行的脚本。 由于此字段用作脚本文件,因此请确保内容与您提供的基本映像中的脚本文件一样。 可以在此配置文件旁边包含其他脚本,并从此入口点引用这些脚本。 例如:

test:
  image: icr.io/continuous-delivery/pipeline/pipeline-base-image:2.69
  script: |
    #!/bin/sh
    scripts/lint.sh
    scripts/unit-test.sh
  • dind: 指定是否对脚本上下文启用 docker-in-docker。 默认设置为 false

  • abort_on_failure: 缺省情况下,当脚本失败时,管道将停止。 将此项设置为 false 会将阶段标记为警告 (淡黄色状态) (随警告一起传递),允许管道继续,并在证据中引用失败的作业。

  • image_pull_policy: 设置在基本映像的映像中提供的 pod ImagePullPolicy 设置。 可能的值与 Kubernetes的有效值相同: AlwaysIfNotPresent。 默认值为 IfNotPresent

  • configmap: 从 PipelineRun 可访问的所提供 configmap 中拉取键/值对。 支持以下值:

    • config-name: 直接 configmap 名称语法。 阶段运行程序尝试访问并安装名为 config-name 的 configmap。
    • $prop-name: 间接 configmap 名称语法。 将从 environment-properties configmap 中查找 configmap 名称,其中包含在管道 UI 上设置的每个环境值。 例如,如果环境属性中有一个 prop my-config 条目,那么 my-config 将用于 $prop
  • secret: 从 PipelineRun 可访问的所提供 configmap 中拉取键/值对。 支持以下值:

    • secret-name: 直接私钥名称语法。 阶段运行程序尝试访问并安装名为 secret-name 的私钥。

    • $prop-name: 间接私钥名称语法。 私钥名称位于包含管道 UI 上设置的每个环境值的环境属性 configmap 中。 例如,如果 environment-properties configmap 包含 my-secret 条目属性,那么 my-secret 将用于 $prop

  • runAfter: 在此属性中指定的阶段完成后运行当前阶段。 使用您在 .pipeline-config.yaml 中使用的阶段名称设置值。 请谨慎使用此属性,并确认管道中存在此属性指定的阶段。 如果阶段不存在,那么管道可能会进入死锁。

  • skip: 如果设置为 true,请在 v10 管道中绕过当前阶段 (如果可能)。 并非所有阶段都可以绕过。 下表列出了在管道运行期间可以跳过的阶段。

流水线运行中可跳过的阶段
管道 阶段
PR 管道 code-unit-testscode-compliance-testscode-pr-finish
CI 管道 code-unit-testscode-static-scancode-compliance-checksbuild-scan-artifactcode-dynamic-scandeploy-acceptance-testsdeploy-releasecode-ci-finish
CD 管道 prod-verify-artifactprod-acceptance-testsprod-finish
CC 管道 cc-static-scancc-dynamic-scancc-compliance-checkscc-scan-artifactcc-finish
应用预览 PR 管道 code-unit-testscode-static-scancode-compliance-checksbuild-scan-artifactdeploy-acceptance-testsapp-preview-pr-finish
Dev-mode CI 管道 code-unit-testscode-static-scandeploy-releasecode-ci-finish
Dev-mode CD 管道 prod-acceptance-testsprod-finish

示例配置

version: '1' # fixed, this value is used to track schema changes
# `setup` runs right after the app repo is cloned
setup:
  # the pipeline will break in case setup fails (default is true)
  abort_on_failure: true
  # any docker image can be used which is accessible by the private worker
  image: icr.io/continuous-delivery/pipeline/pipeline-base-image:2.69
  # you can reference configmaps, each key in the configmap is going to be available as `/config/{key}`
  configmap: my-config
    # `$prop` is the indirect config map syntax, the concrete configmap is looked
    # up from the `environment-properties` configmap
    #
    # Eg. if there's a `prop: my-config` entry in the environment properties,
    # then `my-config` is going to be used for `$prop`
  configmap: $prop
  # the mechanism described works for secrets as well!
  secret: $my-secrets
  # the script is executed inside the checked out app repo
  script: |
    #!/bin/sh
    ...
# `test` runs after `setup`, but before building the docker image
test:
  image: icr.io/continuous-delivery/pipeline/pipeline-base-image:2.69
  script: |
    #!/bin/sh
    ...
# `static-scan` runs after `test`, but before building the docker image
static-scan:
  image: ibmcom/pipeline-base-image:2.12
  script: |
    #!/bin/sh
    ...
# `deploy` runs after building the docker image
deploy:
  image: icr.io/continuous-delivery/pipeline/pipeline-base-image:2.69
  # the script has access to the built docker image, which is available at `/config/image`
  script: |
    #!/bin/sh
    cat /config/image
# `dynamic-scan` runs after `deploy`, but before the acceptance test run
dynamic-scan:
  image: ibmcom/pipeline-base-image:2.12
  script: |
    #!/bin/sh
    ...
# `acceptance-test` runs after `deploy`
acceptance-test:
  image: icr.io/continuous-delivery/pipeline/pipeline-base-image:2.69
  script: |
    #!/bin/sh
    ...