我正在尝试使用 Connect-AzAccount 和服务主体连接到 Azure 订阅。我正在使用这种方法https://learn.microsoft.com/en-us/powershell/module/az.accounts/connect-azaccount?view=azps-11.4.0#example-3-connect-to-azure-使用服务主体帐户。我编写了一个 gitlab-ci.yml ,它应该连接到 azure 并运行 powershell 脚本。 Pipeline 在安装了 Powershell 和 Az 模块的 Windows 运行器上运行。当 Connect-AzAccount 运行时出错时,管道失败。
异常:发送请求时发生错误
我正在我客户的网络中运行它。在运行之前也要设置 PROXY。
gitlab-ci.yml
before-script:
- $env:HTTPS_PROXY = "https://xx.xx.xx.xxx"
- $env:HTTPS_PROXY_CERT = "C:\folders\cert.crt"
- $SecurePassword = ConvertTo-SecureString -String "xxxxxxx" -AsPlainText -Force
- $TenantId = 'xxxxxx'
- $ApplicationId = 'xxxxxx'
- $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationId, $SecurePassword
- Connect-AzAccount -ServicePrincipal -TenantId $TenantId -Credential $Credential
直接从 Windows 运行程序中的 powershell 运行所有这些命令,效果很好。但是,从 gitlab ci 运行时,相同的命令会失败。
Connect-AzAccount 无法从 Gitlab yaml 运行
确保验证没有网络限制或防火墙阻止从 GitLab 管道到
Azure
的连接
您可以使用下面的 .yml 和 powershell 文件来运行 Connect-AzAccount 以及
Service Principal
中的 GitLab
。
.gitlab-ci.yml
image: mcr.microsoft.com/powershell:latest
Job:
stage: deploy
script:
- pwsh ./powershell.ps1
powershell.ps1
Install-Module Az -Force
$TenantId = '22xxxxxxxx-4005-acfb-9bbfa7d40283'
$ApplicationId = 'bf7e1xxxxx9-10f90c4811d2'
$SecurePassword = 'yYA8Q~xxxxx-HKNYX~1S-vgAJVa.n'
$SecurePassword = ConvertTo-SecureString -String $SecurePassword -AsPlainText -Force
$pscredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationId, $SecurePassword
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $TenantId
New-AzResourceGroup -Name RG01 -Location "South Central US"
这是我的存储库,其中包含 .yml 和 PowerShell 文件。
运行管道后,它成功连接到Azure并创建了资源组。
Resource group
已创建成功。