如何通过代码创建Azure DevOps服务连接?

问题描述 投票:0回答:1

我有一个天蓝色的 DevOps 构建管道,在其中一个阶段我想创建一个服务连接。我想要“kubernetes”和“dockerregistry”类型的服务连接。如何实现?

1。 Azure CLI: 我看到 azure CLI 命令仅创建“azurerm”类型的服务连接和 github。但没有找到任何“Kubernetes”和“dockerregistry”类型的命令。 https://learn.microsoft.com/en-us/cli/azure/devops/service-endpoint?view=azure-cli-latest

2。休息API: 找到了创建服务连接的rest api方法,我尝试给出类型“kubernetes”,但无法弄清楚这里的身份验证。给我错误“无效的客户端 ID”。

3.电源外壳: 没有找到任何与此相关的内容。

azure powershell azure-devops azure-cli azure-rest-api
1个回答
0
投票

你好!

正如我所见,您在

Powershell
中找不到任何方法来做到这一点,我为您创建了一些该场景的示例。

  • 首先,我们通过设置 Azure DevOps 组织的预期 URI 以及构建 headers-object 来做好准备
$Organization = "<AzDevOps-Org-Name>"
$PAT = "<PAT>" # <- Should have permissions for Service Connections

$EncodedPAT = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

$Headers = @{
  Authorization = "Basic " + $EncodedPAT
  "Content-Type" = "application/json"
}

$CreateEndpointsUri = "https://dev.azure.com/$($Organization)/_apis/serviceendpoint/endpoints?api-version=7.1-preview.4"

其他类型

  • 示例
    body
    创建
    Others
  • 的服务连接 Docker 注册表类型
# Docker Registry Connection - Others type
$Body = @{
  name = "DockerOthersTest1"
  type = "dockerregistry"
  url = "https://hub.docker.com/"
  authorization = @{
    parameters = @{
      registry = "https://nameofmyregistry.com"
      username = "myusername"
      email = "[email protected]"
    }
    scheme = "UsernamePassword"
  }
  serviceEndpointProjectReferences = @(
    @{
      projectReference = @{
        id = "<GUID-of-your-project>"
        name = "<Name-of-your-project>"
      }
      name = "DockerOthersTest1" # This will be the name seen in the Service Connections for the project 
    }
  )
} | ConvertTo-Json -Depth 10

# Send the request
Invoke-RestMethod -Uri $CreateEndpointsUri -Headers $Headers -Method Post -Body $Body

Azure 容器注册表类型

  • 示例
    body
    创建
    Azure Container Registry
  • 的服务连接 Docker 注册表类型
$Body = @{
  name = "DockerAcrTest1"
  type = "dockerregistry"
  url = "https://hub.docker.com/"
  authorization = @{
    parameters = @{
      loginServer = "<myACRname>.azurecr.io"
      role = "8311e382-0749-4cb8-b61a-304f252e45ec" # This is AcrPush role
      scope = "<Resource-ID-of-ACR>"
      servicePrincipalId = "<Application-ID-of-SPN>"
      tenantId = "<Tenant-GUID>"
    }
    scheme = "ServicePrincipal"
  }
  data = @{
    appObjectId = "<SPN-App-object-ID>"
    registryId = "<Resource-ID-of-my-ACR>"
    registrytype = "ACR"
    spnObjectId = "<Enterprise-SPN-App-object-ID>"
    subscriptionId = "<GUID-for-subscription>"
    subscriptionName = "<Name-of-my-subscription>"
  }
  serviceEndpointProjectReferences = @(
    @{
      projectReference = @{
        id = "<GUID-of-your-project>"
        name = "<Name-of-your-project>"
      }
      name = "DockerAcrTest1" # This will be the name seen in the Service Connections for the project 
    }
  )
} | ConvertTo-Json -Depth 10

# Send the request
Invoke-RestMethod -Uri $CreateEndpointsUri -Headers $Headers -Method Post -Body $Body

结果示例

其他类型

ACR型

我希望这有帮助!祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.