Automate local deployment

This guide shows you how to automate the deployment of your Cloud Code module with the Unity Gaming Services (UGS) CLI or the project file.

If you want to integrate the deployment into your CI/CD pipeline, refer to Integrating with CI/CD.

Prerequisites

You need a Cloud Code module and the UGS CLI to automate local deployment:

  1. Create a Cloud Code module.
  2. Follow the steps in Install the CLI (Unity Gaming Services CLI) to install, configure and authenticate the UGS CLI.

Automate with the UGS CLI

The UGS CLI provides a deploy command that you can use to deploy a solution. The deploy command zips the module and deploys it to the Cloud Code service.

To deploy, run the following command from your terminal in your project directory:

ugs deploy <path_to_solution>

Note: To support compilation, the solution needs to contain a publish profile for the main project. For more information, refer to Deploy C# solutions.

Automate using the project file

You can use the project file to automate the deployment of your Cloud Code module if you configure the project file to publish, zip and deploy your project on build.

Note: This is an advanced workflow. For most cases, the UGS CLI Deploy command is sufficient.

For more information, refer to Microsoft guide on Understanding the project file.

The script uses the UGS CLI Deploy command.

Project file configuration

You can use the following project file configuration to automate the deployment of your Cloud Code module:

<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>

Copy and paste the above configuration into your project file.

  • The RemoveDirectory target removes the Module directory from the OutDir directory.
  • The PublishModule target publishes the module to the Module directory and produces the required assemblies.
  • The ZipModule target zips the Module directory.
  • The DeployModule target deploys the module to the Cloud Code service.

Ready to Run compilation (optional)

Ready to Run compilation is an optional feature that improves the cold start time of your Cloud Code module. To check how this option affects your module, refer to Ready to Run.

To enable Ready to Run compilation, modify the PublishModule target to include the PublishReadyToRun property:

<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

To run the targets, run the following command from your terminal in your project directory:

msbuild /t:DeployModule

If the msbuild command is not found, you may need to add the msbuild path to your PATH environment variable.

Note: You can run any of the other targets individually if you replace DeployModule with another target name. The targets are dependent on each other, so any target that you run also runs the targets it depends on.

Expected output

If the deployment is successful, the following output is displayed:

Deployed:
    ExampleModule.ccm