从本地 Git 部署到 Azure 应用服务Local Git deployment to Azure App Service

本操作方法指南介绍如何将应用从本地计算机上的 Git 存储库部署到 Azure 应用服务This how-to guide shows you how to deploy your app to Azure App Service from a Git repository on your local computer.

先决条件Prerequisites

按照本操作方法指南中的步骤操作:To follow the steps in this how-to guide:

  • 如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

  • 安装 GitInstall Git.

  • 创建包含想要部署的代码的本地 Git 存储库。Have a local Git repository with code you want to deploy. 若要下载示例存储库,请在本地终端窗口运行以下命令:To download a sample repository, run the following command in your local terminal window:

    git clone https://github.com/Azure-Samples/nodejs-docs-hello-world.git
    

准备存储库Prepare your repository

若要从 Azure 应用服务 Kudu 生成服务器获取自动生成,请确保项目中存储库根路径具有正确的文件。To get automatic builds from Azure App Service Kudu build server, make sure that your repository root has the correct files in your project.

运行时Runtime 根目录文件Root directory files
ASP.NET(仅限 Windows)ASP.NET (Windows only) *.sln*.csprojdefault.aspx*.sln, *.csproj, or default.aspx
ASP.NET CoreASP.NET Core *.sln*.csproj*.sln or *.csproj
PHPPHP index.php index.php
Ruby(仅限 Linux)Ruby (Linux only) Gemfile Gemfile
Node.jsNode.js server.js、app.js 或具有启动脚本的 package.json server.js, app.js, or package.json with a start script
PythonPython *.pyrequirements.txtruntime.txt*.py, requirements.txt, or runtime.txt
HTMLHTML default.htm、default.html、default.asp、index.htm、index.html 或 iisstart.htm default.htm, default.html, default.asp, index.htm, index.html, or iisstart.htm
Web 作业WebJobs App_Data/jobs/continuous(适用于连续的 WebJobs)或 App_Data/jobs/triggered(适用于触发的 WebJobs)下的 <job_name>/run.<extension> 。<job_name>/run.<extension> under App_Data/jobs/continuous for continuous WebJobs, or App_Data/jobs/triggered for triggered WebJobs. 有关详细信息,请参阅 Kudu WebJobs 文档For more information, see Kudu WebJobs documentation.

要自定义部署,可以在存储库根路径中添加 .deployment 文件 。To customize your deployment, include a .deployment file in the repository root. 有关详细信息,请参阅自定义部署自定义部署脚本For more information, see Customize deployments and Custom deployment script.

Note

如果在 Visual Studio 中进行开发,让 Visual Studio 创建存储库If you develop in Visual Studio, let Visual Studio create a repository for you. 该项目可立即通过 Git 进行部署。The project is immediately ready to be deployed by using Git.

使用 Kudu 生成服务器进行部署Deploy with Kudu build server

使用 Kudu 应用服务生成服务器为应用启用本地 Git 部署的最简单方法是使用 Azure CLI。The easiest way to enable local Git deployment for your app with the Kudu App Service build server is to use Azure CLI.

配置部署用户Configure a deployment user

可以使用“部署用户” 将 FTP 和本地 Git 部署到 Azure Web 应用。FTP and local Git can deploy to an Azure web app by using a deployment user. 配置部署用户之后,可对所有 Azure 部署使用此用户。Once you configure your deployment user, you can use it for all your Azure deployments. 帐户级部署用户名和密码不同于 Azure 订阅凭据。Your account-level deployment username and password are different from your Azure subscription credentials.

若要配置部署用户,请在 Azure Cloud Shell 中运行 az webapp deployment user set 命令。To configure the deployment user, run the az webapp deployment user set command in Azure Cloud Shell. 将 <username> 和 <password> 替换为部署用户名和密码。Replace <username> and <password> with a deployment user username and password.

  • 用户名必须在 Azure 中唯一,并且对于本地 Git 推送,不能包含“@”符号。The username must be unique within Azure, and for local Git pushes, must not contain the �@� symbol.
  • 密码必须至少为 8 个字符,且具有字母、数字和符号这三种元素中的两种。The password must be at least eight characters long, with two of the following three elements: letters, numbers, and symbols.
az webapp deployment user set --user-name <username> --password <password>

JSON 输出会将该密码显示为 nullThe JSON output shows the password as null. 如果收到 'Conflict'. Details: 409 错误,请更改用户名。If you get a 'Conflict'. Details: 409 error, change the username. 如果收到 'Bad Request'. Details: 400 错误,请使用更强的密码。If you get a 'Bad Request'. Details: 400 error, use a stronger password.

请记录你要用于部署 Web 应用的用户名和密码。Record your username and password to use to deploy your web apps.

获取部署 URLGet the deployment URL

若要获取所需的 URL 来为现有应用启用本地 Git 部署,请在 Azure CLI 中运行 az webapp deployment source config-local-gitTo get the URL to enable local Git deployment for an existing app, run az webapp deployment source config-local-git in the Azure CLI. 请将 <app-name> 和 <group-name> 替换为应用及其 Azure 资源组的名称。Replace <app-name> and <group-name> with the names of your app and its Azure resource group.

az webapp deployment source config-local-git --name <app-name> --resource-group <group-name>

或者,若要创建启用 Git 的新应用,请在 Azure CLI 中结合 --deployment-local-git 参数运行 az webapp createOr, to create a new Git-enabled app, run az webapp create in the Azure CLI with the --deployment-local-git parameter. 请将 <app-name>、<group-name> 和 <plan-name> 替换为新 Git 应用、其 Azure 资源组及其 Azure 应用服务计划的名称。Replace <app-name>, <group-name>, and <plan-name> with the names for your new Git app, its Azure resource group, and its Azure App Service plan.

az webapp create --name <app-name> --resource-group <group-name> --plan <plan-name> --deployment-local-git

上述任一命令都会返回类似于 https://<deployment-username>@<app-name>.scm.chinacloudsites.cn/<app-name>.git 的 URL。Either command returns a URL like: https://<deployment-username>@<app-name>.scm.chinacloudsites.cn/<app-name>.git. 在下一步骤中,将使用此 URL 部署应用。Use this URL to deploy your app in the next step.

如果不使用此帐户级 URL,也可以使用应用级凭据启用本地 Git。Instead of using this account-level URL, you can also enable local Git by using app-level credentials. Azure 应用服务会自动为每个应用生成这些凭据。Azure App Service automatically generates these credentials for every app.

在 Cloud Shell 中运行以下命令以获取应用凭据。Get the app credentials by running the following command in the Cloud Shell. 请将 <app-name> 和 <group-name> 替换为应用和 Azure 资源组的名称。Replace <app-name> and <group-name> with your app's name and Azure resource group name.

az webapp deployment list-publishing-credentials --name <app-name> --resource-group <group-name> --query scmUri --output tsv

在下一步骤中,将使用返回的 URL 部署应用。Use the URL that returns to deploy your app in the next step.

部署 Web 应用Deploy the web app

  1. 打开本地终端窗口并转到本地 Git 存储库,然后添加一个 Azure 远程实例。Open a local terminal window to your local Git repository, and add an Azure remote. 在以下命令中,请将 <url> 替换为你在上一步骤中获取的特定于部署用户的 URL 或特定于应用的 URL。In the following command, replace <url> with the deployment user-specific URL or app-specific URL you got from the previous step.

    git remote add azure <url>
    
  2. 使用 git push azure master 推送到 Azure 远程实例。Push to the Azure remote with git push azure master.

  3. Git 凭据管理器窗口中,输入 部署用户密码 而不是 Azure 登录密码。 In the Git Credential Manager window, enter your deployment user password, not your Azure sign-in password.

  4. 查看输出。Review the output. 你可能会看到特定于运行时的自动化,例如 MSBuild for ASP.NET、npm install for Node.js 和 pip install for Python。You may see runtime-specific automation, such as MSBuild for ASP.NET, npm install for Node.js, and pip install for Python.

  5. 在 Azure 门户中浏览到你的应用以检查内容是否已部署。Browse to your app in the Azure portal to verify that the content is deployed.

排查部署问题Troubleshoot deployment

使用 Git 发布到 Azure 中的应用服务应用时,你可能会看到以下常见错误消息:You may see the following common error messages when you use Git to publish to an App Service app in Azure:

MessageMessage 原因Cause 解决方法Resolution
Unable to access '[siteURL]': Failed to connect to [scmAddress] 应用未正常运行。The app isn't up and running. 在 Azure 门户中启动应用。Start the app in the Azure portal. 如果 Web 应用已停止,Git 部署将不可用。Git deployment isn't available when the web app is stopped.
Couldn't resolve host 'hostname' “azure”远程实例的地址信息不正确。The address information for the 'azure' remote is incorrect. 使用 git remote -v 命令列出所有远程网站以及关联的 URL。Use the git remote -v command to list all remotes, along with the associated URL. 确认“azure”远程网站的 URL 正确。Verify that the URL for the 'azure' remote is correct. 如果需要,请删除此远程网站并使用正确的 URL 重新创建它。If needed, remove and recreate this remote using the correct URL.
No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as 'master'. 在运行 git push 期间未指定分支,或者未在 .gitconfig 中设置 push.default 值。You didn't specify a branch during git push, or you haven't set the push.default value in .gitconfig. 再次运行 git push,并指定主分支:git push azure masterRun git push again, specifying the master branch: git push azure master.
src refspec [branchname] does not match any. 你已尝试推送到“azure”远程实例上除主节点以外的分支。You tried to push to a branch other than master on the 'azure' remote. 再次运行 git push,并指定主分支:git push azure masterRun git push again, specifying the master branch: git push azure master.
RPC failed; result=22, HTTP code = 5xx. 如果尝试通过 HTTPS 推送大型 Git 存储库,则可能出现此错误。This error can happen if you try to push a large git repository over HTTPS. 在本地计算机上更改 Git 配置,以增大 postBufferChange the git configuration on the local machine to make the postBuffer bigger. 例如:git config --global http.postBuffer 524288000For example: git config --global http.postBuffer 524288000.
Error - Changes committed to remote repository but your web app not updated. 你已使用一个指定了其他所需模块的 package.json 文件部署了 Node.js 应用。You deployed a Node.js app with a package.json file that specifies additional required modules. 检查发生此错误之前出现的 npm ERR! 错误消息,以了解有关失败的更多上下文。Review the npm ERR! error messages before this error for more context on the failure. 下面是此错误的已知原因,以及相应的 npm ERR! 消息:The following are the known causes of this error, and the corresponding npm ERR! messages:

package.json 文件格式不当npm ERR! Couldn't read dependencies.Malformed package.json file: npm ERR! Couldn't read dependencies.

本机模块没有适用于 Windows 的二进制分发版Native module doesn't have a binary distribution for Windows:
npm ERR! \cmd "/c" "node-gyp rebuild"\ failed with 1
or
`npm ERR!`npm ERR! [modulename@version] preinstall: \make[modulename@version] preinstall: \make
gmake`gmake`

其他资源Additional resources