教學課程:使用 Jenkins 和 Azure DevOps Services 在 Azure 中將應用程式部署至 Linux 虛擬機器Tutorial: Deploy your app to Linux virtual machines in Azure with using Jenkins and Azure DevOps Services

持續整合 (CI) 與持續部署 (CD) 形成一個您可以建置、發行和部署程式碼的管道。Continuous integration (CI) and continuous deployment (CD) form a pipeline by which you can build, release, and deploy your code. Azure DevOps Services 提供了一組完整且功能齊全的 CI/CD 自動化工具以便部署至 Azure。Azure DevOps Services provides a complete, fully featured set of CI/CD automation tools for deployment to Azure. Jenkins 是廣為使用的第三方 CI/CD 伺服器型工具,也提供 CI/CD 自動化。Jenkins is a popular third-party CI/CD server-based tool that also provides CI/CD automation. 您可以同時使用 Azure DevOps Services 和 Jenkins 來自訂雲端應用程式或服務的傳遞方式。You can use Azure DevOps Services and Jenkins together to customize how you deliver your cloud app or service.

在本教學課程中,您可以使用 Jenkins 來建置 Node.js Web 應用程式。In this tutorial, you use Jenkins to build a Node.js web app. 然後,使用 Azure DevOps 將其部署至You then use Azure DevOps to deploy it

包含 Linux 虛擬機器 (VM) 的部署群組to a deployment group that contains Linux virtual machines (VMs). 您會了解如何:You learn how to:

  • 取得範例應用程式。Get the sample app.
  • 設定 Jenkins 外掛程式。Configure Jenkins plug-ins.
  • 設定適用於 Node.js 的 Jenkins 自由樣式專案。Configure a Jenkins Freestyle project for Node.js.
  • 設定適用於 Azure DevOps Services 整合的 Jenkins。Configure Jenkins for Azure DevOps Services integration.
  • 建立 Jenkins 服務端點。Create a Jenkins service endpoint.
  • 建立 Azure 虛擬機器的部署群組。Create a deployment group for the Azure virtual machines.
  • 建立 Azure Pipelines 發行管線。Create an Azure Pipelines release pipeline.
  • 執行手動和 CI 觸發部署。Execute manual and CI-triggered deployments.

開始之前Before you begin

取得範例應用程式Get the sample app

您需要可部署的應用程式 (儲存在 Git 存放庫)。You need an app to deploy, stored in a Git repository. 在本教學課程中,我們建議您使用從 GitHub 中取得的此範例應用程式For this tutorial, we recommend that you use this sample app available from GitHub. 本教學課程包含用於安裝 Node.js 和應用程式的範例指令碼。This tutorial contains a sample script that's used for installing Node.js and an application. 如果您需要使用自己的存放庫,就應該設定類似的範例。If you want to work with your own repository, you should configure a similar sample.

建立此應用程式的分支並記下位置 (URL),此教學課程的後續步驟中會用到此位置。Create a fork of this app and take note of the location (URL) for use in later steps of this tutorial. 如需詳細資訊,請參閱分支存放庫For more information, see Fork a repo.

注意

應用程式是透過 Yeoman 進行建置。The app was built through Yeoman. 它會使用 Express、Bower 和 Grunt。It uses Express, bower, and grunt. 同時其具有某些 npm 套件作為相依項目。And it has some npm packages as dependencies. 此範例也包含設定 Nginx 和部署應用程式的指令碼。The sample also contains a script that sets up Nginx and deploys the app. 其執行於虛擬機器上。It is executed on the virtual machines. 具體來說,指令碼可以:Specifically, the script:

  1. 安裝 Node、Nginx 和 PM2。Installs Node, Nginx, and PM2.
  2. 設定 Nginx 和 PM2。Configures Nginx and PM2.
  3. 啟動 Node 應用程式。Starts the Node app.

設定 Jenkins 外掛程式Configure Jenkins plug-ins

首先,您必須設定兩個 Jenkins 外掛程式:NodeJSVS Team Services 持續部署First, you must configure two Jenkins plug-ins: NodeJS and VS Team Services Continuous Deployment.

  1. 開啟您的 Jenkins 帳戶,然後選取 [管理 Jenkins]。Open your Jenkins account and select Manage Jenkins.
  2. 在 [管理 Jenkins] 頁面上,選取 [管理外掛程式]。On the Manage Jenkins page, select Manage Plugins.
  3. 篩選清單以找出 NodeJS 外掛程式,然後選取 [Install without restart] (安裝但不要重新啟動) 選項。Filter the list to locate the NodeJS plug-in, and select the Install without restart option. 將 NodeJS 外掛程式新增至 JenkinsAdding the NodeJS plugin to Jenkins
  4. 篩選清單以尋找 VS Team Services 持續部署外掛程式,然後選取 [Install without restart] (安裝但不要重新啟動) 選項。Filter the list to find the VS Team Services Continuous Deployment plug-in and select the Install without restart option.
  5. 回到 Jenkins 儀表板,然後選取 [管理 Jenkins]。Go back to the Jenkins dashboard and select Manage Jenkins.
  6. 選取 [Global Tool Configuration] (全域工具設定)。Select Global Tool Configuration. 尋找 NodeJS,然後選取 [NodeJS installations] (NodeJS 安裝)。Find NodeJS and select NodeJS installations.
  7. 選取 [Install automatically] (自動安裝) 選項,然後輸入 [名稱] 值。Select the Install automatically option, and then enter a Name value.
  8. 選取 [ 儲存]。Select Save.

設定適用於 Node.js 的 Jenkins Freestyle 專案Configure a Jenkins Freestyle project for Node.js

  1. 選取 [新增項目]。Select New Item. 輸入項目名稱。Enter an item name.
  2. 選取 [Freestyle project] (自由樣式專案)。Select Freestyle project. 選取 [確定] 。Select OK.
  3. 在 [原始程式碼管理] 索引標籤中,選取 Git,然後輸入包含應用程式程式碼的存放庫與分支詳細資料。On the Source Code Management tab, select Git and enter the details of the repository and the branch that contain your app code.
    將存放庫新增至組建Add a repo to your build
  4. 在 [Build Triggers] (組建觸發程序) 索引標籤上,選取 [Poll SCM] (輪詢 SCM),並輸入排程 H/03 * * * *,每隔三分鐘輪詢 Git 存放庫以檢查變更。On the Build Triggers tab, select Poll SCM and enter the schedule H/03 * * * * to poll the Git repository for changes every three minutes.
  5. 在 [Build Environment] (組建環境) 索引標籤上,選取 [提供節點 & npm bin/ 資料夾路徑],然後選取 [NodeJS 安裝] 值。On the Build Environment tab, select Provide Node & npm bin/ folder PATH and select the NodeJS Installation value. npmrc 檔案的設定保留為 [使用系統預設值]。Leave npmrc file set to use system default.
  6. 在 [組建] 索引標籤上,選取 [Execute shell] (執行殼層),並輸入 npm install 命令,以確保所有相依性都會更新。On the Build tab, select Execute shell and enter the command npm install to ensure that all dependencies are updated.

設定適用於 Azure DevOps Services 整合的 JenkinsConfigure Jenkins for Azure DevOps Services integration

注意

請確定您用於下列步驟的個人存取權杖 (PAT),包含 Azure DevOps Services 中的發行 (讀取、寫入、執行和管理) 權限。Ensure that the personal access token (PAT) you use for the following steps contains the Release (read, write, execute and manage) permission in Azure DevOps Services.

  1. 在 Azure DevOps Services 組織中建立 PAT (如果您還沒有 PAT 的話)。Create a PAT in your Azure DevOps Services organization if you don't already have one. Jenkins 需要這項資訊來存取 Azure DevOps Services 組織。Jenkins requires this information to access your Azure DevOps Services organization. 請務必儲存本節中後續步驟的權杖資訊。Be sure to store the token information for upcoming steps in this section.

    若要了解如何產生個人存取權杖,請閱讀如何建立 Azure DevOps Services 的個人存取權杖?To learn how to generate a token, read How do I create a personal access token for Azure DevOps Services?.

  2. 在 [Post-build Actions] (建置後動作) 索引標籤上,選取 [Add post-build action] (新增建置後動作)。In the Post-build Actions tab, select Add post-build action. 選取 [Archive the artifacts] (封存成品)。Select Archive the artifacts.

  3. 針對 [要封存的檔案] 輸入 **/* 以包含所有檔案。For Files to archive, enter **/* to include all files.

  4. 若要建立另一個動作,請選取 [Add post-build action] (新增建置後動作)。To create another action, select Add post-build action.

  5. 選取 [Trigger release in TFS/Team Services] (觸發 TFS/Team Services 中的發行)。Select Trigger release in TFS/Team Services. 輸入 Azure DevOps Services 組織的 URI,例如 https://{your-organization-name}.visualstudio.comEnter the URI for your Azure DevOps Services organization, such as https://{your-organization-name}.visualstudio.com.

  6. 輸入專案名稱。Enter the Project name.

  7. 選擇發行管線的名稱。Choose a name for the release pipeline. (請稍後在 Azure DevOps Services 中建立此發行管線。)(You create this release pipeline later in Azure DevOps Services.)

  8. 選擇認證以連接到 Azure DevOps Services 或 Team Foundation Server 環境:Choose credentials to connect to your Azure DevOps Services or Team Foundation Server environment:

    • 如果您是使用 Azure DevOps Services,請將 [使用者名稱] 保留空白。Leave Username blank if you are using Azure DevOps Services.
    • 如果您是使用 Team Foundation Server 的內部部署版本,請輸入使用者名稱和密碼。Enter a username and password if you are using an on-premises version of Team Foundation Server.
      設定 Jenkins 建置後動作
  9. 儲存 Jenkins 專案。Save the Jenkins project.

建立 Jenkins 服務端點Create a Jenkins service endpoint

服務端點可讓 Azure DevOps Services 與 Jenkins 連線。A service endpoint allows Azure DevOps Services to connect to Jenkins.

  1. 開啟 Azure DevOps Services 中的 [服務] 頁面,並開啟 [新的服務端點] 清單,然後選取 [Jenkins]。Open the Services page in Azure DevOps Services, open the New Service Endpoint list, and select Jenkins. 新增 Jenkins 端點Add a Jenkins endpoint
  2. 輸入連線的名稱。Enter a name for the connection.
  3. 輸入您的 Jenkins 伺服器 URL,然後選取 [接受未受信任的 SSL 憑證] 選項。Enter the URL of your Jenkins server, and select the Accept untrusted SSL certificates option. 範例 URL 是 http://{您的 Jenkins URL}.westcentralus.cloudapp.azure.comAn example URL is http://{YourJenkinsURL}.westcentralus.cloudapp.azure.com.
  4. 輸入您 Jenkins 帳戶的使用者名稱和密碼。Enter the username and password for your Jenkins account.
  5. 選取 [驗證連線] 以確認資訊正確。Select Verify connection to check that the information is correct.
  6. 選取 [確定] 以建立服務端點。Select OK to create the service endpoint.

建立 Azure 虛擬機器的部署群組Create a deployment group for Azure virtual machines

您需要部署群組以註冊 Azure DevOps Services 代理程式,使發行管線可以部署至虛擬機器。You need a deployment group to register the Azure DevOps Services agent so the release pipeline can be deployed to your virtual machine. 部署群組可讓您輕鬆地定義部署的目標機器邏輯群組,並在每部機器上安裝必要的代理程式。Deployment groups make it easy to define logical groups of target machines for deployment, and to install the required agent on each machine.

注意

在下列程序中,請務必安裝必要元件,並且請勿使用 sudo 權限執行指令碼。In the following procedure, be sure to install the prerequisites and don't run the script with sudo privileges.

  1. 開啟 [建置 & 發行] 中樞裡的 [發行] 索引標籤,並開啟 [部署群組],然後選取 [+ 新增]。Open the Releases tab of the Build & Release hub, open Deployment groups, and select + New.
  2. 輸入部署群組的名稱和選擇性說明。Enter a name for the deployment group, and an optional description. 然後選取 [建立]。Then select Create.
  3. 選擇部署目標虛擬機器的作業系統。Choose the operating system for your deployment target virtual machine. 例如,選取 [Ubuntu 16.04+]。For example, select Ubuntu 16.04+.
  4. 選取 [使用指令碼中的個人存取權杖進行驗證]。Select Use a personal access token in the script for authentication.
  5. 選取 [系統必要條件] 連結。Select the System prerequisites link. 安裝適用於您作業系統的必要條件。Install the prerequisites for your operating system.
  6. 選取 [將指令碼複製到剪貼簿] 以複製指令碼。Select Copy script to clipboard to copy the script.
  7. 登入您的部署目標虛擬機器並執行指令碼。Log in to your deployment target virtual machine and run the script. 請勿使用 sudo 權限執行指令碼。Don't run the script with sudo privileges.
  8. 安裝之後,系統會提示您提供部署群組標記。After the installation, you are prompted for deployment group tags. 接受預設值。Accept the defaults.
  9. 在 Azure DevOps Services 中,檢查您在 [部署群組] 下的 [目標] 中新註冊的虛擬機器。In Azure DevOps Services, check for your newly registered virtual machine in Targets under Deployment Groups.

建立 Azure Pipelines 發行管線Create an Azure Pipelines release pipeline

發行管線會指定 Azure Pipelines 用來部署應用程式的流程。A release pipeline specifies the process that Azure Pipelines uses to deploy the app. 在此範例中,您要執行殼層指令碼。In this example, you execute a shell script.

若要在 Azure Pipelines 中建立發行管線:To create the release pipeline in Azure Pipelines:

  1. 開啟 [建置 & 發行] 中樞裡的 [發行] 索引標籤,然後選取 [建立發行管線]。Open the Releases tab of the Build & Release hub, and select Create release pipeline.
  2. 選擇開頭為 [空白流程],以選取 [空白] 範本。Select the Empty template by choosing to start with an Empty process.
  3. 在 [成品] 區段中,選取 [+ 新增成品],然後選擇 [Jenkins] 用於 [來源類型]。In the Artifacts section, select + Add Artifact and choose Jenkins for Source type. 選取您的 Jenkins 服務端點連線。Select your Jenkins service endpoint connection. 然後選取 Jenkins 來源作業,並選取 [新增]。Then select the Jenkins source job and select Add.
  4. 選取 [環境 1] 旁的省略符號。Select the ellipsis next to Environment 1. 選取 [新增部署群組階段]。Select Add deployment group phase.
  5. 選擇您的部署群組。Choose your deployment group.
  6. 選取 [+] 將工作新增至 [部署群組階段]。Select + to add a task to Deployment group phase.
  7. 選取 [殼層指令碼] 工作,然後選取 [新增]。Select the Shell Script task and select Add. 殼層指令碼工作提供每個伺服器上要執行的指令碼設定,以安裝 Node.js 並啟動應用程式。The Shell Script task provides the configuration for a script to run on each server in order to install Node.js and start the app.
  8. 若為指令碼路徑,請輸入 $(System.DefaultWorkingDirectory)/Fabrikam-Node/deployscript.shFor Script Path, enter $(System.DefaultWorkingDirectory)/Fabrikam-Node/deployscript.sh.
  9. 選取 [進階],然後啟用 [指定工作目錄]。Select Advanced, and then enable Specify Working Directory.
  10. 若為工作目錄,請輸入 $(System.DefaultWorkingDirectory)/Fabrikam-NodeFor Working Directory, enter $(System.DefaultWorkingDirectory)/Fabrikam-Node.
  11. 將發行管線的名稱編輯為 [Post-build Actions] (建置後動作) 索引標籤中 (位於 Jenkins 組建) 指定的名稱。Edit the name of the release pipeline to the name that you specified on the Post-build Actions tab of the build in Jenkins. 當來源成品更新時,Jenkins 需要此名稱才可觸發新的發行。Jenkins requires this name to be able to trigger a new release when the source artifacts are updated.
  12. 依序選取 [儲存] 和 [確定] 可儲存發行管線。Select Save and select OK to save the release pipeline.

執行手動和 CI 觸發部署Execute manual and CI-triggered deployments

  1. 選取 [+ 發行],然後選取 [建立發行]。Select + Release and select Create Release.
  2. 在反白顯示的下拉式清單中選取您完成的組建,然後選取 [佇列]。Select the build that you completed in the highlighted drop-down list, and select Queue.
  3. 在快顯訊息中選擇發行連結。Choose the release link in the pop-up message. 例如︰「發行 Release-1 已建立。」For example: "Release Release-1 has been created."
  4. 開啟 [記錄] 索引標籤以查看發行主控台輸出。Open the Logs tab to watch the release console output.
  5. 在瀏覽器中,開啟您在部署群組中新增之其中一部伺服器的 URL。In your browser, open the URL of one of the servers that you added to your deployment group. 例如,輸入 http://{您的伺服器 IP 位址}For example, enter http://{your-server-ip-address}.
  6. 移至來源 Git 存放庫,並使用某些變更的文字修改 app/views/index.jade 檔案中的 h1 標題。Go to the source Git repository and modify the contents of the h1 heading in the file app/views/index.jade with some changed text.
  7. 認可變更。Commit your change.
  8. 稍待幾分鐘後,您會在 Azure DevOps 的 [發行] 頁面上看到新的發行。After a few minutes, you will see a new release created on the Releases page of Azure DevOps. 開啟發行以查看正在進行的部署。Open the release to see the deployment taking place. 恭喜!Congratulations!

對 Jenkins 外掛程式進行疑難排解Troubleshooting the Jenkins plugin

如果您遇到任何有關 Jenkins 外掛程式的錯誤,請在 Jenkins JIRA 的特定元件中提交問題。If you encounter any bugs with the Jenkins plugins, file an issue in the Jenkins JIRA for the specific component.

後續步驟Next steps

在本教學課程中,您已使用 Jenkins (用於建置) 和 Azure DevOps Services (用於發行),將應用程式自動部署到 Azure。In this tutorial, you automated the deployment of an app to Azure by using Jenkins for build and Azure DevOps Services for release. 您已了解如何︰You learned how to:

  • 在 Jenkins 中建置應用程式。Build your app in Jenkins.
  • 設定適用於 Azure DevOps Services 整合的 Jenkins。Configure Jenkins for Azure DevOps Services integration.
  • 建立 Azure 虛擬機器的部署群組。Create a deployment group for the Azure virtual machines.
  • 建立發行管線以設定 VM 及部署應用程式。Create a release pipeline that configures the VMs and deploys the app.

若要了解如何部署 LAMP (Linux、Apache、MySQL 和 PHP) 堆疊,請前進到下一個教學課程。To learn about how to deploy a LAMP (Linux, Apache, MySQL, and PHP) stack, advance to the next tutorial.