驗證 Azure App Service 的部署

已完成

為了完成本課程模組中的練習,您已登入沙箱環境。 因為此環境是互動式環境,所以所有部署都已使用初始化沙箱時所使用的認證來進行驗證。 不過,如果要將建置程序自動化,則部署不會使用此互動式環境。 在自動化案例中,您必須將專案設定為使用其中一種支援的驗證方法。

在本單元中,您將了解貴公司如何將 Maven 設定為使用 Azure 驗證。

驗證 Web 應用程式

Azure 可供彈性地決定要如何驗證應用程式。 您所選選項取決於貴公司的組建環境。 以下是使用 Maven 驗證應用程式程式碼的三個選項,以複雜度的順序列出 (從最低到最高):

  • 使用 Azure CLI 進行驗證,或使用 Azure 入口網站上的 Cloud Shell。

  • 建立 Azure 服務主體,使用服務主體認證建立 JSON 檔案,然後修改專案的 pom.xml 檔案以使用 JSON 檔案。

  • 建立 Azure 服務主體,將服務主體認證新增至 Maven settings.xml 檔案,然後修改專案的 pom.xml 檔案以使用 Maven 設定。

Microsoft 建議使用第三個選項,因為該選項提供最可靠、彈性且一致的驗證方法。 在實際設定中,貴公司現有 Java Web 應用程式可能會在未安裝 Azure CLI 工具的本機伺服器上執行。 考慮到這一點,您可能會實作建議,以便使用服務主體和 Maven settings.xml 檔案來新增驗證。 不過,在本練習中,沙箱沒有足夠的權限來建立服務主體。

使用 Azure CLI 進行驗證

驗證 Maven 最簡單的方式,就是使用 Azure CLI 登入。 適用於 Azure App Service 的 Maven 外掛程式可以接著使用認證來部署應用程式,而不需要進行額外的設定。

如果您使用的是 Azure Cloud Shell,就如同在本課程模組中使用 Microsoft Learn 沙箱完成練習時一樣,依預設您會登入 Azure,而不需要執行任何其他命令。 不過,如果您是從另一部電腦使用 Azure CLI,則必須使用 az login 命令登入。

使用驗證檔案進行驗證

驗證 Web 應用程式的第二種方法牽涉到建立 Azure 服務主體,並將服務主體認證儲存到將從專案設定參考的檔案。

若要使用 Azure CLI 建立 Azure 服務主體,請使用下列步驟。

  1. 從 Azure CLI 執行下列命令,以建立 Azure 服務主體:

    az ad sp create-for-rbac --name https://mywebapp-1234567890.azurewebsites.net/ --role Contributor --scopes /subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss
    

    其中,https://mywebapp-1234567890.azurewebsites.net/ 是 Web 應用程式的 URL。

    此命令會傳回具有 JSON 物件的回應,類似於下列範例:

    Creating a role assignment under the scope of "/subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss"
    
    {
      "appId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
      "displayName": "mywebapp-1234567890.azurewebsites.net/",
      "name": "https://mywebapp-1234567890.azurewebsites.net/",
      "password": "pppppppp-pppp-pppp-pppp-pppppppppppp",
      "tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt"
    }
    
  2. 使用來自 az ad sp create-for-rbac 回應的資訊,以服務主體資訊建立驗證檔案。

    1. 使用程式碼編輯器建立新的 JSON 檔案:

      cd ~/MyWebApp
      code auth.json
      
    2. 新增下列 JSON 語法:

      {
        "clientId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
        "clientSecret": "pppppppp-pppp-pppp-pppp-pppppppppppp",
        "subscriptionId": "ssssssss-ssss-ssss-ssss-ssssssssssss",
        "tenantId": "tttttttt-tttt-tttt-tttt-tttttttttttt"
      }
      

      其中:

      參數 描述
      clientId 指定服務主體的 appId
      clientSecret 指定服務主體的 password
      subscriptionId 指定 Azure 訂用帳戶,這是由 az ad sp create-for-rbac 命令所傳回
      tenantId 指定服務主體的 tenant
    3. 鍵入 Ctrl+S 來儲存變更。

    4. 輸入 Ctrl+Q 來結束程式碼編輯器。

  3. 修改 Web 應用程式的 pom.xml 檔案,以參考驗證檔案。

    1. 使用程式碼編輯器開啟 pom.xml 檔案:

      cd ~/MyWebApp
      code pom.xml
      
    2. 尋找 azure-webapp-maven-plugin<configuration> 區段。

    3. 在包含 <region> 項目的那一行後面新增下列 XML:

      <authentication>
        <file>/absolute/path/to/auth.json</file>
      </authentication>
      

      其中,/absolute/path/to/auth.json 是稍早所建立 auth.json 檔案的完整路徑。

      azure-webapp-maven-plugin 區段現在應該類似於下列範例:

      <plugin> 
        <groupId>com.microsoft.azure</groupId>  
        <artifactId>azure-webapp-maven-plugin</artifactId>  
        <version>1.9.0</version>  
        <configuration> 
          <schemaVersion>V2</schemaVersion>  
          <resourceGroup>maven-publish</resourceGroup>  
          <appName>MyWebApp-1234567890</appName>  
          <pricingTier>F1</pricingTier>  
          <region>centralus</region>
          <authentication>
            <file>/home/username/MyWebApp/auth.json</file>
          </authentication>
          <runtime> 
            <os>linux</os>  
            <javaVersion>jre8</javaVersion>  
            <webContainer>TOMCAT 8.5</webContainer> 
          </runtime>  
          <deployment> 
            <resources> 
              <resource> 
                <directory>${project.basedir}/target</directory>  
                <includes> 
                  <include>*.war</include> 
                </includes> 
              </resource> 
            </resources> 
          </deployment> 
        </configuration> 
      </plugin> 
      
    4. 鍵入 Ctrl+S 來儲存變更。

    5. 輸入 Ctrl+Q 來結束程式碼編輯器。

  4. 使用 Maven 建置 Web 應用程式,並將其部署至 Azure App Service:

    mvn azure-webapp:deploy
    

    Maven 會顯示一系列的建置訊息,且最終訊息應會指出成功部署至 Azure:

    [INFO] ------------------------------------------------------------------------
    [INFO] Building MyWebApp Maven Webapp 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- azure-webapp-maven-plugin:1.9.0:deploy (default-cli) @ MyWebApp ---
    [INFO] Authenticate with file: /home/username/MyWebApp/auth.json
    [INFO] [Correlation ID: 12345678-1234-1234-1234-123456789abc] Instance discovery was successful
    [INFO] Updating app service plan
    [INFO] Updating target Web App...
    [INFO] Successfully updated Web App.
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 1 resource to /home/robert/MyWebApp/target/azure-webapp/MyWebApp-1234567890
    [INFO] Trying to deploy artifact to MyWebApp-1234567890...
    [INFO] Deploying the war file MyWebApp.war...
    [INFO] Successfully deployed the artifact to https://mywebapp-1234567890.azurewebsites.net
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 28.759 s
    [INFO] Finished at: 2020-02-12T21:12:00+00:00
    [INFO] Final Memory: 43M/286M
    [INFO] ------------------------------------------------------------------------
    

    回應中的 Authenticate with file: /home/username/MyWebApp/auth.json 行指出驗證檔案是用來將 Web 應用程式發佈到 Azure。

使用 Maven settings.xml 檔案進行驗證

驗證 Web 應用程式的第三種方法包括建立 Azure 服務主體、建立包含服務主體認證的 Maven settings.xml 檔案,以及修改專案的 pom.xml 檔案以使用 Maven 設定。

使用 Azure CLI 建立 Azure 服務主體的步驟,與本單元上一節中的步驟相同。

  1. 從 Azure CLI 執行下列命令,以建立 Azure 服務主體:

    az ad sp create-for-rbac --name https://mywebapp-1234567890.azurewebsites.net/ --role Contributor --scopes /subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss
    

    其中,https://mywebapp-1234567890.azurewebsites.net/ 是 Web 應用程式的 URL。

    此命令會傳回具有 JSON 物件的回應,類似於下列範例:

    Creating a role assignment under the scope of "/subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss"
    
    {
      "appId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
      "displayName": "mywebapp-1234567890.azurewebsites.net/",
      "name": "https://mywebapp-1234567890.azurewebsites.net/",
      "password": "pppppppp-pppp-pppp-pppp-pppppppppppp",
      "tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt"
    }
    
  2. 建立 settings.xml 檔案的使用者版本,以供 Maven 使用。

    1. 使用程式碼編輯器,為 Maven 設定建立新的 XML 檔案:

      code ~/.m2/settings.xml
      
    2. 將下列 XML 貼入該檔案:

      <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
        <servers>
          <server>
             <id>azure-auth</id>
             <configuration>
                 <client>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</client>
                 <tenant>tttttttt-tttt-tttt-tttt-tttttttttttt</tenant>
                 <key>pppppppp-pppp-pppp-pppp-pppppppppppp</key>
             </configuration>
          </server>
        </servers>
      </settings>
      

      其中:

      參數 描述
      client 指定服務主體的 appId
      key 指定服務主體的 password
      tenant 指定服務主體的 tenant
    3. 鍵入 Ctrl+S 來儲存變更。

    4. 輸入 Ctrl+Q 來結束程式碼編輯器。

  3. 修改 Web 應用程式的 pom.xml 檔案,以參考驗證檔案。

    1. 使用程式碼編輯器開啟 pom.xml 檔案:

      cd ~/MyWebApp
      code pom.xml
      
    2. 尋找 azure-webapp-maven-plugin<configuration> 區段。

    3. 在包含 <region> 項目的那一行後面新增下列 XML:

      <authentication>
        <serverId>azure-auth</serverId>
      </authentication>
      

      azure-webapp-maven-plugin 區段現在應該類似於下列範例:

      <plugin> 
        <groupId>com.microsoft.azure</groupId>  
        <artifactId>azure-webapp-maven-plugin</artifactId>  
        <version>1.9.0</version>  
        <configuration> 
          <schemaVersion>V2</schemaVersion>  
          <resourceGroup>maven-publish</resourceGroup>  
          <appName>MyWebApp-1234567890</appName>  
          <pricingTier>F1</pricingTier>  
          <region>centralus</region>
          <authentication>
            <serverId>azure-auth</serverId>
          </authentication>
          <runtime> 
            <os>linux</os>  
            <javaVersion>jre8</javaVersion>  
            <webContainer>TOMCAT 8.5</webContainer> 
          </runtime>  
          <deployment> 
            <resources> 
              <resource> 
                <directory>${project.basedir}/target</directory>  
                <includes> 
                  <include>*.war</include> 
                </includes> 
              </resource> 
            </resources> 
          </deployment> 
        </configuration> 
      </plugin> 
      
    4. 鍵入 Ctrl+S 來儲存變更。

    5. 輸入 Ctrl+Q 來結束程式碼編輯器。

  4. 使用 Maven 建置 Web 應用程式,並將其部署至 Azure App Service:

    mvn azure-webapp:deploy
    

    Maven 會顯示一系列的建置訊息,且最終訊息應會指出成功部署至 Azure:

    [INFO] ------------------------------------------------------------------------
    [INFO] Building MyWebApp Maven Webapp 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- azure-webapp-maven-plugin:1.9.0:deploy (default-cli) @ MyWebApp ---
    [INFO] Authenticate with ServerId: azure-auth
    [INFO] [Correlation ID: 12345678-1234-1234-1234-123456789abc] Instance discovery was successful
    [INFO] Updating app service plan
    [INFO] Updating target Web App...
    [INFO] Successfully updated Web App.
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 1 resource to /home/robert/MyWebApp/target/azure-webapp/MyWebApp-1234567890
    [INFO] Trying to deploy artifact to MyWebApp-1234567890...
    [INFO] Deploying the war file MyWebApp.war...
    [INFO] Successfully deployed the artifact to https://mywebapp-1234567890.azurewebsites.net
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 21.648 s
    [INFO] Finished at: 2020-02-12T21:12:00+00:00
    [INFO] Final Memory: 43M/300M
    [INFO] ------------------------------------------------------------------------
    

    回應中的 Authenticate with ServerId: azure-auth 行指出服務主體認證是用來將 Web 應用程式發佈到 Azure。

檢定您的知識

1.

使用適用於 Azure App Service 的 Maven 外掛程式時,不支援下列哪一種驗證部署的方法?

2.

是非題:您可以將建立 Azure 服務主體時傳回的 JSON 儲存到檔案,然後使用該檔案針對適用於 Azure App Service 的 Maven 外掛程式進行驗證。

3.

建議使用下列哪一種方法,以適用於 Azure App Service 的 Maven 外掛程式驗證部署?