共用方式為


將成品存放庫新增至實驗室

本文說明如何在 Azure DevTest Labs 中將「成品」存放庫新增至實驗室。 成品是在虛擬機器 (VM) 上安裝的工具或應用程式。 您可以在從 GitHub 或 Azure Repos Git 存放庫載入的 JSON 檔案中定義成品。

公用 DevTest Labs GitHub 成品存放庫 提供許多適用於 Windows 與 Linux 的常見成品。 根據預設,此公用存放庫中的成品可在 DevTest Labs 中使用。 如需將成品新增至 VM 的資訊,請參閱將成品新增至 DevTest Labs VM

您也可以建立無法在公用成品存放庫中使用的自訂成品。 若要了解如何建立自訂成品,請參閱建立自訂成品。 您可以將自訂成品新增至您自己的成品存放庫,並將存放庫新增至實驗室,讓所有實驗室使用者都可以使用成品。

本文示範如何使用 Azure 入口網站、Azure Resource Management (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. 在 [New personal access token] \(新增個人存取權杖\) 頁面的 [注意事項] 底下,輸入權杖的選擇性描述。 接受所有預設值,然後選取 [產生權杖]
  6. 儲存產生的權杖。

取得 Azure Repos 的複製 URL 與個人存取權杖

  1. 在具有成品的存放庫主頁面上,選取 [複製]。 在 [Clone Repository] \(複製存放庫\) 頁面上,複製複製 URL。
  2. 在 Azure DevOps 頁面右上角,依序選取 [使用者設定]>[個人存取權杖]
  3. 在 [個人存取權杖] 頁面上,選取 [新增權杖]
  4. 填寫權杖的資訊,針對範圍選取 [讀取],然後選取 [建立]
  5. 請務必在 [成功] 頁面上複製權杖,因為 Azure Repos 不會儲存權杖或再次顯示權杖。

將成品存放庫新增至 Azure 入口網站中的實驗室

  1. 在實驗室的 [概觀] 頁面上,從左側導覽中選取 [設定和原則]

  2. 在 [設定和原則] 頁面上,選取左側導覽中 [外部資源] 底下的 [存放庫]

    在 [存放庫] 頁面上,[Public Artifact Repo] \(公用成品存放庫\) 會自動呈現,並連線至 DevTest Labs 公用 GitHub 存放庫。 若實驗室未啟用此存放庫,請選取 [Public Artifact Repo] \(公用成品存放庫\) 旁的核取方塊,然後選取頂端功能表列上的 [啟用],即可啟用該存放庫。

  3. 若要將成品存放庫新增至實驗室,請選取頂端功能表列中的 [新增]

    顯示 [存放庫設定] 畫面的螢幕快照。

  4. 在 [存放庫] 窗格中,輸入下列資訊:

    • 名稱:要在實驗室中使用的存放庫名稱。
    • Git 複製 URL:來自 GitHub 或 Azure Repos 的 Git HTTPS 複製 URL。
    • 分支 (選擇性):具有成品定義的分支。
    • 個人存取權杖:來自 GitHub 或 Azure Repos 的個人存取權杖。
    • 資料夾路徑:ARM 範本定義的資料夾,相對於 Git 複製 URL。 請務必在資料夾路徑中包括初始斜線。
  5. 選取 [儲存]。

    顯示將新成品存放庫新增至實驗室的螢幕快照。

存放庫現在會出現在實驗室的存放庫清單中。

使用 ARM 範本新增成品存放庫

ARM 範本是 JSON 檔案,描述要建立的 Azure 資源。 如需 ARM 範本的詳細資訊,請參閱了解 ARM 範本的結構與語法

下列 ARM 範本會將成品存放庫新增至實驗室。 若實驗室尚未存在,範本就會建立實驗室。

檢閱 ARM 範本

範例範本會收集下列參數資訊。 某些參數具有預設值,但部署命令必須指定實驗室名稱、成品存放庫 URI、存放庫類型,以及存放庫個人存取權杖。

  • 實驗室名稱。
  • DevTest Labs 中成品存放庫的顯示名稱。 預設值是 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 需要 ResourceIdResourceNameResourceType 配對,才能知道要建立的資源類型。 目前的指令碼會使用 ResourceNameResourceType 配對。

探索資源名稱與資源類型資訊的好方式是使用 Azure REST API 瀏覽器網站。 DevTest Labs 成品來源會顯示用於建立及管理 DevTest Labs 成品來源的 REST API。 目前的指令碼會使用下列資源識別碼:

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"

下一步