您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

通过使用 Jenkins 和 Team Services 将应用部署到 Linux VMDeploy your app to Linux VMs by using Jenkins and Team Services

持续集成 (CI) 和持续部署 (CD) 可形成一个管道,用于生成、发布和部署代码。Continuous integration (CI) and continuous deployment (CD) form a pipeline by which you can build, release, and deploy your code. Visual Studio Team Services 提供一套完整的全功能 CI/CD 自动化工具,用于部署到 Azure。Visual Studio Team 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. 你可以结合使用 Team Services 和 Jenkins 来自定义交付云应用或服务的方式。You can use Team 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. 随后,使用 Team Services 或 Team Foundation Server 将其部署到包含 Linux 虚拟机 (VM) 的部署组You then use Team Services or Team Foundation Server to deploy it to a deployment group that contains Linux virtual machines (VMs).

将能够:You will:

  • 获取示例应用。Get the sample app.
  • 配置 Jenkins 插件。Configure Jenkins plug-ins.
  • 为 Node.js 配置 Jenkins 自由风格项目。Configure a Jenkins Freestyle project for Node.js.
  • 为 Team Services 集成配置 Jenkins。Configure Jenkins for Team Services integration.
  • 创建 Jenkins 服务终结点。Create a Jenkins service endpoint.
  • 为 Azure 虚拟机创建部署组。Create a deployment group for the Azure virtual machines.
  • 创建 Team Services 发布定义。Create a Team Services release definition.
  • 执行手动和 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 插件:NodeJS 和 VS 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”插件,然后选择“安装而不重启”选项。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 持续部署”插件,然后选择“安装而不重启”选项。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. 选择“全局工具配置”。Select Global Tool Configuration. 查找“NodeJS”,然后选择“NodeJS 安装”。Find NodeJS and select NodeJS installations.
  7. 选择“自动安装”选项,然后输入“名称”值。Select the Install automatically option, and then enter a Name value.
  8. 选择“保存”。Select Save.

为 Node.js 配置 Jenkins 自由风格项目Configure a Jenkins Freestyle project for Node.js

  1. 选择“新建项”。Select New Item. 输入项目值。Enter an item name.
  2. 选择“自由风格项目”。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. 在“生成触发器”选项卡上,选择“轮询 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. 在“生成环境”选项卡上,选择“提供节点 & npm bin/ folder PATH”,然后选择“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. 在“生成”选项卡上,选择“执行 shell”,然后输入命令 npm install,以确保更新所有依赖项。On the Build tab, select Execute shell and enter the command npm install to ensure that all dependencies are updated.

为 Team Services 集成配置 JenkinsConfigure Jenkins for Team Services integration


确保你用于以下步骤的个人访问令牌 (PAT) 包含 Team Services 中的“发布”(读取、写入、执行和管理)权限。Ensure that the personal access token (PAT) you use for the following steps contains the Release (read, write, execute and manage) permission in Team Services.

  1. 在 Team Services 帐户中创建 PAT(如果还没有)。Create a PAT in your Team Services account if you don't already have one. Jenkins 需要使用此信息来访问 Team Services 帐户。Jenkins requires this information to access your Team Services account. 确保存储令牌信息以用于本部分后面的步骤。Be sure to store the token information for upcoming steps in this section.

    若要了解如何生成令牌,请阅读如何为 VSTS 和 TFS 创建个人访问令牌?To learn how to generate a token, read How do I create a personal access token for VSTS and TFS?.

  2. 在“生成后操作”选项卡中,选择“添加生成后操作”。In the Post-build Actions tab, select Add post-build action. 选择“存档项目”。Select Archive the artifacts.
  3. 对于“要存档的文件”,输入 **/* 以包括所有文件。For Files to archive, enter **/* to include all files.
  4. 若要创建其他操作,请选择“添加生成后操作”。To create another action, select Add post-build action.
  5. 选择“在 TFS/Team Services 中触发发布” 。Select Trigger release in TFS/Team Services. 输入 Team Services 帐户的 URI,例如https://{your-account-name}.visualstudio.com。Enter the URI for your Team Services account, such as https://{your-account-name}.visualstudio.com.
  6. 输入“团队项目”名称。Enter the Team Project name.
  7. 为发布定义选择名称。Choose a name for the release definition. (你可以稍后在 Team Services 中创建此发布定义。)(You create this release definition later in Team Services.)
  8. 选择用于连接到 Team Services 或 Team Foundation Server 环境的凭据:Choose credentials to connect to your Team Services or Team Foundation Server environment:
    • 如果你正在使用 Team Services,请将“用户名”留空。Leave Username blank if you are using Team 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

服务终结点允许 Team Services 连接到 Jenkins。A service endpoint allows Team Services to connect to Jenkins.

  1. 在 Team Services 中打开“服务”页面,打开“新服务终结点”列表,然后选择“Jenkins”。Open the Services page in Team 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://{YourJenkinsURL}.westcentralus.cloudapp.azure.com。An 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

你需要部署组以注册 Team Services 代理,以便可以将发布定义部署到虚拟机。You need a deployment group to register the Team Services agent so the release definition 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. 在 Team Services 中,在“部署组”下的“目标”中检查新注册的虚拟机。In Team Services, check for your newly registered virtual machine in Targets under Deployment Groups.

创建 Team Services 发布定义Create a Team Services release definition

发布定义将指定 Team Services 用于部署应用的过程。A release definition specifies the process that Team Services uses to deploy the app. 在此示例中,你将执行一个 shell 脚本。In this example, you execute a shell script.

若要在 Team Services 中创建发布定义,请执行以下操作:To create the release definition in Team Services:

  1. 打开“生成 & 发布”中心的“发布”选项卡,然后选择“创建发布定义”。Open the Releases tab of the Build & Release hub, and select Create release definition.
  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. 选择“Shell 脚本”任务,并选择“添加”。Select the Shell Script task and select Add. “Shell 脚本”任务为要在每个服务器上运行的脚本提供配置,以安装 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.sh”。For Script Path, enter $(System.DefaultWorkingDirectory)/Fabrikam-Node/deployscript.sh.
  9. 选择“高级”,然后启用“指定工作目录”。Select Advanced, and then enable Specify Working Directory.
  10. 对于“工作目录”,请输入“$(System.DefaultWorkingDirectory)/Fabrikam-Node”。For Working Directory, enter $(System.DefaultWorkingDirectory)/Fabrikam-Node.
  11. 将发布定义的名称编辑为你在 Jenkins 内部版本的“生成后操作”选项卡上指定的名称。Edit the name of the release definition 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 definition.

执行手动和 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://{your-server-ip-address}。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. 数分钟后,你将看到 Team Services 或 Team Foundation Server 的“发布”页上创建了一个新发布。After a few minutes, you will see a new release created on the Releases page of Team Services or Team Foundation Server. 打开此发布可以看到部署正在进行。Open the release to see the deployment taking place. 祝贺你!Congratulations!

后续步骤Next steps

在本教程中,你通过使用 Jenkins 进行生成并使用 Team Services 进行发布,自动将一个应用部署到 Azure。In this tutorial, you automated the deployment of an app to Azure by using Jenkins for build and Team Services for release. 你已了解如何:You learned how to:

  • 在 Jenkins 中生成应用。Build your app in Jenkins.
  • 为 Team Services 集成配置 Jenkins。Configure Jenkins for Team Services integration.
  • 为 Azure 虚拟机创建部署组。Create a deployment group for the Azure virtual machines.
  • 创建配置 VM 并部署应用的发布定义。Create a release definition 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.