虽然我们可以在 VSTS 中使用 Powershell Azure 任务进行发布,但有时我们也会为发布运行 F# 脚本,作为其中的一部分,我们希望使用服务主体将资产部署到 Azure。我们已经在 VSTS 中注册了 SP,Powershell 可以访问它 - 但是有没有办法让例如原始命令行等访问租户 ID 等,以便我们可以手动使用它们?例如,作为环境变量?
我们唯一的其他选择是手动将租户 ID 等复制到版本中作为构建变量,但这对我来说似乎是一种解决方法。
虽然我们可以在 VSTS 中使用 Powershell Azure 任务进行发布,但有时我们也会为发布运行 F# 脚本,作为其中的一部分,我们希望使用服务主体将资产部署到 Azure。我们已经在 VSTS 中注册了 SP,Powershell 可以访问它 - 但是有没有办法让例如原始命令行等访问租户 ID 等,以便我们可以手动使用它们?例如,作为环境变量?
我们唯一的其他选择是手动将租户 ID 等复制到版本中作为构建变量,但这对我来说似乎是一种解决方法。
是的,您可以在自定义构建/发布步骤/任务中获取相关信息(例如租户 ID)。
更多关于构建扩展的信息,可以参考:添加构建任务。
如果您不知道如何实现,可以参考这些步骤来获取 Azure PowerShell 步骤/任务的所有源代码。
[agent folder]\tasks\AzurePowerShell
简单的构建/发布步骤/任务扩展:
文件:
AzureCustomTask
Ps_modules (can be found in the Azure PowerShell step/task folder, refer to previous steps)
Test.ps1
Icon.png
Task.json
Test.ps1 代码:
$serviceNameInput = Get-VstsInput -Name ConnectedServiceNameSelector -Default 'ConnectedServiceName'
Write-Host $serviceNameInput
$serviceName = Get-VstsInput -Name $serviceNameInput -Default (Get-VstsInput -Name DeploymentEnvironmentName)
Write-Host $serviceName
if (!$serviceName) {
# Let the task SDK throw an error message if the input isn't defined.
Get-VstsInput -Name $serviceNameInput -Require
}
$endpoint = Get-VstsEndpoint -Name $serviceName -Require
Write-Host $endpoint.Auth.Parameters.TenantId
task.json中的部分代码(选择订阅的输入框):
"inputs": [
{
"name": "ConnectedServiceName",
"type": "connectedService:AzureRM",
"label": "Azure RM Subscription",
"defaultValue": "",
"required": true,
"helpMarkDown": "Select the Azure Resource Manager subscription for the deployment."
},
....