Game Tech Tutorial
Published in

Game Tech Tutorial

How to Setup CI/CD Pipeline with Jenkins and Unity Test Runner? (Complete Tutorial)

Continuous Integration and Deployment allow you to have fewer bugs in games to increase your productivity.

With CI/CD, you not only can ensure the game code’s stability in every deployment phase but also can release games on time and within budget. In other words, you won't waste too much time in testing, so you can focus on creating interesting features to attract players.

However, it’s hard to find a complete tutorial to build CI/CD pipelines with Jenkins and Unity Test Runner.

This tutorial will guide you to set up your own CI/CD pipelines on Jenkins with Unity Test Runner from the ground up to improve your work efficiency.

Outline

Getting Started

Environment:

Install and Setup Jenkins

Run

brew install jenkins-lts

Note:

We are not going to use any plugins or 3-Party lib like fastlane to build ios and android because this is easy to debug and learn as a starting porint. And you can fully control to diagnose any error in conolse.

Of course, you can use fastlane in pipeline when needed.

Create Test Runner cases in Unity

Goto your folder → Right Click Mouse → Create → Assembly definition
→ Create PlayMode.asmdef and place it in /Assets/Tests/PlayMode

Setup PlayMode.asmdef as below

Then we will create a Unity Test Runner case as TestGame.cs in /Assets/Tests/PlayMode

TestGame.cs

using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.UI;
namespace TestGame
{
public class PlayModeTestGame
{
bool clicked = false;
[SetUp]
public void SetUp()
{
// Modify this with your first scene in the game UnityEngine.SceneManagement.SceneManager.LoadScene("<Your Game Scene>");
}
[UnityTest]
public IEnumerator TestMenu()
{
var gameObject = new GameObject();
string name = "start"; // Your Button Name
GameObject startButton = GameObject.Find(name);
Assert.NotNull(startButton);
var setupButton = startButton.GetComponent<Button>();
setupButton.onClick.AddListener(Clicked);
setupButton.onClick.Invoke();
Assert.IsTrue(clicked);
yield return new WaitForSeconds(2);
}private void Clicked()
{
clicked = true;
}
}
}

Note:

Test Your Unit Test

Next, we expect Jenkins can run the test case when making any changes in a PR.

Let’s start to set up this pipeline.

Setup Slack Message for Jenkins’s pipeline

Since we will integrate Slack messages in the pipeline, we can always be notified by the latest status via the slack message.

Fill in the following

Click Test Connection to check status

Next, we need to create a pipeline to check PR when someone pushes any changes to the source codes. At this moment, this actually will trigger a webhook to notify Jenkins’s pipeline.

Let’s set up the webhook for the PR pipeline.

Create a webhook in Github to notify Jenkins

Payload URL:

http://<user name>:<API Token>@<Jenkins IP>:<Jenkins Port>/job/<Your PR Pipeline Name>/buildWith Parameters?token=<your_toke_name>

Example:

http://test-user:1143acc4dd35ceeb678d213162565fb123@192.168.1.1:9001/job/my-pr/buildWithParameters? token=mytoken

Note:

Note:

Our pipelines need customized functions to build Android or iOS in Unity.

Remember that you need to download BuilderUtility.cs and place it in your project’s Editor folder for setting up pipelines.

Create a PR pipeline to build Android

This pipeline will run test cases and build games for Android when making any changes in PR.

Fill in the following

{
"pull_request": {
"number": 1,
"state": "closed",
"head": {
"ref": "feature/test-pr"
}
}
}

Let’s start to create a PR pipeline.

Modify Pipeline name, Machine’s user Name, Github URL, and Unity Version.

// Pipeline name
def jobName="your-game-pr"
// Machine's user Name
def machine_user_name="your_user_name"
// Your Github URL
def build_repo="git@github.com:gcoolmaneric/jenkins-game-ci.git"
// Your Unity Version
// Ex: "2020.1.15f2"
def unity_version="<Your unity verison>"
// Distributed URL via https://distribution.com
def DEPLOY_URL=""
// deploy_gate_token
def deploy_gate_token="<deploy_gate_token>"
// deploy_gate_upload url
def deploy_gate_upload_url="https://deploygate.com/api/users/<deploy_group_name>/apps"

Note:

First, we need to understand the following functions in the pipeline.

Function: parseJSON is used to parse request payload from Github’s webhook when making any change in PR.

Function: sendSlackMessage can send messages to Slack Channel for reporting any changes.

Then we start to write the pipeline.

1. Initial environment parameters

Note

2. Clean up workspace and clone branch

Note:

3. Run Test cases in Unity Test Runner

Note

4. Build Android in mono or IL2CPP

Note:

5. Upload App to the Cloud

Note:

Lastly, Uncheck Use Groovy Sandbox.

Create a PR pipeline to build iOS

This pipeline is as same as the previous one, so you can copy the previous pipeline and update the following.

Summary

Congratulations! You have learned how to create Test cases in Unity Test Runner and set up CI/CD pipelines to build games for Android or iOS.

You can download the PR pipeline here for reference.

I hope this tutorial will be helpful for you to save time building CI/CD pipeline to speed up your workflow.

Thanks for reading.

Next, you can refer below to set up Jenkins with high availability and scalability when needed.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eric Wei

Eric Wei

55 Followers

Senior Full Stack Engineer & Solution architecture | AWS, GCP, Azure | Cloud, Unity Game Development, SDK, DevOps, and more.