你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

将项目存储库添加到实验室

本文介绍如何将项目存储库添加到 Azure 开发测试实验室中的实验室。 项目是安装在虚拟机 (VM) 上的工具或应用程序。 在从 GitHub 或 Azure Repos Git 存储库加载的 JSON 文件中定义项目。

公共开发测试实验室 GitHub 项目存储库提供了许多适用于 Windows 和 Linux 的常见项目。 默认情况下,此公共存储库中的项目在开发测试实验室中可用。 有关将项目添加到 VM 的信息,请参阅将项目添加到开发测试实验室 VM

还可以创建未在公共项目存储库中提供的自定义项目。 若要了解如何创建自定义项目,请参阅创建自定义项目。 你可以将自定义项目添加到自己的项目存储库,然后将该存储库添加到实验室,以便所有实验室用户都可以使用这些项目。

本文介绍如何使用 Azure 门户、Azure 资源管理 (ARM) 模板或 Azure PowerShell 将项目存储库添加到实验室。 还可以使用 Azure PowerShell 或 Azure CLI 脚本自动将项目存储库添加到实验室。

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

先决条件

若要将项目存储库添加到实验室,需要知道包含项目文件的 GitHub 或 Azure Repos 存储库的 Git HTTPS 克隆 URL 和个人访问令牌。

获取 GitHub 的克隆 URL 和个人访问令牌

  1. 在包含项目的 GitHub 存储库的主页上选择“代码”,然后在“克隆”下复制 HTTPS URL。
  2. 在 GitHub 右上角选择你的个人资料头像,然后选择“设置”。
  3. 在你的个人资料页上的左侧菜单中选择“开发人员设置”,然后选择“个人访问令牌”。
  4. 选择“生成新令牌”
  5. 在“新建个人访问令牌”页上的“备注”下,输入该令牌的可选说明。 接受所有默认值,然后选择“生成令牌”。
  6. 保存生成的令牌。

获取 Azure Repos 的克隆 URL 和个人访问令牌

  1. 在包含项目的存储库的主页上,选择“克隆”。 在“克隆存储库”页上,复制克隆 URL。
  2. 在“Azure DevOps”页的右上角,选择“用户设置”>“个人访问令牌”。
  3. 在“个人访问令牌”页上,选择“新建令牌”。
  4. 填写令牌信息,选择“读取”作为权限范围,然后选择“创建”。
  5. 在“成功”页上,请务必复制该令牌,因为 Azure Repos 不会存储或再次显示该令牌。

在 Azure 门户中将项目存储库添加到实验室

  1. 在实验室的“概述”页上,从左侧导航栏中选择“配置和策略”。

  2. 在“配置和策略”页上,在左侧导航栏中的“外部资源”下选择“存储库”。

    在“存储库”页上,“公共项目存储库”会自动显示并连接到开发测试实验室公共 GitHub 存储库。 如果未为实验室启用此存储库,可以通过选中“公共项目存储库”旁边的复选框,然后在顶部菜单栏上选择“启用”来启用它。

  3. 若要将项目存储库添加到实验室,请在顶部菜单栏中选择“添加”。

    显示存储库配置屏幕的屏幕截图。

  4. 在“存储库”窗格中输入以下信息:

    • 名称:要在实验室中使用的存储库名称。
    • Git 克隆 URI:GitHub 或 Azure 存储库中的 Git HTTPS 克隆 URL。
    • 分支(可选):包含你的项目定义的分支。
    • 个人访问令牌:GitHub 或 Azure Repos 中的个人访问令牌。
    • 文件夹路径:ARM 模板定义的文件夹,相对于 Git 克隆 URL。 请务必在文件夹路径中包含初始正斜杠。
  5. 选择“保存”。

    显示向实验室添加新项目存储库的屏幕截图。

该存储库现在会显示在实验室的“存储库”列表中。

使用 ARM 模板添加项目存储库

ARM 模板是描述要创建的 Azure 资源的 JSON 文件。 有关 ARM 模板的详细信息,请参阅了解 ARM 模板的结构和语法

以下 ARM 模板将一个项目存储库添加到实验室。 该模板创建实验室(如果尚不存在)。

查看 ARM 模板

示例模板在参数中收集以下信息。 某些参数具有默认值,但部署命令必须指定实验室名称、项目存储库 URI、存储库类型和存储库个人访问令牌。

  • 实验室名称。
  • 项目存储库在开发测试实验室中的显示名称。 默认值为 Team Repository
  • 前面复制的项目存储库 URI。
  • 包含项目的存储库分支。 默认值为 main
  • 包含项目的文件夹的名称。 默认值为:/Artifacts
  • 存储库类型。 允许的值为 VsoGit(适用于 Azure Repos)或 GitHub
  • 前面复制的存储库个人访问令牌。
{

    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "labName": {
            "type": "string"
        },
        "artifactRepositoryDisplayName": {
            "type": "string",
            "defaultValue": "Team Repository"
        },
        "artifactRepoUri": {
            "type": "string"
        },
        "artifactRepoBranch": {
            "type": "string",
            "defaultValue": "main"
        },
        "artifactRepoFolder": {
            "type": "string",
            "defaultValue": "/Artifacts"
        },
        "artifactRepoType": {
            "type": "string",
            "allowedValues": ["VsoGit", "GitHub"]
        },
        "artifactRepoSecurityToken": {
            "type": "securestring"
        }
    },
    "variables": {
        "artifactRepositoryName": "[concat('Repo-', uniqueString(subscription().subscriptionId))]"
    },
    "resources": [{
            "apiVersion": "2016-05-15",
            "type": "Microsoft.DevTestLab/labs",
            "name": "[parameters('labName')]",
            "location": "[resourceGroup().location]",
            "resources": [
                {
                    "apiVersion": "2016-05-15",
                    "name": "[variables('artifactRepositoryName')]",
                    "type": "artifactSources",
                    "dependsOn": [
                        "[resourceId('Microsoft.DevTestLab/labs', parameters('labName'))]"
                    ],
                    "properties": {
                        "uri": "[parameters('artifactRepoUri')]",
                        "folderPath": "[parameters('artifactRepoFolder')]",
                        "branchRef": "[parameters('artifactRepoBranch')]",
                        "displayName": "[parameters('artifactRepositoryDisplayName')]",
                        "securityToken": "[parameters('artifactRepoSecurityToken')]",
                        "sourceType": "[parameters('artifactRepoType')]",
                        "status": "Enabled"
                    }
                }
            ]
        }
    ]
}

部署模板

可通过多种方式部署 ARM 模板来创建或更新 Azure 资源。 有关信息和说明,请参阅以下文章:

此示例使用 Azure PowerShell 部署模板。

注意

用于部署模板的 cmdlet 与特定的上下文相关,因此它们使用当前租户和订阅。 如果需要更改上下文,请在部署模板之前使用 Set-AzContext

  1. 使用 New-AzResourceGroup 创建资源组。 如果要使用的资源组已存在,请跳过此步骤。

    New-AzResourceGroup -Name MyLabResourceGroup1 -Location westus
    
  2. 使用 New-AzResourceGroupDeployment 创建资源组部署。 可将资源多次部署到同一个资源组。 如果要多次部署到同一个资源组,请确保每个部署名称是唯一的。

    New-AzResourceGroupDeployment `
        -Name MyLabResourceGroup-Deployment1 `
        -ResourceGroupName MyLabResourceGroup1 `
        -TemplateFile azuredeploy.json `
        -TemplateParameterFile azuredeploy.parameters.json
    

成功运行 New-AzResourceGroupDeployment 后,输出将显示重要信息,例如配置状态(应为 succeeded)以及模板的任何输出。

使用 Azure PowerShell 添加项目存储库

以下示例 PowerShell 脚本 New-DevTestLabArtifactRepository.ps1 将一个项目存储库添加到实验室。 完整脚本包括一些详细的消息和注释。


<#

.SYNOPSIS
This script creates a new custom repository and adds it to an existing DevTest Lab.

.PARAMETER LabName
The name of the lab.

.PARAMETER LabResourceGroupName
The name of the resource group that contains the lab.

.PARAMETER ArtifactRepositoryName
Name for the new artifact repository. The script creates a random name for the repository if not specified.

.PARAMETER ArtifactRepositoryDisplayName
Display name for the artifact repository.
This name appears in the list of artifact repositories for a lab.

.PARAMETER RepositoryUri
Uri to the artifact repository.

.PARAMETER RepositoryBranch
Branch that contains the artifact files. Defaults to 'main'.

.PARAMETER FolderPath
Folder that contains the artifact files. Defaults to '/Artifacts'

.PARAMETER PersonalAccessToken
Personal access token for the GitHub or Azure Repos repository.

.PARAMETER SourceType
Whether the artifact repository is a VSOGit (Azure Repos) or GitHub repository.

.EXAMPLE
Set-AzContext -SubscriptionId 11111111-1111-1111-1111-111111111111
.\New-DevTestLabArtifactRepository.ps1 -LabName "mydevtestlab" -LabResourceGroupName "mydtlrg" -ArtifactRepositoryName "MyTeam Repository" -RepositoryUri "https://github.com/<myteam>/<nameofrepo>.git" -PersonalAccessToken "1111...." -SourceType "GitHub"

.NOTES
The script uses the current Azure context. To set the context, use Set-AzContext.

#>


[CmdletBinding()]
Param(

    [Parameter(Mandatory=$true)]
    $LabName,

    [Parameter(Mandatory=$true)]
    $LabResourceGroupName,
    $ArtifactRepositoryName,
    $ArtifactRepositoryDisplayName  = 'Team Artifact Repository',

    [Parameter(Mandatory=$true)]
    $RepositoryUri,
    $RepositoryBranch = 'main',
    $FolderPath = '/Artifacts',

    [Parameter(Mandatory=$true)]
    $PersonalAccessToken ,

    [Parameter(Mandatory=$true)]
    [ValidateSet('VsoGit', 'GitHub')]
    $SourceType
)

# Set artifact repository internal name if not specified.

if ($ArtifactRepositoryName -eq $null){
    $ArtifactRepositoryName = "PrivateRepo" + (Get-Random -Maximum 999)
}

# Sign in to Azure.
Connect-AzAccount


#Get Lab Resource.
$LabResource = Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs' -ResourceName $LabName -ResourceGroupName $LabResourceGroupName

Write-Verbose "Lab Name: $($LabResource.Name)"
Write-Verbose "Lab Resource Group Name: $($LabResource.ResourceGroupName)"
Write-Verbose "Lab Resource Location: $($LabResource.Location)"

Write-Verbose "Artifact Repository Internal Name: $ArtifactRepositoryName"

#Prepare properties object for the call to New-AzResource.
$propertiesObject = @{
    uri = $RepositoryUri;
    folderPath = $FolderPath;
    branchRef = $RepositoryBranch;
    displayName = $ArtifactRepositoryDisplayName;
    securityToken = $PersonalAccessToken;
    sourceType = $SourceType;
    status = 'Enabled'
}

Write-Verbose "Properties to be passed to New-AzResource:$($propertiesObject | Out-String)"

#Add resource to the current subscription.
$resourcetype = 'Microsoft.DevTestLab/labs/artifactSources'
$resourceName = $LabName + '/' + $ArtifactRepositoryName
Write-Verbose "Az ResourceType: $resourcetype"
Write-Verbose "Az ResourceName: $resourceName"

Write-Verbose "Creating artifact repository '$ArtifactRepositoryDisplayName'..."
$result = New-AzResource -Location $LabResource.Location -ResourceGroupName $LabResource.ResourceGroupName -properties $propertiesObject -ResourceType $resourcetype -ResourceName $resourceName -ApiVersion 2016-05-15 -Force

#Alternate implementation:
# Use resourceId rather than resourcetype and resourcename parameters.
# Using resourceId lets you specify the $SubscriptionId rather than using the
# subscription id of Get-AzContext.
#$resourceId = "/subscriptions/$SubscriptionId/resourceGroups/$($LabResource.ResourceGroupName)/providers/Microsoft.DevTestLab/labs/$LabName/artifactSources/$ArtifactRepositoryName"
#$result = New-AzResource -properties $propertiesObject -ResourceId $resourceId -ApiVersion 2016-05-15 -Force


# Check the result.
if ($result.Properties.ProvisioningState -eq "Succeeded") {
    Write-Verbose ("Successfully added artifact repository source '$ArtifactRepositoryDisplayName'")
}
else {
    Write-Error ("Error adding artifact repository source '$ArtifactRepositoryDisplayName'")
}

#Return the newly created resource to use in later scripts.
return $result

参数

该 PowerShell 脚本采用以下参数:

参数 描述
LabName 实验室的名称。
ArtifactRepositoryName 新项目存储库的名称。 该脚本为存储库创建一个随机名称(如果未指定)。
ArtifactRepositoryDisplayName 在实验室的项目存储库列表中显示的名称。
RepositoryUri 前面复制的项目存储库 URI。
RepositoryBranch 包含项目的存储库分支。 默认值为 main
FolderPath 包含项目的文件夹。 默认值为:/Artifacts
PersonalAccessToken 前面复制的、用于访问存储库的安全令牌。
SourceType 项目存储库是 VSOGit (Azure Repos) 还是 GitHub 存储库。

存储库需要一个内部标识名称,该名称与 Azure 门户中的显示名称不同。 使用 Azure 门户时看不到内部名称,但使用 Azure REST API 或 Azure PowerShell 时可以看到。 如果部署命令未指定名称,该脚本将创建一个随机名称。

#Set artifact repository name, if not set by user
if ($ArtifactRepositoryName -eq $null){
    $ArtifactRepositoryName = "PrivateRepo" + (Get-Random -Maximum 999)
}

PowerShell 命令

该脚本使用以下 PowerShell 命令:

Command 注释
Get-AzResource 获取有关实验室的详细信息,例如其位置。 在与实验室相同的位置以及资源组下创建项目存储库源。
New-AzResource 添加 Azure 资源。 没有用于添加项目存储库的特定命令。 此 cmdlet 需要通过 ResourceId,或者 ResourceNameResourceType 对来了解要创建的资源类型。 当前脚本使用 ResourceNameResourceType 对。

发现资源名称和资源类型信息的一个好办法是使用 Azure REST API 浏览器网站。 开发测试实验室项目源显示用于创建和管理开发测试实验室项目源的 REST API。 当前脚本使用以下资源 ID:

https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/artifactsources/{name}

资源类型是 URI 中 providers 之后列出的所有内容(大括号中的项除外)。 资源名称是大括号中列出的所有内容。 如果为资源名称使用多个项,请用斜杠分隔每个项:

$resourcetype = 'Microsoft.DevTestLab/labs/artifactSources'
$resourceName = $LabName + '/' + $ArtifactRepositoryName

运行 PowerShell 脚本

运行 PowerShell 脚本(请将 LabNameLabResourceGroupNameArtifactRepositoryNameRepositoryUriPersonalAccessTokenSourceType 中的示例值替换为你自己的值):

Set-AzContext -SubscriptionId <Your Azure subscription ID>

.\New-DevTestLabArtifactRepository.ps1 -LabName "mydevtestlab" -LabResourceGroupName "mydtlrg" -ArtifactRepositoryName "myteamrepository" -RepositoryUri "https://github.com/myteam/myteamrepository.git" - "1111...." -SourceType "GitHub"

后续步骤