[英]Connect-AzAccount with Azure Devops Pipeline?
我發現很難找到將 connect-azaccount 與 azure devops 管道一起使用的最佳和安全方法。 我在管道中有以下這個簡單的 powershell 腳本,它用於創建 azure 資源。 只是為了簡化事情,我只使用了資源組的創建:
$Location = "Location Name"
$resourceGroupName = "Resource Group Name"
try {
#Creation of Resource Group
$resourceGroup = Get-AzResourceGroup -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue
if($null -eq $resourceGroup)
{
New-AzResourceGroup -Name $resourceGroupName -Location $Location
}
else
{
Write-Host "The ResourceGroup with the name: $resourceGroupName already exists."
}
}
catch
{
Write-Host "Error occurred: $_"
}
這里的問題是當管道正在運行並到達 Powershell 任務時,它給了我一個錯誤,錯誤發生:運行 Connect-AzAccount 登錄。
我的問題是,老實說,我不知道在不輸入任何用戶憑據的情況下哪種方式是最安全的連接方式。 它應該直接連接和創建資源。 請注意,我使用的是 Multi-Factor Authentication 。 為了實現這一點,我找到了幾種解決方案,但在選擇最佳方式時我需要幫助。 我通過在 Yaml 文件中添加 powershell 任務找到了幾種解決方案。 這是 Yaml 顯示 powershell 任務運行腳本:
- task: PowerShell@2
inputs:
filePath: '$(Pipeline.Workspace)/Deploy/functionapp.ps1'
選項1:
Connect-AzAccount -Tenant 'xxxx-xxxx-xxxx-xxxx' -SubscriptionId 'yyyy-yyyy-yyyy-yyyy'
現在這里的問題是租戶 ID 和訂閱將在代碼中可見,這是一個非常糟糕的做法
選項 2 是使用以下腳本:
$User = "xxx@xxxx.onmicrosoft.com"
$PWord = ConvertTo-SecureString -String "<Password>" -AsPlainText -Force
$tenant = "<tenant id>"
$subscription = "<subscription id>"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User,$PWord
Connect-AzAccount -Credential $Credential -Tenant $tenant -Subscription $subscription
這與第一個非常相似,但如果我沒記錯的話,它僅限於特定用戶。
選項 3 是使用服務主體:
$azureAplicationId ="Azure AD Application Id"
$azureTenantId= "Your Tenant Id"
$azurePassword = ConvertTo-SecureString "strong password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Connect-AzAccount -Credential $psCred -TenantId $azureTenantId -ServicePrincipal
我不知道創建服務主體是否會產生任何費用以及我應該采取哪些步驟才能使其正常工作。
老實說,我對這一切都很陌生,有人可以告訴我實現這一目標的確切步驟是什么。 謝謝您的回答:)
最安全的方法是創建 Azure 資源管理器服務連接並在您的管道中使用它。 您可以使用自動方式創建它,也可以使用以前創建的服務主體手動創建它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.