使用 Azure Pipelines 在 Azure Government 中部署應用程式

本操作指南協助您使用 Azure Pipelines 設定在 Azure Government 中執行的 Web 應用程式持續整合 (CI) 和持續傳遞 (CD)。 CI/CD 會自動化存放庫中的程式碼建置,以及將建置的程式碼成品部署 (發行) 到 Azure Government 中的某個服務或一組服務。 在本操作指南中,您將建置 Web 應用程式並部署到 Azure Governments App Service。 建置和發行程序是由存放庫中的程式碼檔案變更所觸發。

注意

Azure DevOps 無法在 Azure Government 上使用。 雖然本操作指南示範如何設定 Azure Pipelines 的 CI/CD 功能,將應用程式部署至 Azure Government 內的服務,但請注意:Azure Pipelines 會在 Azure Government 外部執行其管線。 使用它作為部署工具之前,請先研究組織的安全性和服務原則。 如需使用 Azure DevOps Server 在 Azure Government 的私人網路中建立 DevOps 體驗的操作指引,請參閱 Azure Government 上的 Azure DevOps Server

開發小組會使用 Azure Pipelines 來設定 Azure 訂用帳戶中已裝載應用程式的持續部署。 我們可以定義 Azure Government 的服務連線,將此服務用於 Azure Government 中執行的應用程式。

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

必要條件

開始本操作指南之前,您必須先完成下列必要條件:

如果您沒有使用中的 Azure Government 訂用帳戶,請在開始前建立免費帳戶

建立 Azure Government App Service 應用程式

請遵循教學課程:部署 Azure App Service 應用程式,以瞭解如何將 Azure App Service 應用程式部署至 Azure Government。 下列步驟會設定 CD 程序以部署至您的 Web 應用程式。

設定組建和原始檔控制整合

檢閱下列其中一個快速入門,為您的特定類型應用程式設定組建:

產生服務主體

  1. 複製下列服務主體「建立 PowerShell 指令碼」並貼到 IDE 或編輯器,然後儲存指令碼。 此程式碼僅與 Azure Az PowerShell v7.0.0 或更高版本相容。

    param
    (
        [Parameter(Mandatory=$true, HelpMessage="Enter Azure subscription name - you need to be subscription admin to execute the script")]
        [string] $subscriptionName,
    
        [Parameter(Mandatory=$false, HelpMessage="Provide SPN role assignment")]
        [string] $spnRole = "owner",
    
        [Parameter(Mandatory=$false, HelpMessage="Provide Azure environment name for your subscription")]
        [string] $environmentName = "AzureUSGovernment"
    )
    
    # Initialize
    $ErrorActionPreference = "Stop"
    $VerbosePreference = "SilentlyContinue"
    $userName = ($env:USERNAME).Replace(' ', '')
    $newguid = [guid]::NewGuid()
    $displayName = [String]::Format("AzDevOps.{0}.{1}", $userName, $newguid)
    $homePage = "http://" + $displayName
    $identifierUri = $homePage
    
    # Check for Azure Az PowerShell module
    $isAzureModulePresent = Get-Module -Name Az -ListAvailable
    if ([String]::IsNullOrEmpty($isAzureModulePresent) -eq $true)
    {
        Write-Output "Script requires Azure PowerShell modules to be present. Obtain Azure PowerShell from https://learn.microsoft.com//powershell/azure/install-az-ps" -Verbose
        return
    }
    
    Import-Module -Name Az.Accounts
    Write-Output "Provide your credentials to access your Azure subscription $subscriptionName" -Verbose
    Connect-AzAccount -Subscription $subscriptionName -Environment $environmentName
    $azureSubscription = Get-AzSubscription -SubscriptionName $subscriptionName
    $connectionName = $azureSubscription.Name
    $tenantId = $azureSubscription.TenantId
    $id = $azureSubscription.SubscriptionId
    
    # Create new Azure AD application
    Write-Output "Creating new application in Azure AD (App URI - $identifierUri)" -Verbose
    $azureAdApplication = New-AzADApplication -DisplayName $displayName -HomePage $homePage -Verbose
    $appId = $azureAdApplication.AppId
    $objectId = $azureAdApplication.Id
    Write-Output "Azure AD application creation completed successfully (Application Id: $appId) and (Object Id: $objectId)" -Verbose
    
    # Add secret to Azure AD application
    Write-Output "Creating new secret for Azure AD application"
    $secret = New-AzADAppCredential -ObjectId $objectId -EndDate (Get-Date).AddYears(2)
    Write-Output "Secret created successfully" -Verbose
    
    # Create new SPN
    Write-Output "Creating new SPN" -Verbose
    $spn = New-AzADServicePrincipal -ApplicationId $appId
    $spnName = $spn.DisplayName
    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
    Write-Output "Assigning role ($spnRole) to SPN app ($appId)" -Verbose
    New-AzRoleAssignment -RoleDefinitionName $spnRole -ApplicationId $spn.AppId
    Write-Output "SPN role assignment completed successfully" -Verbose
    
    # Print values
    Write-Output "`nCopy and paste below values for service connection" -Verbose
    Write-Output "***************************************************************************"
    Write-Output "Connection Name: $connectionName(SPN)"
    Write-Output "Environment: $environmentName"
    Write-Output "Subscription Id: $id"
    Write-Output "Subscription Name: $connectionName"
    Write-Output "Service Principal Id: $appId"
    Write-Output "Tenant Id: $tenantId"
    Write-Output "***************************************************************************"
    
  2. 開啟 PowerShell 視窗並執行下列命令,以設定啟用執行本機檔案的原則:

    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

    當系統詢問您是否要變更執行原則時,請輸入 "A" (代表「全部皆是」)。

  3. 導覽至服務主體「建立 PowerShell 指令碼」的儲存目錄。

  4. 使用指令碼名稱編輯下列命令後執行:

    ./<name of script file you saved>

  5. 如需 "subscriptionName" 參數,請透過 Connect-AzAccount -EnvironmentName AzureUSGovernment 登入 Azure Government 訂用帳戶後執行 Get-AzureSubscription

  6. 提供您的 Azure Government 訂用帳戶認證後,應該會看到下列訊息:

    The Environment variable should be AzureUSGovernment

  7. 執行指令碼之後,應該會看到您的服務連線值。 複製這些值,因為稍後設定端點時會用到這些值。

    Service connection values displayed after running the PowerShell script.

設定 Azure Pipelines 服務連線

請遵循管理服務連線來設定 Azure Pipelines 服務連線。

對 Azure Government 進行一項特定變更:

定義發行程序

請遵循部署 Azure Web 應用程式指示,設定發行管線並在 Azure Government 中部署應用程式。

Q&A

我需要組建代理程式嗎?
您需要至少一個代理程式來執行部署。 根據預設,組建和部署程序會設定為使用裝載的代理程式。 設定私人代理程式會限制 Azure Government 外部的資料共用。

是否可以在 Azure DevOps Server (之前稱為 Team Foundation Server) 上設定 CD,鎖定 Azure Government 為目標?
您可以在 Azure Government 中設定 Azure DevOps Server。 如需使用 Azure DevOps Server 在 Azure Government 的私人網路中建立 DevOps 體驗的操作指引,請參閱 Azure Government 上的 Azure DevOps Server

下一步

如需詳細資訊,請參閱以下資源: