使用自訂 Script 來自訂管線
自訂 Script 是在管線中新增的自訂作業。 採用者、團隊及使用者提供 Script 來執行自訂作業,以確保持續整合及持續部署策略。
自訂 Script 可控制管線階段。 您可以使用配置檔 (.pipeline-config.yaml) 來配置階段、Script 內容及執行 Script 之基本映像檔的行為。 管線階段的 Script 及配置從可以是應用程式 (應用程式) 儲存庫 (類似於 .travis.yml 或 Jenkinsfile) 或自訂儲存庫的 Git 儲存庫中載入。
啟動任何自訂指令碼時,自訂指令碼檔案的完整 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.yml 或 Jenkinsfile 檔案相同的方式,將 .pipeline-config.yaml 配置檔儲存在應用程式儲存庫中。 若為連續部署管線,請將此檔案儲存在專用儲存庫中。
對於所有管線,您可以使用下列管線使用者介面參數來自訂 .pipeline-config.yaml 檔案的位置及來源:
pipeline-config,以設定配置檔的路徑。 預設值為.pipeline-config.yaml。pipeline-config-repo: 設定儲存庫,以從中取回配置和 Script。 預設值為 Continuous Integration 應用程式儲存庫。pipeline-config-branch,用來作為配置儲存庫中配置的分支。 預設值為 Continuous Integration 應用程式儲存庫分支,以及連續部署中的master分支。
配置參數
.pipeline-config.yaml 檔中的配置支援下列參數:
這些設定不是管線參數,它們必須是 .pipeline-config.yaml 檔案的一部分。
-
image: 您可以使用管線工作者可存取的任何 Docker 映像檔作為作業的基本映像檔。 -
script: 要在階段中執行的 Script。 因為此欄位用作 Script 檔,所以請確保內容的運作方式與您所提供的基本映像檔中的 Script 檔相同。 您可以在此配置檔旁邊併入其他 Script,並從此進入點參照它們。 例如:
test:
image: icr.io/continuous-delivery/pipeline/pipeline-base-image:2.69
script: |
#!/bin/sh
scripts/lint.sh
scripts/unit-test.sh
-
dind: 指定是否針對 Script 環境定義啟用docker-in-docker。 預設設定為false。 -
abort_on_failure: 依預設,當 Script 失敗時,管線會停止。 將此設為false會將階段標示為警告 (琥珀色狀態) (已通過但有警告),容許管線繼續進行,並在證明中參照失敗的工作。 -
image_pull_policy: 設定基本映像檔映像檔中提供的 PodImagePullPolicy設定。 可能的值與 Kubernetes的有效值相同:Always或IfNotPresent。 預設值為IfNotPresent。 -
configmap: 從PipelineRun可存取的所提供 configmap 中取回鍵值組。 下列是支援的值:config-name: 直接 configmap 名稱語法。 階段執行程式會嘗試存取及裝載名為config-name的 configmap。$prop-name: 間接 configmap 名稱語法。 會從環境內容 configmap 查閱 configmap 名稱,其中包含在管線使用者介面上設定的每個環境值。 例如,如果環境內容中有一個 propmy-config項目,則my-config會用於$prop。
-
secret: 從PipelineRun可存取的所提供 configmap 中取回鍵值組。 下列是支援的值:-
secret-name: 直接密鑰名稱語法。 階段執行程式嘗試存取及裝載名為secret-name的密鑰。 -
$prop-name: 間接「密鑰」名稱語法。 「密鑰」名稱位於環境內容 configmap 中,其中包含管線使用者介面上設定的每個環境值。 例如,如果 environment-properties configmap 包含my-secret項目內容,則my-secret會用於$prop。
-
-
runAfter: 在此內容中指定的階段完成之後,執行現行階段。 使用您在.pipeline-config.yaml中使用的暫置名稱來設定值。 請謹慎使用此內容,並確認此內容指定的暫置存在於管線中。 如果階段不存在,管線可能會陷入死鎖。 -
skip: 如果設為true,請在 v10 管線中略過現行階段 (如果可能的話)。 並非所有階段都可以略過。 下表列出在管線執行期間可以跳過的階段。
| 管線 | 階段 |
|---|---|
| PR 管線 | code-unit-tests、code-compliance-tests 及 code-pr-finish |
| CI 管線 | code-unit-tests、code-static-scan、code-compliance-checks、build-scan-artifact、code-dynamic-scan、deploy-acceptance-tests、deploy-release 及 code-ci-finish |
| CD 管線 | prod-verify-artifact、prod-acceptance-tests 及 prod-finish |
| CC 管線 | cc-static-scan、cc-dynamic-scan、cc-compliance-checks、cc-scan-artifact 和 cc-finish |
| 應用程式-預覽 PR 管線 | code-unit-tests、code-static-scan、code-compliance-checks、build-scan-artifact、deploy-acceptance-tests 及 app-preview-pr-finish |
| 開發模式 CI 管線 | code-unit-tests、code-static-scan、deploy-release 和 code-ci-finish |
| Dev-mode CD 管線 | prod-acceptance-tests 及 prod-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
...