# 自动执行本地部署

> Automate the deployment of your Cloud Code module with the Unity Gaming Services CLI or project file.

该指南向您展示如何使用 Unity Gaming Services（Unity 游戏服务）(UGS) CLI 或项目文件自动部署 Cloud Code 模块。

如果要将部署集成到 CI/CD 管线中，请参阅[与 CI/CD 集成](./automation.md)。

## 先决条件##prerequisites

您需要一个 Cloud Code 模块和 UGS CLI 来自动执行本地部署：

1. 创建 [Cloud Code 模块](../getting-started)。
2. 按照[安装 CLI](https://services.docs.unity.com/guides/ugs-cli/latest/general/get-started/install-the-cli/)（Unity Gaming Services（Unity 游戏服务）CLI）的步骤进行 UGS CLI 的安装、配置和身份验证。

## 使用 UGS CLI 自动部署##automate-with-the-ugs-cli

UGS CLI 提供了可用于部署解决方案的 `deploy` 命令。`deploy` 命令将模块压缩并部署到 Cloud Code 服务。

要进行部署，请在项目目录中从终端运行以下命令：

```bash
ugs deploy <path_to_solution>
```

> **Note:**
>
> **注意**：为了支持编译，解决方案需要包含[主项目](./module-structure.md#main-project)的发布配置文件。如需了解更多信息，请参阅[部署 C# 解决方案](./write-modules/cli.md#deploy-c-solutions)。

## 使用项目文件自动部署##automate-using-the-project-file

如果您配置项目文件以便在构建时发布、压缩和部署项目，则可以使用项目文件自动执行 Cloud Code 模块的部署。

> **Note:**
>
> **注意**：这是一个先进的工作流程。对于大多数情况来说，UGS CLI [Deploy 命令](#automate-with-the-ugs-cli)就足够了。

如需了解更多信息，请参阅 Microsoft 指南：[了解项目文件](https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/web-deployment-in-the-enterprise/understanding-the-project-file)。

该脚本使用 [UGS CLI Deploy](./write-modules/cli.md#deploy-modules) 命令。

### 项目文件配置##project-file-configuration

您可以使用以下项目文件配置来自动执行 Cloud Code 模块的部署：

```xml
<Target Name="RemoveDirectory">
    <RemoveDir Directories="$(OutDir)Module/" />
 </Target>
<Target Name="PublishModule" DependsOnTargets="RemoveDirectory">
    <Exec Command="dotnet publish -r linux-x64 --no-self-contained -o $(OutDir)Module/$(AssemblyName)" />
</Target>
<Target Name="ZipModule" DependsOnTargets="PublishModule">
    <ZipDirectory SourceDirectory="$(OutDir)Module/$(AssemblyName)" DestinationFile="$(OutDir)Module/$(AssemblyName).ccm" />
</Target>
<Target Name="DeployModule" DependsOnTargets="ZipModule">
    <Exec Command="ugs deploy $(OutDir)/Module/$(AssemblyName).ccm" />
</Target>
```

将上述配置复制并粘贴到项目文件中。

* `RemoveDirectory` 目标从 `OutDir` 目录中移除 `Module` 目录。
* `PublishModule` 目标将模块发布到 `Module` 目录并生成所需的程序集。
* `ZipModule` 目标压缩 `Module` 目录。
* `DeployModule` 目标将模块部署到 Cloud Code 服务。

#### Ready to Run 编译（可选）##ready-to-run-compilation-optional

Ready to Run 编译是一项可缩短 Cloud Code 模块冷启动时间的可选功能。要检查此选项如何影响您的模块，请参阅 \[Ready to Run]\(./package-code.mdx#ready-to-run-r2r-compilation。

要启用 Ready to Run 编译，请修改 `PublishModule` 目标以包含 `PublishReadyToRun` 属性：

```xml
<Target Name="PublishModule" DependsOnTargets="RemoveDirectory">
    <Exec Command="dotnet publish -r linux-x64  -p:PublishReadyToRun=true --no-self-contained -o $(OutDir)Module/$(AssemblyName)" />
 </Target>
```

### 运行目标##running-targets

要运行这些目标，请在项目目录中从终端运行以下命令：

```bash
msbuild /t:DeployModule
```

如果未找到 `msbuild ` 命令，可能需要将 `msbuild` 路径添加到 `PATH` 环境变量。

> **Note:**
>
> **注意**：通过将 `DeployModule` 替换为另一个目标名称，可以单独运行任何其他目标。这些目标相互依赖，因此运行任何目标也会运行其所依赖的目标。

### 预期输出##expected-output

如果部署成功，则会显示以下输出：

```text
Deployed:
    ExampleModule.ccm
```
