如何在 Azure Web 应用上创建和启动井字游戏

发布:2024-09-19 10:16 阅读:41 点赞:0

在本指南中,我们将逐步介绍如何将 Tic Tac Toe 游戏部署到 Azure Web App 上。以下步骤将帮助你完成从项目初始化、推送到 GitHub,到最终在 Azure 上托管游戏的全过程。

一. 项目初始化并推送到 GitHub

  1. 初始化 Git 仓库,并将代码推送到 GitHub。
# 初始化 Git 仓库
git init

# 添加所有文件到仓库
git add .

# 提交更改
git commit -m "Initial Tic Tac Toe commit"

# 添加远程 GitHub 仓库并推送代码
git remote add origin 
git push -u origin master

git init:初始化一个本地 Git 仓库。
git add .:将当前文件夹中的所有文件添加到 Git 暂存区。
git commit -m "Initial Tic Tac Toe commit":提交更改并添加提交信息。
git remote add origin :添加远程 GitHub 仓库。
git push -u origin master:将代码推送到 GitHub 仓库中的 master 分支。

二. 登录 Azure

  1. 使用 Azure CLI 登录 Azure。
az login

az login:使用 Azure CLI 登录 Azure。

三. 创建资源组

  1. 创建资源组来托管你的 Azure 资源。
az group create \
    --name TicTacToeGroup \
    --location "East US"

az group create:创建一个新的资源组。
--name:指定资源组的名称。
--location:指定资源组的区域。

四. 创建应用服务计划

  1. 创建一个应用服务计划,用来定义托管 Web 应用的配置和区域。
az appservice plan create \
    --name TicTacToePlan \
    --resource-group TicTacToeGroup \
    --sku FREE

az appservice plan create:创建一个应用服务计划。
--name:定义计划名称。
--resource-group:指定所属资源组。
--sku FREE:选择免费层级的服务。

五. 创建 Web 应用

  1. 在 Azure 上创建 Web 应用以托管你的游戏。
az webapp create \
    --resource-group TicTacToeGroup \
    --plan TicTacToePlan \
    --name TicTacToeGameApp \
    --runtime "node|16-lts"

az webapp create:创建 Web 应用。
--name:指定应用名称。
--runtime:定义应用的运行时环境。

六. 配置 GitHub 部署

  1. 将 Web 应用配置为从 GitHub 自动部署代码。
az webapp deployment source config \
    --name TicTacToeGameApp \
    --resource-group TicTacToeGroup \
    --repo-url  \
    --branch master \
    --manual-integration

az webapp deployment source config:配置源代码部署。
--repo-url:指定 GitHub 仓库的 URL。
--branch:设置要部署的分支。
--manual-integration:启用手动集成。

七. 访问你的游戏

  1. 部署完成后,你可以通过以下 URL 访问你的游戏:
https://.azurewebsites.net

你已经成功将 Tic Tac Toe 游戏部署到 Azure Web App 上!

重新开始游戏