如何在Linux上搭建Unity项目的Git触发式持续集成系统
Hey there! I’ve set up exactly this kind of automated Unity build pipeline on Ubuntu with Semaphore before, so let me walk you through every step clearly.
Unity doesn’t have an official APT repo, but we can use the Unity Hub CLI to manage installations cleanly—perfect for CI environments.
First, install all required system dependencies to avoid runtime errors:
sudo apt update && sudo apt install -y libgtk-3-0 libnss3 libasound2 libx11-xcb1 libxcb-dri3-0 libxss1 libxtst6 libatspi2.0-0 libuuid1 libgconf-2-4 libgbm1
Next, download and install the Unity Hub Deb package:
wget https://public-cdn.cloud.unity3d.com/hub/prod/UnityHub.deb sudo dpkg -i UnityHub.deb # Fix any missing dependencies if needed sudo apt --fix-broken install
Now install your project’s specific Unity version (replace 2022.3.10f1 with your project’s version) plus required modules (like linux-il2cpp for IL2CPP builds):
unityhub install --version 2022.3.10f1 --module linux-il2cpp
Finally, activate Unity (critical for headless builds). Use environment variables for credentials to keep them secure:
/opt/Unity/Hub/Editor/2022.3.10f1/Editor/Unity -batchmode -quit \ -serial "$UNITY_SERIAL" \ -username "$UNITY_EMAIL" \ -password "$UNITY_PASSWORD"
If you’re using a personal license, you might need to generate an offline license file beforehand (via Unity Hub on your local machine) and use the
-manualLicenseFile /path/to/license.ulfflag instead of online credentials—this avoids CI network issues.
Once Unity is set up, cloning your repo and running the build is straightforward.
First, pull your project from Git:
git clone YOUR_GIT_REPO_URL && cd YOUR_PROJECT_ROOT_FOLDER
Run the build command (adjust paths and target platform to match your project):
/opt/Unity/Hub/Editor/2022.3.10f1/Editor/Unity -batchmode -quit \ -projectPath . \ -buildTarget StandaloneLinux64 \ -buildOutput Builds/Linux \ -logFile /dev/stdout
Let’s break down the key flags:
-batchmode: Runs Unity without a GUI (essential for CI)-quit: Exits Unity automatically after the build finishes-buildTarget: Specifies the output platform (useStandaloneWindows64for Windows,StandaloneOSXfor macOS if needed)-buildOutput: The directory where your built game will be saved-logFile /dev/stdout: Redirects Unity logs to the console, so you can debug failures in your CI logs
Both tools work similarly since they use Ubuntu-based runners. Let’s use Semaphore as an example:
- Connect your Git repo: Create a new project in Semaphore and link your Unity repo.
- Add secure environment variables: Go to your project’s settings and add
UNITY_SERIAL,UNITY_EMAIL,UNITY_PASSWORD, andUNITY_VERSION(e.g.,2022.3.10f1). - Create a Semaphore config file: Add
.semaphore/semaphore.ymlto your repo with these steps:
version: v1.0 name: Unity Automated Build agent: machine: type: e1-standard-2 os_image: ubuntu2004 blocks: - name: Build Unity Project task: jobs: - name: Install & Build commands: # Install system dependencies - sudo apt update && sudo apt install -y libgtk-3-0 libnss3 libasound2 libx11-xcb1 libxcb-dri3-0 libxss1 libxtst6 libatspi2.0-0 libuuid1 libgconf-2-4 libgbm1 # Install Unity Hub - wget https://public-cdn.cloud.unity3d.com/hub/prod/UnityHub.deb - sudo dpkg -i UnityHub.deb - sudo apt --fix-broken install -y # Install specified Unity version - unityhub install --version "$UNITY_VERSION" --module linux-il2cpp # Activate Unity - /opt/Unity/Hub/Editor/"$UNITY_VERSION"/Editor/Unity -batchmode -quit -serial "$UNITY_SERIAL" -username "$UNITY_EMAIL" -password "$UNITY_PASSWORD" # Clone repo (Semaphore does this automatically, but included for clarity) - checkout # Run build - /opt/Unity/Hub/Editor/"$UNITY_VERSION"/Editor/Unity -batchmode -quit -projectPath . -buildTarget StandaloneLinux64 -buildOutput Builds/Linux -logFile /dev/stdout # Optional: Upload build to cloud storage (e.g., AWS S3) - aws s3 cp Builds/Linux s3://your-bucket/unity-builds/ --recursive
For Shippable, create a shippable.yml with nearly identical steps—just adjust the syntax to match Shippable’s configuration format.
Key Tips for Reliability
- Match Unity versions exactly: Even minor version mismatches can break builds, so ensure your CI installs the exact same version your project uses.
- Increase CI timeout: Unity builds can take 10-30 minutes, so set your CI runner’s timeout to at least 45 minutes.
- Clean up old builds: Add a step to delete previous build artifacts before starting a new build to save space.
内容的提问来源于stack exchange,提问作者Norman Percy

