jenkins job builder定義job變量缺省值的覆蓋順序

一、jjb中的變量繼承---官網定義

在JJB中可以爲不同級別的變量定義默認值,這樣用戶可以覆蓋job-templates中定義的變量。

每種定義類型的變量優先級如下:

  1. job-group
  2. project
  3. job-template
  4. defaults

從這個列表中我們可以看到,如果我們想在job-templates中覆蓋變量,那麼使用defaults配置是沒用的,因爲它具有最低優先級。

另一方面,job-group具有最高優先級。如果我們在job-group中定義一個變量並打算在項目級別覆蓋它,是無法做到的。因此,請避免在job-group中設置變量,除非我們要對一組job強制執行設置並阻止projects覆蓋它。

二、一些實踐

對於project和job-template中變量定義的細節,我做了一些試驗,結果如下:

1. 

- project:
    name: foo
    tasty: 'project-tasty'
    module:
      - apple:
          tasty: 'module-apple-tasty'
      - orange
    stream:
      - master:
          tasty: 'stream-master-tasty'
    jobs:
      - '{module}-verify-{stream}'

- job-template:
    name: '{module}-verify-{stream}'
    project-type: pipeline
    sandbox: true
    tasty: 'job-template-tasty'
    parameters:
        - string:
            name: TASTY
            default: '{tasty}'
    dsl:
        !include-raw-escape: '../../pipeline/kw/test.groovy'

apple-verify-master和orange-verify-master中的缺省參數均爲:

猜測通過{module}-verify-{stream}這種方式生成job時,後邊{stream}中的變量定義會覆蓋前面{module}中的變量定義。

2. 然後我們去掉stream中的tasty變量定義:

- project:
    name: foo
    tasty: 'project-tasty'
    module:
      - apple:
          tasty: 'module-apple-tasty'
      - orange
    stream:
      - master
    jobs:
      - '{module}-verify-{stream}'

- job-template:
    name: '{module}-verify-{stream}'
    project-type: pipeline
    sandbox: true
    tasty: 'job-template-tasty'
    parameters:
        - string:
            name: TASTY
            default: '{tasty}'
    dsl:
        !include-raw-escape: '../../pipeline/kw/test.groovy'

apple-verify-master中的缺省參數:

現在爲{module}中定義的參數了。orange-verify-master中的缺省參數:

爲projects中定義的tasty變量的值。(project中定義的變量,默認會爲所有的job-template生成這一變量值)

3. 最後我們去掉project中的tasty變量定義:

- project:
    name: foo
    module:
      - apple:
          tasty: 'module-apple-tasty'
      - orange
    stream:
      - master
    jobs:
      - '{module}-verify-{stream}'

- job-template:
    name: '{module}-verify-{stream}'
    project-type: pipeline
    sandbox: true
    tasty: 'job-template-tasty'
    parameters:
        - string:
            name: TASTY
            default: '{tasty}'
    dsl:
        !include-raw-escape: '../../pipeline/kw/test.groovy'

apple-verify-master中的缺省參數:

orange-verify-master中的缺省參數:

此時爲job-template中默認定義的tasty變量值。

簡單幾個試驗,不充分,僅供參考,如有錯誤請指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章