[英]Use variables in Azure DevOps Pipeline templates
我們收集了 Azure 個 DevOps 管道模板,我們可以在多個存儲庫中重復使用這些模板。 因此,我們希望有一個包含所有模板變量的文件。
回購結構看起來像這樣
template repo
├── template-1.yml
├── template-2.yml
└── variables.yml
project repo
├── ...
└── azure-pipelines.yml
variables.yml
看起來像這樣
...
variables:
foo: bar
在template-1.yml
中,我們正在導入variables.yml
,如此處所述
variables:
- template: variables.yml
在azure-pipelines.yml
中,我們使用這樣的模板
resources:
repositories:
- repository: build-scripts
type: git
name: project-name/build-scripts
steps:
...
- template: template-1.yml@build-scripts
當我們現在嘗試運行管道時,我們收到以下錯誤消息:
template-1.yml@build-scripts (Line: 10, Col: 1): Unexpected value 'variables'
問題是因為您在步驟范圍內使用了變量模板。 variables
根本不存在於那個級別。 這應該適合你:
resources:
repositories:
- repository: build-scripts
type: git
name: project-name/build-scripts
variables:
- template: template-1.yml@build-scripts
steps:
...
這可以在任何可以使用變量的地方使用。 因此,例如,您可以以這種方式使用它:
jobs:
- job: myJob
timeoutInMinutes: 10
variables:
- template: template-1.yml # Template reference
pool:
vmImage: 'ubuntu-16.04'
steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.
如果你的模板文件只有variables
,你可以參考 Krzysztof Madej 的回答。
如果您的模板文件具有如下所示的variables
和steps
,則它只能由extends 使用。
# File: template-1.yml
variables: ...
steps: ...
或者你可以在一個階段中編寫它們,如下所示。
# File: template-1.yml
stages:
- stage: {stage}
variables: ...
jobs:
- job: {job}
steps: ...
然后將其作為單獨的階段插入。
# azure-pipelines.yml
stages:
- stage: ...
- template: template-1.yml
回復我認為原來的解釋中遺漏了一些東西:
第一:如果您在模板中引用變量,那么您必須確保在調用它時擴展該模板。 調用定義未擴展變量的模板將導致管道執行失敗。
template-1.yml@build-scripts (Line: 10, Col: 1): Unexpected value 'variables'
第二:在多個模板中引用變量時不要使用:
${{ variables.foo }} #even though this is seen in templates examples.
而是使用普通的變量語法
$(foo) #this works
因此,對於最初的參考示例,以下內容應該有效。
變量.yml:
variables:
foo: bar
模板 1.yml:
variables:
- template: variables.yml
....
steps:
...
- task: CmdLine@2
displayName: Display Variable
inputs:
script: |
echo $(foo)
azure-pipelines.yml:
resources:
repositories:
- repository: build-scripts
type: git
name: project-name/build-scripts
extends:
template: template-1.yml@build-scripts
模板2.yml
steps:
...
- task: CmdLine@2
displayName: Display Variable
inputs:
script: |
echo $(foo)
這種方法可以幫助某人,所以我決定在這里發布。
這種情況的另一種“解決方法”,可能有點“臟”,因為每次執行模板時都需要明確指定參數,如果要傳遞很多參數,這不是一個好主意。 (實際上,有一種方法,請閱讀以下改進版本)但是,如果您真的想要或沒有那么多參數,這應該可行:
邏輯是:您在變量模板中擁有所有參數,例如templates/vars.yml
:
variables:
- name: myVar1
value: MyValue1
- name: myVar2
value: MyValue2
並且由於您在變量中擁有所需的一切,因此可能無需將變量導入模板本身,因為模板將在管道中執行,這將導入您的變量,您可以顯式替換它,如下例所示:
templates/my-template-setup-env.yml
的內容(里面沒有變量):
steps:
- script: |
echo "$(myVar3)"
my-azure-pipeline.yml
的內容(帶有導入變量模板):
name: my-cute-CI-name
trigger:
- main
pool:
vmImage: ubuntu-18.04
variables:
- template: templates/vars.yml # importing your variables from templates
stages:
- stage: RunTheStage
displayName: 'Run first stage'
jobs:
- job: RunTheJob
displayName: 'Run your job'
steps:
- template: templates/my-template-setup-env.yml # your template
parameters:
myVar3: $(myVar1) # substitute value from vars.yml, so myVar1 will be used in templated and printed
改良版
但是,如果您在所有管道和模板中對參數和變量進行了唯一命名,則在模板使用期間不明確指定它是安全的,這也將起作用:
已編輯和縮短的my-azure-pipeline.yml
(如果模板中的變量和參數名稱相同):
variables:
- template: templates/vars.yml
...
steps:
- template: templates/my-template-setup-env.yml # env preparation
# parameters:
# myVar2: $(myVar2) # you don't need to pass any parameters explicitly to the template since you have same name of variable
templates/my-template-setup-env.yml
應該是這樣的:
steps:
- script: |
echo "$(myVar2)" # not myVar3, since myVar3 is not in initial variables file templates/vars.yml
或者您還需要將剩余的變量(在我們的第一種情況下為templates/vars.yml
)添加到templates/vars.yml
文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.