本文展示了如何構(gòu)建VSTS與中國Azure賬戶之間的連接。開發(fā)人員在Github上集成后,編譯并測試通過,再發(fā)布到Azure Web應(yīng)用上,期望的是持續(xù)集成持續(xù)發(fā)布,也就是CICD。我們可以通過Visual Studio Online(簡稱VSO),與Azure服務(wù)的集成,使用非常方便。
具體步驟如下:
1.登陸VSO并創(chuàng)建VSTS項目。
2.構(gòu)建VSTS與中國Azure賬戶之間的連接。
a.首先調(diào)用Powershell腳本來注冊當前VSTS服務(wù)到AzureAD里面,并授予它相應(yīng)Azure資源的Contributor權(quán)限,比如資源組或者整個訂閱,當然也可以是某個資源。
b.安裝Azure PowerShell,并將以下PowerShell腳本保存為:RegisterVstsMooncake.ps1。
PowerShell
param
(
[Parameter(Mandatory=$true,HelpMessage="Enter Azure Subscription name.You need to be Subscription Admin to execute the script")]
[string]$subscriptionName,
[Parameter(Mandatory=$true,HelpMessage="Provide a password for SPN application that you would create")]
[string]$password,
[Parameter(Mandatory=$false,HelpMessage="Provide a SPN role assignment")]
[string]$spnRole="contributor",
[Parameter(Mandatory=$false,HelpMessage="If assign role for target resource group,provide its name")]
[string]$resourceGroupName
)
#Initialize
$ErrorActionPreference="Stop"
$VerbosePreference="SilentlyContinue"
$userName=$env:USERNAME
$newguid=[guid]::NewGuid()
$displayName=[String]::Format("VSO.{0}.{1}",$userName,$newguid)
$homePage="http://"+$displayName
$identifierUri=$homePage
#Initialize subscription
$isAzureModulePresent=Get-Module-Name AzureRM*-ListAvailable
if([String]::IsNullOrEmpty($isAzureModulePresent)-eq$true)
{
Write-Output"Script requires AzureRM modules to be present.Obtain AzureRM from https://github.com/Azure/azure-powershell/releases.Please refer https://github.com/Microsoft/vsts-tasks/blob/master/Tasks/DeployAzureResourceGroup/README.md for recommended AzureRM versions."-Verbose
return
}
Import-Module-Name AzureRM.Profile
Write-Output"Provide your credentials to access Azure subscription$subscriptionName"-Verbose
Login-AzureRmAccount-SubscriptionName$subscriptionName-Environment'AzureChinaCloud'
$azureSubscription=Get-AzureRmSubscription-SubscriptionName$subscriptionName
$connectionName=$azureSubscription.Name
$tenantId=$azureSubscription.TenantId
$id=$azureSubscription.Id
$securePASS=ConvertTo-SecureString-String$password-AsPlainText-Force
#Create a new AD Application
Write-Output"Creating a new Application in AAD(App URI-$identifierUri)"-Verbose
$azureAdApplication=New-AzureRmADApplication-DisplayName$displayName-HomePage$homePage-IdentifierUris$identifierUri-Password$securePASS-Verbose
$appId=$azureAdApplication.ApplicationId
Write-Output"Azure AAD Application creation completed successfully(Application Id:$appId)"-Verbose
#Create new SPN
Write-Output"Creating a new SPN"-Verbose
$spn=New-AzureRmADServicePrincipal-ApplicationId$appId
$spnName=$spn.ServicePrincipalNames[0]
Write-Output"SPN creation completed successfully(SPN Name:$spnName)"-Verbose
#Assign role to SPN
Write-Output"Waiting for SPN creation to reflect in Directory before Role assignment"
Start-Sleep 20
if([String]::IsNullOrEmpty($resourceGroupName)-eq$true)
{
Write-Output"Assigning role($spnRole)to SPN App($appId)"-Verbose
New-AzureRmRoleAssignment-RoleDefinitionName$spnRole-ServicePrincipalName$appId
}
else
{
Write-Output"Assigning role($spnRole)to SPN App($appId)for resource group($resourceGroupName)"-Verbose
New-AzureRmRoleAssignment-ResourceGroupName$resourceGroupName-RoleDefinitionName$spnRole-ServicePrincipalName$appId
}
Write-Output"SPN role assignment completed successfully"-Verbose
#Print the values
Write-Output"`nCopy and Paste below values for Service Connection"-Verbose
Write-Output"***************************************************************************"
Write-Output"Connection Name:$connectionName(SPN)"
Write-Output"Subscription Id:$id"
Write-Output"Subscription Name:$connectionName"
Write-Output"Service Principal Id:$appId"
Write-Output"Service Principal key:<Password that you typed in>"
Write-Output"Tenant Id:$tenantId"
Write-Output"***************************************************************************"
c.執(zhí)行以下命令:
PowerShell
.RegisterVsts2Mooncake.ps1-subscriptionName'[YourAzureChinaSubscriptionName]'-password'[YourPassword]'-resourceGroupName'[YourAzureChinaResourceGroup]'
備注
第一個參數(shù)subscriptionName是訂閱名稱,password是密碼。注意這個密碼不一定是AzureChina登錄的密碼,我們可以設(shè)置為其他的密碼。請牢記這個密碼,我們會在后續(xù)步驟中使用,resourceGroupName是資源組名稱。
d.登錄VSTS,在創(chuàng)建的項目的主頁上,點擊設(shè)置->Services->New Service Endpoint->Azure Resource Manager。
3.在彈出的對話框里,點擊文字鏈接“use the full version of the endpoint dialog.”,會出現(xiàn)如下對話框,輸入前面腳本的輸出值,然后可以點擊“Verify connection”驗證連接。
此時創(chuàng)建好連接之后,便可以進行持續(xù)發(fā)布了。
4.發(fā)布Azure Web應(yīng)用。
在build and release標簽頁下的Builds選項中選擇template為ASP.net(如果項目為.netcore選擇Asp.net core)。
在Releases選項中選擇Azure App Service Deployment即可成功發(fā)布。
或者在build選項中選擇template為Azure Web App for ASP.NET,直接進行發(fā)布。