gpt4 book ai didi

azure - DevOps 变量组

转载 作者:行者123 更新时间:2023-12-03 06:14:10 25 4
gpt4 key购买 nike

我遇到的情况是,我有一个变量组来保存我的管道任务将使用的凭据(用户名/密码)。

我想使用一个变量组来保存所有环境的凭据,并且我想将环境名称(dev、test等)添加到所有变量中,这样我就可以将它用作参数来帮助任务区分要使用哪个变量使用。我不想为每个环境创建多个变量组。

我的变量组名称:CredentialsVariableGroup变量:用户名开发者密码用户名测试密码测试用户名uat密码用户名产品密码产品

现在在 yaml 中,我将在调用时传递环境名称。环境名称的潜在值 - dev、test、prod:

parameters:
- name: environmentName
type: string

// some other parameters, stages, variable groups, jobs

- task: Sometask
displayName: Sometaskdisplayname
inputs:
username: '$(usernamedev)'
password: '$(pwddev)'

我想要一种方法来使环境名称部分从参数中使用,以便它变得动态并在相应环境中部署时引用正确的变量。

编辑:

库(管道)中我的变量的快照 enter image description here

我的 Yaml 管道

parameters:
- name: environmentName
type: string

stages:
- stage:
dependsOn:
condition:
displayName:
variables:
- group: Group1
- group: Group2
- group: 'MyVariableGroup'
- template: vars.yaml
- ${{ if ne(parameters[''], 'PROD') }}:
- group: NonProdGroup

- ${{ if eq(parameters[''], 'PROD') }}:
- group: ProdGroup

- group: Group3
- group: Group4

pool:
name: Default
jobs:
- deployment:
displayName:
pool:
name: Default
environment:
name: ${{ parameters.paramname }}
strategy:
runOnce:
deploy:
steps:
- download: current
artifact: drop

- task: AzureResourceManagerTemplateDeployment@3
displayName: 'Deploy arm template'
inputs:
azureResourceManagerConnection:
subscriptionId: ''
deploymentName: $()
location: ''
resourceGroupName:
csmFile: ''
csmParametersFile: ''
deploymentMode: 'Incremental'

- job: DeployApiApp
dependsOn:
displayName:
steps:
- task: DownloadBuildArtifacts@0
displayName:
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'drop'
downloadPath: ''

- task: AzureWebApp@1
displayName:
inputs:
appType: webApp
azureSubscription: ''
appName: ''
package: ''

- job: DeployWebApp
dependsOn:
displayName:
steps:
- task: DownloadBuildArtifacts@0
displayName:
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'drop'
downloadPath: ''

- task: AzureWebApp@1
displayName:
inputs:
appType: webApp
azureSubscription: ''
appName: ''
package: ''

- job: DeployDB
dependsOn:
displayName:
steps:
- download: current
artifact:

- task: SqlAzureDacpacDeployment@1
displayName:
inputs:
azureSubscription: ''
ServerName: ''
DatabaseName: ''
SqlUserName: ''
SqlPassword: $[variables['adminpwd${{ upper(parameters.environmentName) }}']]
deployType: 'DacpacTask'
DeploymentAction: 'Publish'
DacpacFile: ''
AdditionalArguments: ''
IpDetectionMethod: ''
StartIpAddress: ''
EndIpAddress: ''
DeleteFirewallRule:

最佳答案

正如 @daniel-mann 所评论的,您可能不想将来自不同环境的变量混合在同一变量组中。

如果您需要动态构建变量名称,则此语法应该有效:

parameters:
- name: environmentName
type: string

steps:
- pwsh: |
Write-Host "$(username${{parameters.environmentName}})"

根据您组织 yaml 文件的方式,运行时变量也应该起作用:

parameters:
- name: environmentName
type: string

jobs:
- job: A
variables:
- group: my-variable-group-name
- name: username
value: $[variables['username${{parameters.environmentName}}']]
steps:
- pwsh: |
Write-Host "$(username)"

关于azure - DevOps 变量组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76440480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com