教學課程:使用 Jenkins 和 Azure CLI 將應用程式部署至 Azure Spring Apps

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但在我們努力更新資產,例如螢幕快照、影片和圖表時,您會在某些地方看到舊名稱一段時間。

Azure Spring Apps 是完全受控的微服務開發,內建服務探索和組態管理。 此服務可讓您輕鬆地將 Spring Boot 型微服務應用程式部署至 Azure。 本教學課程示範如何在 Jenkins 中使用 Azure CLI,將 Azure Spring Apps 的持續整合和傳遞 (CI/CD) 自動化。

在本教學課程中,您將完成下列工作:

  • 布建服務實例並啟動Java Spring 應用程式
  • 準備 Jenkins 伺服器
  • 在 Jenkins 管線中使用 Azure CLI 來建置和部署微服務應用程式

必要條件

  • Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

布建服務實例並啟動Java Spring 應用程式

我們會使用 Piggy Metrics 作為範例 Microsoft 服務應用程式,並遵循 快速入門:使用 Azure CLI 啟動 Java Spring 應用程式,以布建服務實例並設定應用程式。 如果您已經完成相同的程式,您可以跳到下一節。 否則,下列包含 Azure CLI 命令。 請參閱快速入門:使用 Azure CLI 啟動 Java Spring 應用程式以取得詳細資訊。

您的本機計算機必須符合與 Jenkins 組建伺服器相同的必要條件。 請確定已安裝下列專案來建置和部署微服務應用程式:

  1. 安裝 Azure Spring Apps 擴充功能:

    az extension add --name spring
    
  2. 建立資源群組以包含您的 Azure Spring Apps 服務:

    az group create --location eastus --name <resource group name>
    
  3. 布建 Azure Spring Apps 的實例:

    az spring create -n <service name> -g <resource group name>
    
  4. Piggy 計量 存放庫派生至您自己的 GitHub 帳戶。 在本機電腦中,在名為 source-code的目錄中複製您的存放庫:

    mkdir source-code
    git clone https://github.com/<your GitHub id>/piggymetrics
    
  5. 設定組態伺服器。 請確定您將 GitHub 識別碼>取代<為正確的值。

    az spring config-server git set -n <your-service-name> --uri https://github.com/<your GitHub id>/piggymetrics --label config
    
  6. 建置專案:

    cd piggymetrics
    mvn clean package -D skipTests
    
  7. 建立三個微服務:閘道、驗證服務和帳戶服務:

    az spring app create --n gateway -s <service name> -g <resource group name>
    az spring app create --n auth-service -s <service name> -g <resource group name>
    az spring app create --n account-service -s <service name> -g <resource group name>
    
  8. 部署應用程式:

    az spring app deploy -n gateway -s <service name> -g <resource group name> --jar-path ./gateway/target/gateway.jar
    az spring app deploy -n account-service -s <service name> -g <resource group name> --jar-path ./account-service/target/account-service.jar
    az spring app deploy -n auth-service -s <service name> -g <resource group name> --jar-path ./auth-service/target/auth-service.jar
    
  9. 將公用端點指派給閘道:

    az spring app update -n gateway -s <service name> -g <resource group name> --is-public true
    
  10. 查詢閘道應用程式以取得 URL,以便確認應用程式正在執行。

    az spring app show --name gateway | grep url
    
  11. 流覽至上一個命令所提供的 URL,以執行 PiggyMetrics 應用程式。

準備 Jenkins 伺服器

在本節中,您會準備 Jenkins 伺服器來執行組建,這很適合用於測試。 不過,基於安全性影響,您應該使用 Azure VM 代理程式或 Azure 容器代理程式來啟動 Azure 中的代理程式來執行組建。

安裝外掛程式

  1. 登入您的 Jenkins 伺服器。

  2. 選取 [ 管理 Jenkins]。

  3. 選取 [ 管理外掛程式]。

  4. 在 [ 可用的] 索引標籤上,選取下列外掛程式:

    如果這些外掛程式未出現在清單中,請檢查 [ 已安裝 ] 索引標籤,以查看是否已安裝。

  5. 若要安裝外掛程式,請選取 [ 立即下載],然後在重新啟動后安裝。

  6. 重新啟動 Jenkins 伺服器以完成安裝。

在 Jenkins 認證存放區中新增您的 Azure 服務主體認證

  1. 您需要 Azure 服務主體才能部署至 Azure。 如需詳細資訊,請參閱部署至 Azure App 服務 教學課程中的建立服務主體一節。 的 az ad sp create-for-rbac 輸出看起來會像這樣:

    {
        "appId": "xxxxxx-xxx-xxxx-xxx-xxxxxxxxxxxx",
        "displayName": "xxxxxxxjenkinssp",
        "name": "http://xxxxxxxjenkinssp",
        "password": "xxxxxx-xxx-xxxx-xxx-xxxxxxxxxxxx",
        "tenant": "xxxxxx--xxx-xxxx-xxx-xxxxxxxxxxxx"
    }
    
  2. 在 Jenkins 儀錶板上,選取 [認證>系統]。 然後,選取 [全域認證][不受限制]。

  3. 選取 [ 新增認證]。

  4. 選取 [Microsoft Azure 服務主體 ] 作為 [種類]。

  5. 提供下列欄位的值:

    • 訂用帳戶識別碼:Azure 訂用帳戶識別碼
    • 用戶端標識碼:服務主體 appid
    • 用戶端密碼:服務主體密碼
    • 租用戶標識碼:Microsoft 帳戶租用戶標識碼
    • Azure 環境:為您的環境選取適當的值。 例如,針對 Azure Global 使用 Azure
    • 識別碼:設定為 azure_service_principal。 本文稍後的步驟會使用此標識符
    • 描述:這個值是選擇性的,但建議從文件/維護的觀點來看。

安裝 Maven 和 Azure CLI Spring 擴充功能

範例管線會使用 Maven 來建置和 Azure CLI 來部署至服務實例。 安裝 Jenkins 時,它會建立名為 jenkins 的系統管理員帳戶。 請確定使用者 jenkins 具有執行 spring 擴充功能的許可權。

  1. 連線 透過 SSH 連線到 Jenkins 控制器。

  2. 安裝 Maven。

    sudo apt-get install maven
    
  3. 輸入 az version來確認已安裝 Azure CLI。 如果未安裝 Azure CLI,請參閱 安裝 Azure CLI

  4. 切換至 jenkins 使用者:

    sudo su jenkins
    
  5. 安裝 spring 擴充功能:

    az extension add --name spring
    

建立 Jenkinsfile

  1. 在您自己的存放庫中 - https://github.com/your_github_id/piggymetrics 在根目錄中建立 Jenkinsfile

  2. 更新檔案,如下所示。 請務必取代資源組名>和服務<名稱>的值。< 如果您在 Jenkins 中新增認證時使用不同的值,請將azure_service_principal取代為正確的標識碼。

        node {
          stage('init') {
            checkout scm
          }
          stage('build') {
            sh 'mvn clean package'
          }
          stage('deploy') {
            withCredentials([azureServicePrincipal('azure_service_principal')]) {
              // Log in to Azure
              sh '''
                az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID
                az account set -s $AZURE_SUBSCRIPTION_ID
              '''  
              // Set default resource group name and service name. Replace <resource group name> and <service name> with the right values
              sh 'az config set defaults.group=<resource group name>'
              sh 'az config set defaults.spring=<service name>'
    
              // Deploy applications
              sh 'az spring app deploy -n gateway --jar-path ./gateway/target/gateway.jar'
              sh 'az spring app deploy -n account-service --jar-path ./account-service/target/account-service.jar'
              sh 'az spring app deploy -n auth-service --jar-path ./auth-service/target/auth-service.jar'
              sh 'az logout'
            }
          }
        }
    
  3. 儲存並認可變更。

建立作業

  1. 在 Jenkins 儀錶板上,選取 [ 新增專案]。

  2. 提供作業的名稱 Deploy-PiggyMetrics ,然後選取 [ 管線]。 按一下 [確定]。

  3. 選取 [管線] 索引 標籤

  4. 針對 [定義],從 SCM 選取 [管線腳本]。

  5. 針對 [SCM],選取 [Git]。

  6. 輸入分支存放庫的 GitHub URL: https://github.com/&lt;your GitHub id&gt;/piggymetrics.git

  7. 針對 [分支規範] [黑色],選取 [/Azure]。

  8. 針對 [ 腳本路徑],選取 [Jenkinsfile]。

  9. 選取儲存

驗證並執行作業

在執行作業之前,請先編輯登入輸入方塊中的文字,以 輸入登入標識符

  1. 在您的存放庫中,於 中/gateway/src/main/resources/static/開啟 index.html

  2. enter your login搜尋並將該文字更新為 enter login ID

    <input class="frontforms" id="frontloginform" name="username" placeholder="enter login ID" type="text" autocomplete="off"/>
    
  3. 儲存並認可變更。

  4. 手動在 Jenkins 中執行作業。 在 Jenkins 儀錶板上,選取作業 Deploy-PiggyMetrics ,然後選取 [ 立即建置]。

作業完成之後,流覽至應用程式的公用IP gateway ,並確認您的應用程式已更新。

Updated Piggy Metrics

清除資源

不再需要時,請刪除本文中建立的資源:

az group delete -y --no-wait -n <resource group name>

下一步