Azure DevOps CLI 服务终结点

Azure DevOps Services

az devops service-endpoint使用此命令,可以创建和管理不同类型的服务连接。 服务连接允许 Azure DevOps 与外部服务通信,例如 Azure、Bitbucket、Kubernetes、Maven、GitHub 等。 使用 az devops service-endpoint,可以执行以下任务:

  • 使用配置文件创建服务终结点
  • 更新服务终结点
  • 管理 GitHub 服务终结点/连接
  • 管理 Azure 资源管理器服务终结点/连接
  • 列出为项目定义的服务终结点
  • 获取服务终结点的详细信息。

有关详细信息命令语法,请参阅 az devops service-endpoint。 有关服务终结点的 REST API 的语法,请参阅 终结点

还可以使用 azure cli 命令获取详细信息、列出、删除和更新服务终结点。 请参阅 Azure DevOps CLI 示例、服务终结点或服务连接的索引。

若要使用 Web 门户创建和编辑服务连接,请参阅 管理服务连接

使用配置文件创建服务终结点

若要使用配置文件创建服务终结点,必须先定义配置文件。 配置文件的内容因连接类型而异,例如 Azure 经典、Azure 数据资源管理器、Bitbucket Cloud、Chef 等。

配置文件格式

以下语法显示了 json 配置文件的格式。

{
  "data": {},
  "name": "MyNewServiceEndpoint",
  "type": "Generic",
  "url": "https://myserver",
  "authorization": {
    "parameters": {
      "username": "myusername",
      "password": "mysecretpassword"
    },
    "scheme": "UsernamePassword"
  },
  "isShared": false,
  "isReady": true,
  "serviceEndpointProjectReferences": [
    {
      "projectReference": {
        "id": "c7e5f0b3-71fa-4429-9fb3-3321963a7c06",
        "name": "TestProject"
      },
      "name": "MyNewServiceEndpoint"
    }
  ]
}

下表对各参数进行了说明: 该 type 参数支持创建任何类型的服务终结点。

参数 类型 说明
name string 设置终结点的友好名称。
type string 设置终结点的类型。
url string 设置终结点的 URL。
authorization EndpointAuthorization 设置用于与终结点通信的授权数据。
isShared boolean 指示服务终结点是否与其他项目共享。
isReady boolean EndPoint 状态指示器。
serviceEndpointProjectReferences 项目参考 设置服务终结点的项目引用。

有关受支持的类型及其所需输入参数的列表,可以练习以下 REST API 条目:

https://dev.azure.com/{organization}/_apis/serviceendpoint/types?api-version=6.0-preview.1

此外,有关服务连接类型及其可能需要的其他参数的说明,请参阅 管理服务连接、常见服务连接类型

运行 create 命令

使用命令创建服务终结点 az devops service-endpoint create

az devops service-endpoint create --service-endpoint-configuration 
                                  [--encoding {ascii, utf-16be, utf-16le, utf-8}]
                                  [--org]
                                  [--project]

参数

  • service-endpoint-configuration:必需。 json具有服务终结点配置的配置文件的名称。
  • 编码:可选。 输入文件的编码。 默认值为 utf-8。 接受的值:asciiutf-16beutf-16leutf-8
  • org:Azure DevOps 组织 URL。 可以使用 配置默认组织 az devops configure -d organization=ORG_URL。 如果未配置为默认值,则为必需。
  • 项目:项目的名称或 ID。 可以使用 配置默认项目 az devops configure -d project=NAME_OR_ID。 如果未配置为默认值,则为必需。

示例

以下命令创建引用 ServiceConnectionGeneric.json 该文件的服务连接。

az devops service-endpoint create --service-endpoint-configuration ./ServiceConnectionGeneric.json

成功创建后,会向服务终结点分配一个 Id 响应,并返回类似于以下语法的响应。

{
  "administratorsGroup": null,
  "authorization": {
    "parameters": {
      "password": null,
      "username": "myusername"
    },
    "scheme": "UsernamePassword"
  },
  "createdBy": {
    "descriptor": "aad.OGYxZTFlODEtMGJiNC03N2ZkLThkYzUtYjE3MTNiNTQ2MjQ4",
    "directoryAlias": null,
    "displayName": "Jamal Hartnett",
    "id": "60c83423-4eb6-4c5e-8395-1e71cb4aef4c",
    "imageUrl": "https://dev.azure.com/fabrikam/_apis/GraphProfile/MemberAvatars/aad.OGYxZTFlODEtMGJiNC03N2ZkLThkYzUtYjE3MTNiNTQ2MjQ4",
    "inactive": null,
    "isAadIdentity": null,
    "isContainer": null,
    "isDeletedInOrigin": null,
    "profileUrl": null,
    "uniqueName": "fabrikamfiber4@hotmail.com",
    "url": "https://spsprodwcus0.vssps.visualstudio.com/A0214b8cc-a36c-4b93-abbf-6348473c2f0a/_apis/Identities/60c83423-4eb6-4c5e-8395-1e71cb4aef4c"
  },
  "data": {},
  "description": null,
  "groupScopeId": null,
  "id": "3b6890ef-54b3-47ec-a907-a5d2f96237da",
  "isReady": true,
  "isShared": false,
  "name": "MyNewServiceEndpoint",
  "operationStatus": null,
  "owner": "library",
  "readersGroup": null,
  "serviceEndpointProjectReferences": [
    {
      "name": "MyNewServiceEndpoint",
      "projectReference": {
        "id": "677da0fb-b067-4f77-b89b-f32c12bb8617",
        "name": null
      }
    }
  ],
  "type": "Generic",
  "url": "https://myserver"
}

创建 GitHub 服务终结点

若要创建 GitHub 服务终结点,请使用 az devops service-endpoint github create 以下命令:

az devops service-endpoint github create --github-url
                                         --name 
                                         [--org]
                                         [--project]

在交互模式下,命令 az devops service-endpoint github create 使用提示消息请求 GitHub PAT 令牌 ,以便自动化目的使用 AZURE_DEVOPS_EXT_GITHUB_PAT 环境变量设置 GitHub PAT 令牌。 有关详细信息,请参阅使用个人访问令牌登录(PAT)。

创建 Azure 资源管理器服务终结点

若要创建 Azure 资源管理器服务终结点,请使用az devops service-endpoint azurerm create该命令。

az devops service-endpoint azurerm create --azure-rm-service-principal-id
                                          --azure-rm-subscription-id
                                          --azure-rm-subscription-name
                                          --azure-rm-tenant-id
                                          --name
                                          [--azure-rm-service-principal-certificate-path] 
                                          [--org]
                                          [--project]

使用客户端密码/密码

在交互模式下, az devops service-endpoint azurerm create 该命令使用提示消息请求服务主体密码/机密。 出于自动化目的,请使用 AZURE_DEVOPS_EXT_AZURE_RM_SERVICE_PRINCIPAL_KEY 环境变量设置服务主体密码/机密。

export AZURE_DEVOPS_EXT_AZURE_RM_SERVICE_PRINCIPAL_KEY=<your_secret_here>
$env:AZURE_DEVOPS_EXT_AZURE_RM_SERVICE_PRINCIPAL_KEY=<your_secret_here>

使用客户端证书

如果 Microsoft Entra 应用程序使用 证书进行身份验证,则为证书创建一个 .pem 文件,并使用参数将路径传递给 .pem 文件 --azure-rm-service-principal-certificate-path

可以使用 openssl 创建 .pem 文件:

openssl pkcs12 -in file.pfx -out file.pem -nodes -password pass:<password_here>