繁体   English   中英

从 yaml 管道创建自动化凭证

[英]Create an automation credential from a yaml pipeline

我正在尝试从 Azure DevOps 中的 YAML 管道创建自动化凭据。 我正在使用 AzurePowerShell@5 任务在 YAML 文件中编写(内联)PowerShell 脚本。 并且要创建的凭据的用户名和密码存储在 Azure DevOps 中的一个变量组中。

在 Microsoft 文档中,cmdlet New-AzAutomationCredential包含如何使用 cmdlet 的示例:

$User = "Contoso\PFuller"
$Password = ConvertTo-SecureString "Password" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
New-AzAutomationCredential -AutomationAccountName "Contoso17" -Name "ContosoCredential" -Value $Credential -ResourceGroupName "ResourceGroup01"

但是如果我尝试用变量组中的两个变量替换纯文本用户名和密码,则部署失败:

$User = $UserVariable
$Password = ConvertTo-SecureString $PasswordVariable -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
New-AzAutomationCredential -AutomationAccountName "Contoso17" -Name "ContosoCredential" -Value $Credential -ResourceGroupName "ResourceGroup01"

是否可以使用管道变量创建凭据并执行 New-AzAutomationCredential cmdlet?

编辑:添加有关我在哪个级别设置变量组的信息:

- stage: devstage
  displayName: 'Dev Environment Deployment'
  dependsOn: build
  condition: succeeded()
  variables: 
  - group: dev-vg

如果它们在变量中,那么您应该从环境中引用它们或将它们作为参数显式传递给脚本。 除非明确配置,否则不会在环境中设置机密。

AzurePowerShell@v5
  - inputs:
    script: |
       $User = $env:username
       $Password = ConvertTo-SecureString $env:password -AsPlainText -Force
       $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
       New-AzAutomationCredential -AutomationAccountName "Contoso17" -Name "ContosoCredential" -Value $Credential -ResourceGroupName "ResourceGroup01"
  - env:
    myuser: $(username)
    mypassword: $(password)

或者,将变量直接传递到脚本内容中(警告,脚本,包括密码将通过这种方式写入磁盘):

AzurePowerShell@v5
  - inputs:
    script: |
       $User = $(username)
       $Password = ConvertTo-SecureString $(password) -AsPlainText -Force
       $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
       New-AzAutomationCredential -AutomationAccountName "Contoso17" -Name "ContosoCredential" -Value $Credential -ResourceGroupName "ResourceGroup01"

在 YAML 管道中,您可以在根、阶段和作业级别设置变量。 您还可以在 UI 中指定 YAML 管道之外的变量。 当您在 UI 中设置变量时,该变量可以被加密并设置为机密。 秘密变量不会在 YAML 管道中自动解密,需要使用 env: 或根级别的变量传递到您的 YAML 文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM