admin管理员组文章数量:1300188
i'm trying to pass to a parameter a variable declared in a variable group devops library:
name: $(BuildDefinitionName)$(SourceBranchName)$(date:yyyyMMdd)$(rev:.r)
trigger:
- none
variables: # terraform variables
- group: 'IAC Terraform'
pool:
name: $(agentPoolName)
stages:
##################################################################### 1-resource_group
###################################################### NO PROD
- stage: noprod_validate_terraform_rg
displayName: 'RG - NO PROD Validate TF'
jobs:
- template: plan-apply.yml
parameters:
env: $(env-dv)
project: "myproject/$(env-dv)/1-resource_group"
subscription_id: $(subscription_id_dv)
bk_key: $(rg_state_name)
- template: plan-apply.yml
parameters:
env: $(env-st)
project: "myproject/networking/$(env-st)/1-resource_group"
subscription_id: $(subscription_id_st)
bk_key: $(rg_state_name)
when i run the pipeline parameter pass value as $(env-st) etc.. it is work if i declare directly in parameter the correct value. Is there a way to pass the correct value in parameter from variable group var??
i'm trying to pass to a parameter a variable declared in a variable group devops library:
name: $(BuildDefinitionName)$(SourceBranchName)$(date:yyyyMMdd)$(rev:.r)
trigger:
- none
variables: # terraform variables
- group: 'IAC Terraform'
pool:
name: $(agentPoolName)
stages:
##################################################################### 1-resource_group
###################################################### NO PROD
- stage: noprod_validate_terraform_rg
displayName: 'RG - NO PROD Validate TF'
jobs:
- template: plan-apply.yml
parameters:
env: $(env-dv)
project: "myproject/$(env-dv)/1-resource_group"
subscription_id: $(subscription_id_dv)
bk_key: $(rg_state_name)
- template: plan-apply.yml
parameters:
env: $(env-st)
project: "myproject/networking/$(env-st)/1-resource_group"
subscription_id: $(subscription_id_st)
bk_key: $(rg_state_name)
when i run the pipeline parameter pass value as $(env-st) etc.. it is work if i declare directly in parameter the correct value. Is there a way to pass the correct value in parameter from variable group var??
Share Improve this question edited Feb 11 at 14:27 Emanuele asked Feb 11 at 14:22 EmanueleEmanuele 3595 silver badges18 bronze badges 4- Hi @Emanuele, Good day. Have you got a chance to check the answer below to test with variables defined in template and referenced in template expressions? Hope the explanation could help resolve your query in this post. Thx. – Alvin Zhao - MSFT Commented Feb 13 at 1:24
- hi @AlvinZhao-MSFT i will test it next week and let you know – Emanuele Commented Feb 14 at 16:49
- Sure, and please share the deploy.yml definition if further assistance is needed. Thx for the sharing & good luck with your works to go. – Alvin Zhao - MSFT Commented Feb 17 at 1:52
- Hi @Emanuele, May I know if the suggested syntax is working for you? Please feel welcome to share a minimal sample of your deploy.yml that reproduces the issue for further investigation. Have a lovely weekend. – Alvin Zhao - MSFT Commented Feb 21 at 8:56
2 Answers
Reset to default 0Technically, no. The values that are passed into the parameters aren't resolved, and this shouldn't be a problem if you're using the parameters correctly. Templates aren't functions, the entire pipeline is expanded at compile-time into a single YAML file and then executed.
The most important thing to understand about the pipeline run sequence is that parameters are evaluated at compile-time, whereas variables are evaluated at run-time.
When you pass a variable using macro syntax to a parameter $(subscription_id_dv)
, the value at compile-time is unknown so the literal value of "$(subscription_id_dv)" is passed to the template.
In some scenarios, this is perfectly acceptable assuming that the value you are passing will be used as a variable or other runtime expression. In the example below, the parameter is used to set an environment variable for a script. The default value refers to an existing variable declared elsewhere. This allows the caller to pass a literal value or another variable. If no value is passed, the value of $(azureRegion)
if it's defined elsewhere is used at runtime:
Valid:
parameters:
- name: azureRegion
type: string
default: $(azureRegion)
steps:
- script: |
tf plan
env:
TF_VAR_REGION: ${{ parameters.azureRegion }} # compiles to -> $(azureRegion)
Invalid:
However, it is not possible to use a variable in a compile-time expression. In this case, the name of a variable group refers to an external resource so it must have a value at compile-time:
# template.yml
parameters:
- name: variableGroupName
type: string
variables:
- group: ${{ parameters.variableGroupName }} # <- value must be known at compile-time
steps:
- script: ...
# pipeline.yml
variables:
- group: external_variables
steps:
- template: template.yml
parameters:
variableGroupName: $(dev_group_name) # creates an error about missing variable group "$(dev_group_name)"
Caveats:
There are of course some exceptions and caveats:
Variables that are declared inline at the top of the pipeline can appear in compile-time expressions:
${{ variables.<name> }}
Some (but not all) Pre-Defined pipeline variables are available during compile-time. (See the "Available in Templates" column)
Some task input variables and yaml elements that refer to system resources cannot be declared as variables. For example, service-connections cannot be declared as run-time variables because the pipeline needs to check authorizations, permissions and checks + approvals before the job can run.
You didn't share your plan-apply.yml template definition, therefore we couldn't know how your pipeline used those parameters to be passed with values of variables in a variable group across templates.
Based on my sample templates below, we CAN reference values of variables in a variable group using macro syntax $(variableName)
for some pipeline task properties (like workingDirectory
) or in the script, being passed as parameter values across templates, since those properties or script only require the variables to be expanded at pipeline runtime. However, we CANNOT pass parameter values with macro syntax variables for certain pipeline structures such as the symbolic name of a deployment
job or its associated environment
, as they require the variables to be expanded at compile time, while the variables in a variable group are only processed at pipeline runtime after compile time.
main template
name: $(BuildDefinitionName)$(SourceBranchName)$(date:yyyyMMdd)$(rev:.r)
trigger:
- none
variables: # terraform variables
- group: 'IAC Terraform'
- template: variables.yml # Defines variables env-dv and env-st
- name: system.debug
value: true
pool:
name: $(agentPoolName)
stages:
##################################################################### 1-resource_group
###################################################### NO PROD
- stage: noprod_validate_terraform_rg
displayName: 'RG - NO PROD Validate TF'
jobs:
- template: plan-apply.yml
parameters:
env: ${{ variables['env-dv'] }} # $(env-dv)
project: "myproject/$(env-dv)/1-resource_group"
subscription_id: $(subscription_id_dv)
bk_key: $(rg_state_name)
workingDirectory: $(workingDirectory)
- template: plan-apply.yml
parameters:
env: ${{ variables['env-st'] }} # $(env-st)
project: "myproject/networking/$(env-st)/1-resource_group"
subscription_id: $(subscription_id_st)
bk_key: $(rg_state_name)
workingDirectory: $(workingDirectory)
plan-apply.yml
parameters:
- name: env
default: ''
- name: project
default: ''
- name: subscription_id
default: ''
- name: bk_key
default: ''
- name: workingDirectory
default: ''
jobs:
- deployment: deploy_${{ parameters.env }}
pool:
vmImage: windows-latest
displayName: Deploy ${{ parameters.env }}
environment: E-${{ parameters.env }}
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: AzureCLI@2
inputs:
azureSubscription: 'ARMSvcCnnWIFRootMG'
addSpnToEnvironment: true # Enabled to use the token stored in the ARM service connection
workingDirectory: ${{ parameters.workingDirectory }} # Where the terraform templates locate
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
Write-Host "================ 0. Install Terraform on Windows & setup Terraform environment variables================"
choco install terraform
terraform version
$env:ARM_TENANT_ID ??= $env:tenantId
$env:ARM_SUBSCRIPTION_ID ??= "${{ parameters.subscription_id }}"
$env:ARM_CLIENT_ID ??= $env:servicePrincipalId
$env:ARM_USE_OIDC ??= ($env:idToken -ne $null).ToString().ToLower()
$env:ARM_OIDC_TOKEN ??= $env:idToken
$env:ARM_USE_CLI ??= (!($env:idToken -or $env:servicePrincipalKey)).ToString().ToLower()
$env:ARM_CLIENT_SECRET ??= $env:servicePrincipalKey
Write-Host "================ 1. Terraform init ================"
terraform init `
-backend-config="resource_group_name=rg-azstorageaccount" `
-backend-config="storage_account_name=azsaterraform${{ parameters.env }}" `
-backend-config="container_name=containertf" `
-backend-config="key=${{ parameters.bk_key }}"
Write-Host "================ 2. Terraform apply ================"
terraform apply -input=false -var "RGNAME=rg-tf-dem-sp-wif-b$(Build.BuildId)" -auto-approve
With that being said, you may also consider storing variable values in a variable template instead of a variable group, so that you can pass the static variable value(s) in template expressions (${{ variables['variable.name']}}
), which are expanded at compile time.
variables.yml
variables:
- name: env-dv
value: dev
- name: env-st
value: test
See the document to help us Understand variable syntax.
本文标签: azureunable to read variable from parameterStack Overflow
版权声明:本文标题:azure - unable to read variable from parameter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741657962a2390869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论