Ruby 开发堆栈目前仅支持 Ruby on Rails。 如果想要使用不同的平台(如 Sinatra),或者想要使用不受支持的 Ruby 版本,则需要 在自定义容器中运行它 。

如果没有 Azure 订阅 ,请在开始之前创建一个 Azure 免费帐户 。

Azure CLI

如果使用 Cloud Shell,则无需安装这些工具。

Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。 可以使用 Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。

若要启动 Azure Cloud Shell,请执行以下操作:

示例/链接
  • 选择代码块(或命令块)上的“复制”按钮以复制代码或命令。

  • 在 Windows 和 Linux 上选择 Ctrl+Shift+V,或在 macOS 上选择 Cmd+Shift+V 将代码或命令粘贴到 Cloud Shell 会话中。

  • 选择“Enter”运行代码或命令。

    本快速入门教程介绍了如何使用 Cloud Shell 将 Ruby on Rails 应用部署到 Linux 上的应用服务。

  • 在终端窗口中,将示例应用程序克隆到本地计算机,并导航到包含示例代码的目录。

    git clone https://github.com/Azure-Samples/ruby-docs-hello-world
    cd ruby-docs-hello-world
    
  • 确保默认分支为 main。

    git branch -m main
    

    应用服务不需要更改分支名称。 但是,由于许多存储库将其默认分支更改为 main,因此本教程还介绍如何从 main 部署存储库。 有关详细信息,请参阅更改部署分支。

    在本地运行应用程序

    如果你想要在本地运行应用程序以查看其工作方式,请在本地克隆存储库并执行以下步骤。

    由于与 Ruby 的版本冲突,这些步骤无法在 Azure Cloud Shell 中运行。

  • 安装所需的 gem。 示例中包含了 Gemfile,因此只需运行以下命令:

    bundle install
    
  • 安装 gem 后,启动应用:

    bundle exec rails server
    
  • 使用 Web 浏览器导航到 http://localhost:3000 以在本地测试该应用。

    配置部署用户

    可以使用“deployment user”将 FTP 和本地 Git 部署到 Azure Web 应用。 配置部署用户之后,可对所有 Azure 部署使用此用户。 帐户级部署用户名和密码不同于 Azure 订阅凭据。

    若要配置部署用户,请在 Azure Cloud Shell 中运行 az webapp deployment user set 命令。 将 <username> 和 <password> 替换为部署用户的用户名和密码。

  • 用户名在 Azure 中必须唯一,并且为了本地Git推送,不能包含“@”符号。
  • 密码必须至少为 8 个字符,且具有字母、数字和符号这三种元素中的两种。
  • az webapp deployment user set --user-name <username> --password <password>
    

    JSON 输出会将该密码显示为 null。 如果收到 'Conflict'. Details: 409 错误,请更改用户名。 如果收到 'Bad Request'. Details: 400 错误,请使用更强的密码。

    请记录你要用于部署 Web 应用的用户名和密码。

    创建资源组

    资源组是在其中部署和管理 Azure 资源(例如 Web 应用、数据库和存储帐户)的逻辑容器。 例如,可以选择在使用完之后通过一个简单的步骤删除整个资源组。

    在 Cloud Shell 中,使用 az group create 命令创建资源组。 以下示例在“西欧”位置创建名为“myResourceGroup”的资源组。 若要查看“基本”层中 Linux 上的应用服务支持的所有位置,请运行 az appservice list-locations --sku B1 --linux-workers-enabled 命令。

    az group create --name myResourceGroup --location "West Europe"
    

    通常在附近的区域中创建资源组和资源。

    此命令完成后,JSON 输出会显示资源组属性。

    创建 Azure 应用服务计划

    在 Cloud Shell 中,使用 az appservice plan create 命令创建一个应用服务计划。

    以下示例在免费定价层中创建名为 myAppServicePlan 的应用服务计划:

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

    创建应用服务计划后,Azure CLI 会显示类似于以下示例的信息:

    "freeOfferExpirationTime": null, "geoRegion": "West Europe", "hostingEnvironmentProfile": null, "id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan", "kind": "linux", "location": "West Europe", "maximumNumberOfWorkers": 1, "name": "myAppServicePlan", < JSON data removed for brevity. > "targetWorkerSizeId": 0, "type": "Microsoft.Web/serverfarms", "workerTierName": null
  • 在 应用服务计划中创建一个 Web 应用。

    在 Cloud Shell 中可以使用 az webapp create 命令。 在以下示例中,将 <app-name> 替换为全局唯一的应用名称(有效字符是 a-z、0-9 和 -)。 运行时设置为 RUBY|2.7。 若要查看所有受支持的运行时,请运行 az webapp list-runtimes --os linux。

    az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app-name> --runtime 'RUBY|2.7' --deployment-local-git
    

    创建 Web 应用后,Azure CLI 会显示类似于以下示例的输出:

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

    现在你已经创建了一个新的空 Web 应用并启用了 Git 部署。

    Git 远程的 URL 将显示在 deploymentLocalGitUrl 属性中,其格式为 https://<username>@<app-name>.scm.azurewebsites.net/<app-name>.git。 保存此 URL,后续将会用到。

  • 由于要部署 main 分支,因此需要将应用服务应用的默认部署分支设置为 main(请参阅main)。 在 Cloud Shell 中,使用 az webapp config appsettings set 命令设置 DEPLOYMENT_BRANCH 应用设置。

    az webapp config appsettings set --name <app-name> --resource-group myResourceGroup --settings DEPLOYMENT_BRANCH='main'
    
  • 回到本地终端窗口,将 Azure 远程功能添加到本地 Git 存储库。 将 <deploymentLocalGitUrl-from-create-step> 替换为创建应用时使用的 deploymentLocalGitUrl 值。

    git remote add azure <deploymentLocalGitUrl-from-create-step>
    
  • 使用以下命令推送到 Azure 远程库以部署应用。 当 Git 凭据管理器提示输入凭据时,请确保输入在配置部署用户中创建的凭据,而不是用于登录到 Azure 门户的凭据。

    git push azure main
    

    此命令可能需要花费几分钟时间运行。 运行时,该命令会显示类似于以下示例的信息:

    remote: Using turbolinks 5.2.0 remote: Using uglifier 4.1.20 remote: Using web-console 3.7.0 remote: Bundle complete! 18 Gemfile dependencies, 78 gems now installed. remote: Bundled gems are installed into `/tmp/bundle` remote: Zipping up bundle contents remote: ....... remote: ~/site/repository remote: Finished successfully. remote: Running post deployment command(s)... remote: Deployment successful. remote: App container will begin restart within 10 seconds. To https://<app-name>.scm.azurewebsites.net/<app-name>.git a6e73a2..ae34be9 main -> main
  • 部署完成后,请等待大约 10 秒,然后重启 Web 应用,再导航到 Web 应用并验证结果。

    http://<app-name>.azurewebsites.net
    

    应用重启时,可能会在浏览器中看到 HTTP 状态代码 Error 503 Server unavailable 或在默认页面中看到 Hey, Ruby developers!。 可能需要花费几分钟时间才能完全重启应用。

  • 在 Azure Cloud Shell 中启动文本编辑器,编辑文件 app/controllers/application_controller.rb。 编辑 ApplicationController 类,使其显示“Hello world from Azure App Service on Linux!” 而不是“Hello from Azure App Service on Linux!”。

    class ApplicationController < ActionController::Base
        def hello
            render html: "Hello world from Azure App Service on Linux!"
    
  • 保存并关闭该文件。

  • 使用以下命令将更改提交到 Git:

    git add .
    git commit -m "Hello world"
    git push azure main
    
  • 编辑 ApplicationController 类,使其显示“Hello world from Azure App Service on Linux!” 而不是“Hello from Azure App Service on Linux!”。

    class ApplicationController < ActionController::Base
        def hello
            render html: "Hello world from Azure App Service on Linux!"
    
  • 从“源代码管理”菜单中,选择“暂存更改”按钮以暂存更改。

  • 输入提交消息,如 Hello world。 然后选择“提交和推送”。

  • 完成部署后,返回到“浏览到应用”步骤期间打开的浏览器窗口,然后刷新页面。

  •