从VSTS版本访问Azure Service Prinicpal详细信息

时间:2017-03-17 08:00:37

标签: powershell security azure azure-pipelines-release-pipeline azure-devops-rest-api

虽然我们能够在VSTS中使用Powershell Azure任务来发布版本,但我们有时也会为发行版运行F#脚本,作为其中的一部分,我们希望使用服务主体将资源部署到Azure。我们已经在VSTS中注册了SP,而Powershell可以使用它 - 但是有一种方法可以用于原始命令行等到达租户ID等等,以便我们可以手动使用它们?例如,作为环境变量?

我们唯一的另一种选择是将租户ID等手动复制到版本作为构建变量,但这对我来说似乎是一种解决方法。

1 个答案:

答案 0 :(得分:2)

是的,您可以在自定义构建/发布步骤/任务中获取相关信息(例如租户ID)。

有关构建扩展的更多信息,请参阅:Add a build task

如果您不知道如何实现它,可以参考这些步骤获取Azure PowerShell步骤/任务的所有源代码。

  1. 设置内部部署构建代理:Deploy an agent on Windows
  2. 创建构建/发布定义
  3. 添加Azure PowerShell步骤/任务并对其进行配置
  4. 排队此构建/发布
  5. 登录构建代理计算机,检查[agent folder]\tasks\AzurePowerShell
  6. 中的Azure PowerShell步骤/任务

    简单的构建/发布步骤/任务扩展:

    文件:

     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."
        },
    ....