从构建管道启动 Azure 虚拟机

start Azure Virtual machine from the build pipeline

请帮助我从构建管道启动某个虚拟机。

我可以使用我的域帐户访问 https://dev.azure.com/organization and https://portal.azure.com/#home

我想编写将连接到门户并启动 VM 的构建管道

所以我创建了服务连接并编写了如下构建管道代码:

variables:
  resourceGroup: group-AZDCWBLA03-017732
  system.Debug: true
pool: server
steps:

    - task: InvokeRESTAPI@1
      displayName: 'Invoke REST API: POST'
      inputs:
        serviceConnection: 'devTest VM start'
        headers: |
         {
         "Content-Type":"application/json", 
         "Authorization": "Bearer $env:TOKEN"
         }
        urlSuffix: '/resourceGroups/$(resourceGroup)/providers/Microsoft.Compute/virtualMachines/AZDCWBLA03/start?api-version=2018-06-01'
      env:
        TOKEN: $(system.accesstoken)

机器名称是 AZDCWBLA03。构建结果是

============================================================================== 
Task         : Invoke REST API: POST
Description  : Invoke a REST API as a part of your pipeline.
Version      : 1.0.6
Author       : Microsoft Corporation
Help         : [More information](https://go.microsoft.com/fwlink/?linkid=870236)
============================================================================== 



POST https://management.azure.com/subscriptions/ orgid /resourceGroups/group-AZDCWBLA03-017732/providers/Microsoft.Compute/virtualMachines/AZDCWBLA03/start?api-version=2018-06-01
                Response Code: 0
                Response: An error was encountered while processing request. Exception: {"error":{"code":"InvalidAuthenticationToken","message":"The access token is invalid."}}
Exception Message: The remote server returned an error: (401) Unauthorized. (type WebException)

问题是我不知道构建令牌如何与 azure VM 门户连接。我想 $(system.accesstoken) 可能与我需要的不一样。如果可以执行 Azure REST API

,我不想使用我的用户凭据(除了仅用于测试目的)并使用当前构建令牌

我可以从日志中看到 POST 使用具有正确 orgid 和资源组名称的正确 URI。

你能给我一些提示吗?

我相当确定 $(system.accesstoken) 仅对调用 Azure Devops 本身有效。因此,您需要以某种方式传入令牌(在前面的步骤之一中获取)。

但老实说,使用 Azure Powershell 步骤要简单得多:

- task: AzurePowerShell@3
  inputs:
    azureSubscription: Subscription Name
    ScriptType: InlineScript
    Inline: |
        Start-AzVm -Name AZDCWBLA03 -ResourceGroupName group-AZDCWBLA03-017732
    azurePowerShellVersion: LatestVersion

它将为您处理身份验证,但您需要创建服务连接并赋予其适当的权限(例如 VM Contributor

我来晚了一点,但无论如何这对我有用:

parameters:
- name: action
  default: ''
  values:
  - start
  - deallocate

steps:
  - task: InvokeRESTAPI@1
    inputs:
      connectionType: 'connectedServiceNameARM'
      azureServiceConnection: '$(AzureService)'
      method: 'POST'
      headers: |
        {
          "Content-Type":"application/json",
          "AuthToken": "$(system.AccessToken)"
        }
      urlSuffix: 'subscriptions/$(AzureSubscriptionId)/resourceGroups/$(AzureResourceGroupName)/providers/Microsoft.Compute/virtualMachines/$(AzureVmName)/${{ parameters.action }}?api-version=$(AzureApiVersion)'

首先添加了新参数 connectionType 并且 serviceConnection 替换为 azureServiceConnection.
现在 $(system.AccessToken) 实际上属于已建立的 azureServiceConnection,需要作为 header 中的 AuthToken 参数发送。 此外,我使用 Azure SubscriptionId 扩展了 urlSuffix

有了这个开始并按预期解除分配工作。