之前的內容,主要針對 GitLab CI 的基本語法做描述,接下來的內容則主要會針對整理 .gitlab-ci.yml 的內容作討論。今天主要要談的就是變數怎麼整理。
在之前 Day05 的章節曾經提過關於變數的使用,也有稍微提到 default 這個可以設定預設關卡內容的應用,供流水線上所有工作使用。那麼它有哪些限制呢?
在整理 .gitlab-ci.yml 的時候,如果有發現絕大部分的工作都有固定的行為時,就可以考慮把這些工作或這些動作放置到 default 預設工作的內容裡頭,而預設工作中,目前支援底下的幾個參數:
舉例來說,如果大部分的流水線使用的 docker image 都是固定的,那麼就可以在 default 內把 image 設定成該 image。而如果其中一個工作並非使用該預設的 image 那麼,也只需要在工作裡頭重新宣告一次 image 就可以將 default 的設定值覆寫。
default:
image: ubuntu:20.04
default_test:
stage: test
script:
- echo 'test default on ubuntu'
centos_test:
stage: test
image: centos:7
script:
- echo 'test on centos'
如上面的範例,其中 default_test 這個工作,因為預設值設定了 image 的關係,期使用的 docker image 就是 ubuntu:20.04,而 centos_test 這個工作,因為重新宣告 image 為 centos:7 因此使用的就會覆寫 default,改變為 centos:7。
另外,如果想使用建立全流水線的變數預設值,也可以直接在 .gitlab-ci.yml 設定,如底下的例子:
variables:
BUILD_OS: 'ubuntu'
BUILD_VER: 'v1.0'
job:
script:
- echo "build ${BUILD_VER} on ${BUILD_OS}"
在 job 中,其變數就會取得 v1.0 及 ubuntu。
在 GitLab 12.9 之後,增加了新的設定方法 inherit 這參數主要就是在解決部分這種想繼承,但又不想全部繼承的情境。
在 inherit 這個參數中,可以針對 default 及 variables 兩種 global 參數作繼承與否的選擇。
在 inherit 參數底下,可以針對 default 及 variables 設定 true 或 false,如:
job:
inherit:
default: true
variables: true
或
job:
inherit:
default: false
variables: false
也可以只選擇想要的參數,其可以使用 YAML 的陣列語法,如:
job:
inherit:
default: [image, before_script]
variables: [variable1, variable2]
或
job:
inherit:
default:
- image
- before_script
variables:
- variable1
- variable2
接下來針對 inherit 的這些參數做一些範例實驗:
default:
image: 'ubuntu:20.04'
before_script:
- echo 'echo by default'
variables:
BUILD_OS: 'ubuntu'
BUILD_VER: 'v1.0'
all_default:
script:
- echo "build ${BUILD_VER} on ${BUILD_OS}"
all_setfalse:
inherit:
default: false
variables: false
script:
- echo "build ${BUILD_VER} on ${BUILD_OS}"
default_setone_variables_setone:
inherit:
default: [image]
variables: [BUILD_OS]
script:
- echo "build ${BUILD_VER} on ${BUILD_OS}"
default_ontset_variables_setfalse:
inherit:
variables: false
script:
- echo "build ${BUILD_VER} on ${BUILD_OS}"
default_settrue_variables_one:
inherit:
default: true
variables: [BUILD_OS]
script:
- echo "build ${BUILD_VER} on ${BUILD_OS}"
在上面這個範例中 default 設定了 image 及 before_script 兩個參數,如工作中沒有使用到繼承,以 image 來說,會取到 runner 本身的預設值,而 before_script 則預設為空。variables 則預設為沒有該變數值。細節執行結果可以查看這邊。
因此在第一個工作執行結果中,因為無論有沒有設定 inherit 都是預計繼承的,因此 all_default 這個工作,引用了 default 及 variables 兩個參數內的設定值。使用了 ubuntu:20.04 這個 image,透過 before_script 印出了預設印出的內容,也順利在 script 中印出參數。
第二個工作 all_setfalse,因為全數設定為 false,因此從執行結果中可以看到使用了 gitlab 官方預設的 image ruby:2.5 沒能印出 before_script 中的內容,且 script 的兩個變數都沒印出。
第三個工作 default_setone_variables_setone 都僅取一個參數,因此從執行結果中可以看到:維持使用 default 中設定的 image ubuntu:20.04,但沒有 before_script 的輸出,script 的輸出也僅印出 BUILD_OS 的數值。
第四個工作 default_no_set_variables_setfalse,執行結果可以看到:引用了 default 中的所有參數,但 variables 的部分在 script 中都沒有印出來,因為其設定了 false。
最後,第五個工作 default_settrue_variables_one,執行結果可以看到:使用了 default 中的所有參數,而 variables 僅取 BUILD_OS 因此 script 中僅印出了 ubuntu 字樣。
從上面的實驗中可以看到,無論是 default 或者是 variables 兩者在工作中預設都是 true 預設繼承的。而 inherit 參數,可以選擇需要的預設值,因此解決了某些狀況下有預設值的困擾。
這是關於參數預設值的使用,接下來將繼續介紹關於內容的重複利用。我是墨嗓(陳佑竹),期待這系列的文章能夠讓人有些幫助。