在基于 Linux 上的 Azure 应用服务中生成 Ruby 和 Postgres 应用Build a Ruby and Postgres app in Azure App Service on Linux

Azure 应用服务提供高度可缩放、自修复的 Web 托管服务。Azure App Service provides a highly scalable, self-patching web hosting service. 本教程介绍如何创建 Ruby 应用,并将其连接到 PostgreSQL 数据库。This tutorial shows how to create a Ruby app and connect it to a PostgreSQL database. 完成本教程后,Linux 上的应用服务将会运行一个 Ruby on Rails 应用。When you're finished, you'll have a Ruby on Rails app running on App Service on Linux.

标题为“任务”的 Ruby on Rails 应用示例的屏幕截图。

在本教程中,你将了解如何执行以下操作:In this tutorial, you learn how to:

  • 在 Azure 中创建 PostgreSQL 数据库Create a PostgreSQL database in Azure
  • 将 Ruby on Rails 应用连接到 PostgreSQLConnect a Ruby on Rails app to PostgreSQL
  • 将应用部署到 AzureDeploy the app to Azure
  • 更新数据模型并重新部署应用Update the data model and redeploy the app
  • 从 Azure 流式传输诊断日志Stream diagnostic logs from Azure
  • 在 Azure 门户中管理应用Manage the app in the Azure portal

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

先决条件Prerequisites

为完成此教程:To complete this tutorial:

准备本地 PostgresPrepare local Postgres

此步骤在本地 Postgres 服务器中创建一个数据库,以便在本教程中使用。In this step, you create a database in your local Postgres server for your use in this tutorial.

连接到本地 Postgres 服务器Connect to local Postgres server

打开终端窗口,并运行 psql 以连接到本地 Postgres 服务器。Open the terminal window and run psql to connect to your local Postgres server.

sudo -u postgres psql

如果连接成功,则表示 Postgres 数据库已在运行。If your connection is successful, your Postgres database is running. 否则,应确保按照下载 - PostgreSQL Core 发行版中的步骤启用本地 Postgres 数据库。If not, make sure that your local Postgres database is started by following the steps at Downloads - PostgreSQL Core Distribution.

键入 \q 退出 Postgres 客户端。Type \q to exit the Postgres client.

使用登录的 Linux 用户名,创建一个可以通过运行以下命令来创建数据库的 Postgres 用户。Create a Postgres user that can create databases by running the following command, using your signed-in Linux username.

sudo -u postgres createuser -d <signed-in-user>

在本地创建 Ruby on Rails 应用Create a Ruby on Rails app locally

此步骤创建一个 Ruby on Rails 示例应用程序、配置其数据库连接,并在本地运行该应用程序。In this step, you get a Ruby on Rails sample application, configure its database connection, and run it locally.

克隆示例Clone the sample

在终端窗口中,通过 cd 转到工作目录。In the terminal window, cd to a working directory.

运行下列命令以克隆示例存储库。Run the following command to clone the sample repository.

git clone https://github.com/Azure-Samples/rubyrails-tasks.git

通过 cd 转到克隆目录。cd to your cloned directory. 安装所需程序包。Install the required packages.

cd rubyrails-tasks
bundle install --path vendor/bundle

在本地运行示例Run the sample locally

运行 Rails 迁移以创建应用程序所需的表。Run the Rails migrations to create the tables the application needs. 若要查看迁移中创建了哪些表,请查看 Git 存储库中的 db/migrate 目录。To see which tables are created in the migrations, look in the db/migrate directory in the Git repository.

rake db:create
rake db:migrate

运行应用程序。Run the application.

rails server

在浏览器中导航至 http://localhost:3000Navigate to http://localhost:3000 in a browser. 在页面中添加一些任务。Add a few tasks in the page.

Ruby on Rails 已成功连接到 Postgres

若要停止 Rails 服务器,请在终端中键入 Ctrl + CTo stop the Rails server, type Ctrl + C in the terminal.

在 Azure 中创建 PostgresCreate Postgres in Azure

此步骤在 Azure Database for PostgreSQL 中创建一个 Postgres 数据库。In this step, you create a Postgres database in Azure Database for PostgreSQL. 稍后需要将 Ruby on Rails 应用程序配置为连接到此数据库。Later, you configure the Ruby on Rails application to connect to this database.

创建资源组Create a resource group

资源组是在其中部署和管理 Azure 资源(如 Web 应用、数据库和存储帐户)的逻辑容器。A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed. 例如,可以选择在使用完之后通过一个简单的步骤删除整个资源组。For example, you can choose to delete the entire resource group in one simple step later.

在 Azure CLI 中,使用 az group create 命令创建资源组。In the Azure CLI, create a resource group with the az group create command. 以下示例在“中国东部 2”位置创建名为 myResourceGroup 的资源组。**The following example creates a resource group named myResourceGroup in the China East 2 location. 若要查看基本层中 Linux 上的应用服务支持的所有位置,请运行 az appservice list-locations --sku B1 --linux-workers-enabled 命令。To see all supported locations for App Service on Linux in Basic tier, run the az appservice list-locations --sku B1 --linux-workers-enabled command.

az group create --name myResourceGroup --location "China East 2"

通常在附近的区域中创建资源组和资源。You generally create your resource group and the resources in a region near you.

此命令完成后,JSON 输出会显示资源组属性。When the command finishes, a JSON output shows you the resource group properties.

在 Azure 中创建 Postgres 数据库Create Postgres database in Azure

在本部分,你将创建 Azure Database for PostgreSQL 服务器和数据库。In this section, you create an Azure Database for PostgreSQL server and database. 若要开始,请使用以下命令安装 db-up 扩展:To start, install the db-up extension with the following command:

az extension add --name db-up

如以下示例所示,使用 az postgres up 命令在 Azure 中创建 Postgres 数据库。Create the Postgres database in Azure with the az postgres up command, as shown in the following example. 将 <postgresql-name> 替换为唯一名称(服务器终结点为“https://<postgresql-name>.postgres.database.azure.cn”) 。Replace <postgresql-name> with a unique name (the server endpoint is https://<postgresql-name>.postgres.database.azure.cn). 对于 <admin-username> 和 <admin-password>,请指定用来为此 Postgres 服务器创建管理员用户的凭据 。For <admin-username> and <admin-password>, specify credentials to create an administrator user for this Postgres server.

az postgres up --resource-group myResourceGroup --location chinaeast2 --server-name <postgresql-name> --database-name sampledb --admin-user <admin-username> --admin-password <admin-password> --ssl-enforcement Enabled

此命令可能会运行较长时间,因为它要执行以下操作:This command may take a while because it's doing the following:

  • 创建名为 myResourceGroup资源组(如果不存在)。Creates a resource group called myResourceGroup, if it doesn't exist. 每个 Azure 资源都需要位于某个资源组中。Every Azure resource needs to be in one of these. --resource-group 是可选项。--resource-group is optional.
  • 创建带有管理用户的 Postgres 服务器。Creates a Postgres server with the administrative user.
  • 创建 sampledb 数据库。Creates a sampledb database.
  • 允许从本地 IP 地址进行访问。Allows access from your local IP address.
  • 允许从 Azure 服务进行访问。Allows access from Azure services.
  • 创建有权访问 sampledb 数据库的数据库用户。Create a database user with access to the sampledb database.

可以使用其他 az postgres 命令和 psql 单独执行每个步骤,但 az postgres up 可以通过一个步骤完成所有这些操作。You can do all the steps separately with other az postgres commands and psql, but az postgres up does all of them in one step for you.

命令完成后,找到以 Ran Database Query: 开头的输出行。When the command finishes, find the output lines that being with Ran Database Query:. 这些行显示了系统为你创建的数据库用户(用户名为 root,密码为 Sampledb1)。They show the database user that's created for you, with the username root and password Sampledb1. 稍后你要使用这些凭据将应用连接到数据库。You'll use them later to connect your app to the database.

提示

--location <location-name> 可以设置为任一个 Azure 区域--location <location-name>, can be set to any one of the Azure regions. 可以使用 az account list-locations 命令获取可供你的订阅使用的区域。You can get the regions available to your subscription with the az account list-locations command. 对于生产应用,请将数据库和应用放置在同一位置。For production apps, put your database and your app in the same location.

将应用连接到 Azure PostgresConnect app to Azure Postgres

此步骤将 Ruby on Rails 应用程序连接到在 Azure Database for PostgreSQL 中创建的 Postgres 数据库。In this step, you connect the Ruby on Rails application to the Postgres database you created in Azure Database for PostgreSQL.

配置数据库连接Configure the database connection

在存储库中,打开 config/database.ymlIn the repository, open config/database.yml. 在该文件的底部,将生产变量替换为以下代码。At the bottom of the file, replace the production variables with the following code.

production:
  <<: *default
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB_DATABASE'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>

保存更改。Save the changes.

在本地测试应用程序Test the application locally

回到本地终端,设置以下环境变量:Back in the local terminal, set the following environment variables:

export DB_HOST=<postgres-server-name>.postgres.database.azure.cn
export DB_DATABASE=sampledb 
export DB_USERNAME=root@<postgres-server-name>
export DB_PASSWORD=Sampledb1

使用刚刚配置的生产值运行 Rails 数据库迁移,在 Azure Database for PostgreSQL 中的 Postgres 数据库内创建表。Run Rails database migrations with the production values you just configured to create the tables in your Postgres database in Azure Database for PostgreSQL.

rake db:migrate RAILS_ENV=production

在生产环境中运行时,Rails 应用程序需要预编译的资产。When running in the production environment, the Rails application needs precompiled assets. 使用以下命令生成所需的资产:Generate the required assets with the following command:

rake assets:precompile

Rails 生产环境还使用机密来管理安全性。The Rails production environment also uses secrets to manage security. 生成机密密钥Generate a secret key.

rails secret

将机密密钥保存到 Rails 生产环境使用的相应变量。Save the secret key to the respective variables used by the Rails production environment. 为方便起见,可对两个变量使用相同的密钥。For convenience, you use the same key for both variables.

export RAILS_MASTER_KEY=<output-of-rails-secret>
export SECRET_KEY_BASE=<output-of-rails-secret>

启用 Rails 生产环境,以提供 JavaScript 和 CSS 文件。Enable the Rails production environment to serve JavaScript and CSS files.

export RAILS_SERVE_STATIC_FILES=true

在生产环境中运行示例应用程序。Run the sample application in the production environment.

rails server -e production

导航到 http://localhost:3000Navigate to http://localhost:3000. 如果页面可加载且未出错,则表示 Ruby on Rails 应用程序正在连接到 Azure 中的 Postgres 数据库。If the page loads without errors, the Ruby on Rails application is connecting to the Postgres database in Azure.

在页面中添加一些任务。Add a few tasks in the page.

Ruby on Rails 已成功连接到 Azure Database for PostgreSQL

若要停止 Rails 服务器,请在终端中键入 Ctrl + CTo stop the Rails server, type Ctrl + C in the terminal.

提交更改Commit your changes

运行以下的 Git 命令,提交更改:Run the following Git commands to commit your changes:

git add .
git commit -m "database.yml updates"

应用已可用于部署。Your app is ready to be deployed.

“部署到 Azure”Deploy to Azure

此步骤将已连接 Postgres 的 Rails 应用程序部署到 Azure 应用服务。In this step, you deploy the Postgres-connected Rails application to Azure App Service.

配置部署用户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.

创建应用服务计划Create an App Service plan

在 Azure CLI 中,使用 az appservice plan create 命令在资源组中创建应用服务计划。In the Azure CLI, create an App Service plan in the resource group with the az appservice plan create command.

应用服务计划指定托管应用的 Web 服务器场的位置、大小和功能。An App Service plan specifies the location, size, and features of the web server farm that hosts your app. 可以在托管多个应用时节省资金,方法是将多个 Web 应用配置为共用单个应用服务计划。You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan.

应用服务计划定义:App Service plans define:

  • 区域(例如:中国东部、中国东部 2、中国北部、中国北部 2)Region (for example: China East, China East 2, China North, China North 2)
  • 实例大小(小、中、大)Instance size (small, medium, or large)
  • 规模计数(1 到 10 个实例)Scale count (1 to 10 instances)
  • SKU(基本、标准)SKU (Basic, Standard)

以下示例在免费定价层 (--sku F1) 和 Linux 容器 (--is-linux) 中创建名为 myAppServicePlan 的应用服务计划。The following example creates an App Service plan named myAppServicePlan in the Free pricing tier (--sku F1) and in a Linux container (--is-linux).

az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku F1 --is-linux

创建应用服务计划后,Azure CLI 会显示类似于以下示例的信息:When the App Service plan has been created, the Azure CLI shows information similar to the following example:

{ 
  "adminSiteName": null,
  "appServicePlanName": "myAppServicePlan",
  "geoRegion": "China East 2",
  "hostingEnvironmentProfile": null,
  "id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan",
  "kind": "linux",
  "location": "China East 2",
  "maximumNumberOfWorkers": 1,
  "name": "myAppServicePlan",
  < JSON data removed for brevity. >
  "targetWorkerSizeId": 0,
  "type": "Microsoft.Web/serverfarms",
  "workerTierName": null
} 

创建 Web 应用Create a web app

myAppServicePlan 应用服务计划中创建一个 Web 应用Create a web app in the myAppServicePlan App Service plan.

在 Azure CLI 中,可以使用 az webapp create 命令。In the Azure CLI, you can use the az webapp create command. 在以下示例中,将 <app-name> 替换为全局唯一的应用名称(有效字符是 a-z0-9-)。In the following example, replace <app-name> with a globally unique app name (valid characters are a-z, 0-9, and -). 运行时设置为 RUBY|2.6.2The runtime is set to RUBY|2.6.2. 若要查看所有受支持的运行时,请运行 az webapp list-runtimes --linuxTo see all supported runtimes, run az webapp list-runtimes --linux.

# Bash
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app-name> --runtime "RUBY|2.6.2" --deployment-local-git
# PowerShell
az --% webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app-name> --runtime "RUBY|2.6.2" --deployment-local-git

创建 Web 应用后,Azure CLI 会显示类似于以下示例的输出:When the web app has been created, the Azure CLI shows output similar to the following example:

Local git is configured with url of 'https://<username>@<app-name>.scm.chinacloudsites.cn/<app-name>.git'
{
  "availabilityState": "Normal",
  "clientAffinityEnabled": true,
  "clientCertEnabled": false,
  "cloningInfo": null,
  "containerSize": 0,
  "dailyMemoryTimeQuota": 0,
  "defaultHostName": "<app-name>.chinacloudsites.cn",
  "deploymentLocalGitUrl": "https://<username>@<app-name>.scm.chinacloudsites.cn/<app-name>.git",
  "enabled": true,
  < JSON data removed for brevity. >
}

现在你已经创建了一个新的空 Web 应用并启用了 Git 部署。You’ve created an empty new web app, with git deployment enabled.

备注

Git 远程的 URL 将显示在 deploymentLocalGitUrl 属性中,其格式为 https://<username>@<app-name>.scm.chinacloudsites.cn/<app-name>.gitThe URL of the Git remote is shown in the deploymentLocalGitUrl property, with the format https://<username>@<app-name>.scm.chinacloudsites.cn/<app-name>.git. 保存此 URL,后续将会用到。Save this URL as you need it later.

配置数据库设置Configure database settings

在应用服务中,使用 Azure CLI 中的 az webapp config appsettings set 命令将环境变量设置为应用设置。In App Service, you set environment variables as app settings by using the az webapp config appsettings set command in the Azure CLI.

以下 Azure CLI 命令用于配置应用设置 DB_HOSTDB_DATABASEDB_USERNAMEDB_PASSWORDThe following Azure CLI command configures the app settings DB_HOST, DB_DATABASE, DB_USERNAME, and DB_PASSWORD. 替换占位符 <appname><postgres-server-name>Replace the placeholders <appname> and <postgres-server-name>.

az webapp config appsettings set --name <app-name> --resource-group myResourceGroup --settings DB_HOST="<postgres-server-name>.postgres.database.azure.cn" DB_DATABASE="sampledb" DB_USERNAME="root@<postgres-server-name>" DB_PASSWORD="Sampledb1"

配置 Rails 环境变量Configure Rails environment variables

在本地终端中,为 Azure 中的 Rails 生产环境生成新的机密In the local terminal, generate a new secret for the Rails production environment in Azure.

rails secret

配置 Rails 生产环境所需的变量。Configure the variables required by Rails production environment.

在以下 Azure CLI 命令中,将两个 <output-of-rails-secret> 占位符替换在本地终端中生成的新机密密钥。In the following Azure CLI command, replace the two <output-of-rails-secret> placeholders with the new secret key you generated in the local terminal.

az webapp config appsettings set --name <app-name> --resource-group myResourceGroup --settings RAILS_MASTER_KEY="<output-of-rails-secret>" SECRET_KEY_BASE="<output-of-rails-secret>" RAILS_SERVE_STATIC_FILES="true" ASSETS_PRECOMPILE="true"

ASSETS_PRECOMPILE="true" 告知默认 Ruby 容器要在每次进行 Git 部署时预编译资产。ASSETS_PRECOMPILE="true" tells the default Ruby container to precompile assets at each Git deployment. 有关详细信息,请参阅预编译资产提供静态资产For more information, see Precompile assets and Serve static assets.

从 Git 推送到 AzurePush to Azure from Git

在本地终端中,将 Azure 远程功能添加到本地 Git 存储库。In the local terminal, add an Azure remote to your local Git repository.

git remote add azure <paste-copied-url-here>

推送到 Azure 远程功能以部署 Ruby on Rails 应用程序。Push to the Azure remote to deploy the Ruby on Rails application. 系统会提示输入前面在创建部署用户期间提供的密码。You are prompted for the password you supplied earlier as part of the creation of the deployment user.

git push azure master

在部署期间,Azure 应用服务会向 Git 告知其进度。During deployment, Azure App Service communicates its progress with Git.

Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 291 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Updating branch 'master'.
remote: Updating submodules.
remote: Preparing deployment for commit id 'a5e076db9c'.
remote: Running custom deployment command...
remote: Running deployment command...
...
< Output has been truncated for readability >

转到 Azure 应用Browse to the Azure app

浏览到 http://<app-name>.chinacloudsites.cn 并在列表中添加一些任务。Browse to http://<app-name>.chinacloudsites.cn and add a few tasks to the list.

标题为“任务”的 Ruby on Rails 应用示例的屏幕截图。

恭喜,你已在 Azure 应用服务中运行了一个数据驱动的 Ruby 应用。Congratulations, you're running a data-driven Ruby on Rails app in Azure App Service.

在本地更新模型和重新部署Update model locally and redeploy

在此步骤中,对 task 数据模型和 webapp 进行简单更改,然后将更新发布到 Azure。In this step, you make a simple change to the task data model and the webapp, and then publish the update to Azure.

对于任务方案,需要修改应用程序,以便能够将任务标记为已完成。For the tasks scenario, you modify the application so that you can mark a task as complete.

添加列Add a column

在终端中,导航到 Git 存储库中的根路径。In the terminal, navigate to the root of the Git repository.

生成新的迁移,用于将名为 Done 的布尔列添加到 Tasks 表:Generate a new migration that adds a boolean column called Done to the Tasks table:

rails generate migration AddDoneToTasks Done:boolean

此命令在 db/migrate 目录中生成新的迁移文件。This command generates a new migration file in the db/migrate directory.

在终端中运行 Rails 数据库迁移,以便在本地数据库中进行更改。In the terminal, run Rails database migrations to make the change in the local database.

rake db:migrate

更新应用程序逻辑Update application logic

打开 app/controllers/tasks_controller.rb 文件。Open the app/controllers/tasks_controller.rb file. 在该文件的末尾找到以下行:At the end of the file, find the following line:

params.require(:task).permit(:Description)

修改此行,以包含新的 Done 参数。Modify this line to include the new Done parameter.

params.require(:task).permit(:Description, :Done)

更新视图Update the views

打开 app/views/tasks/_form.html.erb 文件,即“编辑”窗体。Open the app/views/tasks/_form.html.erb file, which is the Edit form.

找到 <%=f.error_span(:Description) %> 行,并紧接在其下方插入以下代码:Find the line <%=f.error_span(:Description) %> and insert the following code directly below it:

<%= f.label :Done, :class => 'control-label col-lg-2' %>
<div class="col-lg-10">
  <%= f.check_box :Done, :class => 'form-control' %>
</div>

打开 app/views/tasks/show.html.erb 文件,即单记录“视图”页。Open the app/views/tasks/show.html.erb file, which is the single-record View page.

找到 <dd><%= @task.Description %></dd> 行,并紧接在其下方插入以下代码:Find the line <dd><%= @task.Description %></dd> and insert the following code directly below it:

  <dt><strong><%= model_class.human_attribute_name(:Done) %>:</strong></dt>
  <dd><%= check_box "task", "Done", {:checked => @task.Done, :disabled => true}%></dd>

打开 app/views/tasks/index.html.erb 文件,即所有记录的“索引”页。Open the app/views/tasks/index.html.erb file, which is the Index page for all records.

找到 <th><%= model_class.human_attribute_name(:Description) %></th> 行,并紧接在其下方插入以下代码:Find the line <th><%= model_class.human_attribute_name(:Description) %></th> and insert the following code directly below it:

<th><%= model_class.human_attribute_name(:Done) %></th>

在同一文件中找到 <td><%= task.Description %></td> 行,并紧接在其下方插入以下代码:In the same file, find the line <td><%= task.Description %></td> and insert the following code directly below it:

<td><%= check_box "task", "Done", {:checked => task.Done, :disabled => true} %></td>

在本地测试更改Test the changes locally

在本地终端中运行 Rails 服务器。In the local terminal, run the Rails server.

rails server

若要查看任务状态的变化,请导航到 http://localhost:3000 并添加或编辑项。To see the task status change, navigate to http://localhost:3000 and add or edit items.

将复选框添加到任务

若要停止 Rails 服务器,请在终端中键入 Ctrl + CTo stop the Rails server, type Ctrl + C in the terminal.

发布对 Azure 所做的更改Publish changes to Azure

在终端中,针对生产环境运行 Rails 数据库迁移,在 Azure 数据库中进行更改。In the terminal, run Rails database migrations for the production environment to make the change in the Azure database.

rake db:migrate RAILS_ENV=production

提交在 Git 中进行的所有更改,然后将代码更改推送到 Azure。Commit all the changes in Git, and then push the code changes to Azure.

git add .
git commit -m "added complete checkbox"
git push azure master

git push 完成后,请导航至 Azure 应用,测试新功能。Once the git push is complete, navigate to the Azure app and test the new functionality.

发布到 Azure 的模型和数据库更改

如果添加任何任务,则它们保留在数据库中。If you added any tasks, they are retained in the database. 更新数据架构不会改变现有数据。Updates to the data schema leave existing data intact.

流式传输诊断日志Stream diagnostic logs

若要访问应用服务中的应用程序代码内生成的控制台日志,请在 Azure CLI 中运行以下命令以打开诊断日志记录:To access the console logs generated from inside your application code in App Service, turn on diagnostics logging by running the following command in the Azure CLI:

az webapp log config --resource-group <resource-group-name> --name <app-name> --application-logging true --level Verbose

--level 的可能值为:ErrorWarningInfoVerbosePossible values for --level are: Error, Warning, Info, and Verbose. 每个后续级别包括上一个级别。Each subsequent level includes the previous level. 例如:Error 仅包含错误消息,Verbose 则包含所有消息。For example: Error includes only error messages, and Verbose includes all messages.

启用诊断日志记录功能以后,请运行以下命令来查看日志流:Once diagnostic logging is turned on, run the following command to see the log stream:

az webapp log tail --resource-group <resource-group-name> --name <app-name>

如果没有立即看到控制台日志,请在 30 秒后重新查看。If you don't see console logs immediately, check again in 30 seconds.

备注

也可通过浏览器在 https://<app-name>.scm.chinacloudsites.cn/api/logs/docker 中检查日志文件。You can also inspect the log files from the browser at https://<app-name>.scm.chinacloudsites.cn/api/logs/docker.

若要随时停止日志流式处理,请键入 Ctrl+CTo stop log streaming at any time, type Ctrl+C.

管理 Azure 应用Manage the Azure app

转到 Azure 门户管理已创建的应用。Go to the Azure portal to manage the app you created.

在左侧菜单中单击应用程序服务,然后单击 Azure 应用的名称。From the left menu, click App Services, and then click the name of your Azure app.

在门户中导航到 Azure 应用

这里我们可以看到应用的“概述”页。You see your app's Overview page. 在此处可以执行基本的管理任务,例如停止、启动、重启、浏览和删除。Here, you can perform basic management tasks like stop, start, restart, browse, and delete.

左侧菜单提供用于配置应用的页面。The left menu provides pages for configuring your app.

Azure 门户中的“应用服务”页

清理资源Clean up resources

若要清理资源,请运行以下命令:To clean up your resources, run the following command:

az group delete --name myResourceGroup

后续步骤Next steps

在本教程中,你了解了如何执行以下操作:In this tutorial, you learned how to:

  • 在 Azure 中创建一个 Postgres 数据库Create a Postgres database in Azure
  • 将 Ruby on Rails 应用连接到 PostgresConnect a Ruby on Rails app to Postgres
  • 将应用部署到 AzureDeploy the app to Azure
  • 更新数据模型并重新部署应用Update the data model and redeploy the app
  • 从 Azure 流式传输诊断日志Stream diagnostic logs from Azure
  • 在 Azure 门户中管理应用Manage the app in the Azure portal

继续学习下一篇教程,了解如何将自定义 DNS 名称映射到应用。Advance to the next tutorial to learn how to map a custom DNS name to your app.

或者,查看其他资源:Or, check out other resources: