Azure:如何使用 VSTS 發(fā)布 Web 應用

來源: Microsoft
作者:Microsoft
時間:2021-03-10
17554
本文展示了如何構建VSTS與中國Azure賬戶之間的連接。開發(fā)人員在Github上集成后,編譯并測試通過,再發(fā)布到Azure Web應用上,期望的是持續(xù)集成持續(xù)發(fā)布,也就是CICD。我們可以通過Visual Studio Online(簡稱VSO),與Azure服務的集成,使用非常方便。

本文展示了如何構建VSTS與中國Azure賬戶之間的連接。開發(fā)人員在Github上集成后,編譯并測試通過,再發(fā)布到Azure Web應用上,期望的是持續(xù)集成持續(xù)發(fā)布,也就是CICD。我們可以通過Visual Studio Online(簡稱VSO),與Azure服務的集成,使用非常方便。

具體步驟如下:

1.登陸VSO并創(chuàng)建VSTS項目。

2.構建VSTS與中國Azure賬戶之間的連接。

a.首先調用Powershell腳本來注冊當前VSTS服務到AzureAD里面,并授予它相應Azure資源的Contributor權限,比如資源組或者整個訂閱,當然也可以是某個資源。

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]'

備注

第一個參數subscriptionName是訂閱名稱,password是密碼。注意這個密碼不一定是AzureChina登錄的密碼,我們可以設置為其他的密碼。請牢記這個密碼,我們會在后續(xù)步驟中使用,resourceGroupName是資源組名稱。

01 (1).png

d.登錄VSTS,在創(chuàng)建的項目的主頁上,點擊設置->Services->New Service Endpoint->Azure Resource Manager。

02 (1).png

3.在彈出的對話框里,點擊文字鏈接“use the full version of the endpoint dialog.”,會出現如下對話框,輸入前面腳本的輸出值,然后可以點擊“Verify connection”驗證連接。

03 (1).png

04 (1).png

此時創(chuàng)建好連接之后,便可以進行持續(xù)發(fā)布了。

4.發(fā)布Azure Web應用。

在build and release標簽頁下的Builds選項中選擇template為ASP.net(如果項目為.netcore選擇Asp.net core)。

05 (1).png

在Releases選項中選擇Azure App Service Deployment即可成功發(fā)布。

06 (1).png

07 (1).png

08 (1).png

或者在build選項中選擇template為Azure Web App for ASP.NET,直接進行發(fā)布。

09 (1).png

立即登錄,閱讀全文
版權說明:
本文內容來自于Microsoft,本站不擁有所有權,不承擔相關法律責任。文章內容系作者個人觀點,不代表快出海對觀點贊同或支持。如有侵權,請聯系管理員(zzx@kchuhai.com)刪除!
優(yōu)質服務商推薦
更多
掃碼登錄
打開掃一掃, 關注公眾號后即可登錄/注冊
加載中
二維碼已失效 請重試
刷新
賬號登錄/注冊
小程序
快出海小程序
公眾號
快出海公眾號
商務合作
商務合作
投稿采訪
投稿采訪
出海管家
出海管家