Verified Commit 54500b97 authored by Sebastian Höffner's avatar Sebastian Höffner
Browse files
parent cf2ba4a5
Pipeline #131974 canceled with stages
stages:
- build_and_test
- deploy
# If you are looking for a place where to add 'UNITY_LICENSE_FILE' and other secrets, please visit your project's gitlab page:
# settings > CI/CD > Variables instead
variables:
BUILD_NAME: PhysicsSimulator
UNITY_ACTIVATION_FILE: ./unity3d.alf
UNITY_VERSION: "2019.4.13f1"
IMAGE: unityci/editor # https://hub.docker.com/r/unityci/editor
IMAGE_VERSION: "0.9.0" # https://github.com/game-ci/docker/releases
UNITY_DIR: $CI_PROJECT_DIR # this needs to be an absolute path. Defaults to the root of your tree.
image: $IMAGE:$UNITY_VERSION-base-$IMAGE_VERSION
.unity_before_script: &unity_before_script
before_script:
- chmod +x ./ci/before_script.sh && ./ci/before_script.sh
.cache: &cache
cache:
key: "$CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG-$TEST_PLATFORM"
paths:
- $UNITY_DIR/Library/
.license: &license
rules:
- if: '$UNITY_LICENSE != null'
when: always
# run this job when you need to request a license
# you may need to follow activation steps from documentation
get-activation-file:
rules:
- if: '$UNITY_LICENSE == null'
when: manual
stage: build_and_test
script:
- chmod +x ./ci/get_activation_file.sh && ./ci/get_activation_file.sh
artifacts:
paths:
- $UNITY_ACTIVATION_FILE
expire_in: 10 min # Expiring this as artifacts may contain sensitive data and should not be kept public
.test: &test
stage: build_and_test
<<: *unity_before_script
<<: *cache
<<: *license
script:
- chmod +x ./ci/test.sh && ./ci/test.sh
artifacts:
paths:
- $UNITY_DIR/$TEST_PLATFORM-results.xml
- $UNITY_DIR/$TEST_PLATFORM-coverage/
# https://gitlab.com/gableroux/unity3d-gitlab-ci-example/-/issues/83
# you may need to remove or replace these to fit your need if you are using your own runners
tags:
- gitlab-org
coverage: /<Linecoverage>(.*?)</Linecoverage>/
test-playmode:
<<: *test
variables:
TEST_PLATFORM: playmode
test-editmode:
<<: *test
variables:
TEST_PLATFORM: editmode
.build: &build
stage: build_and_test
<<: *unity_before_script
<<: *cache
<<: *license
script:
- chmod +x ./ci/build.sh && ./ci/build.sh
artifacts:
paths:
- $UNITY_DIR/Builds/
# https://gitlab.com/gableroux/unity3d-gitlab-ci-example/-/issues/83
# you may need to remove or replace these to fit your need if you are using your own runners
tags:
- gitlab-org
build-StandaloneLinux64:
<<: *build
variables:
BUILD_TARGET: StandaloneLinux64
build-StandaloneLinux64-il2cpp:
<<: *build
image: $IMAGE:$UNITY_VERSION-linux-il2cpp-$IMAGE_VERSION
variables:
BUILD_TARGET: StandaloneLinux64
SCRIPTING_BACKEND: IL2CPP
build-StandaloneOSX:
<<: *build
image: $IMAGE:$UNITY_VERSION-mac-mono-$IMAGE_VERSION
variables:
BUILD_TARGET: StandaloneOSX
#Note: build target names changed in recent versions, use this for versions < 2017.2:
# build-StandaloneOSXUniversal:
# <<: *build
# variables:
# BUILD_TARGET: StandaloneOSXUniversal
build-StandaloneWindows64:
<<: *build
image: $IMAGE:$UNITY_VERSION-windows-mono-$IMAGE_VERSION
variables:
BUILD_TARGET: StandaloneWindows64
# For webgl support, you need to set Compression Format to Disabled for v0.9. See https://github.com/game-ci/docker/issues/75
build-WebGL:
<<: *build
image: $IMAGE:$UNITY_VERSION-webgl-$IMAGE_VERSION
# Temporary workaround for https://github.com/game-ci/docker/releases/tag/v0.9 and webgl support in current project to prevent errors with missing ffmpeg
before_script:
- chmod +x ./ci/before_script.sh && ./ci/before_script.sh
- apt-get update && apt-get install ffmpeg -y
variables:
BUILD_TARGET: WebGL
build-android:
<<: *build
image: $IMAGE:$UNITY_VERSION-android-$IMAGE_VERSION
variables:
BUILD_TARGET: Android
BUNDLE_VERSION_CODE: $CI_PIPELINE_IID
BUILD_APP_BUNDLE: "false"
build-android-il2cpp:
<<: *build
image: $IMAGE:$UNITY_VERSION-android-$IMAGE_VERSION
variables:
BUILD_TARGET: Android
BUNDLE_VERSION_CODE: $CI_PIPELINE_IID
BUILD_APP_BUNDLE: "false"
SCRIPTING_BACKEND: IL2CPP
#deploy-android:
# stage: deploy
# image: ruby
# script:
# - cd $UNITY_DIR/Builds/Android
# - echo $GPC_TOKEN > gpc_token.json
# - gem install bundler
# - bundle install
# - fastlane supply --aab $BUILD_NAME.aab --track internal --package_name com.youcompany.yourgame --json_key ./gpc_token.json
# needs: ["build-android"]
build-ios-xcode:
<<: *build
image: $IMAGE:$UNITY_VERSION-ios-$IMAGE_VERSION
variables:
BUILD_TARGET: iOS
#build-and-deploy-ios:
# stage: deploy
# script:
# - cd $UNITY_DIR/Builds/iOS/$BUILD_NAME
# - pod install
# - fastlane ios beta
# tags:
# - ios
# - mac
# needs: ["build-ios-xcode"]
pages:
image: alpine:latest
stage: deploy
script:
- mv "$UNITY_DIR/Builds/WebGL/${BUILD_NAME}" public
artifacts:
paths:
- public
only:
- master
workflow:
rules:
- if: $CI_MERGE_REQUEST_ID
when: never
- if: $CI_COMMIT_TAG
when: never
- when: always
using UnityEditor;
using System.Linq;
using System;
using System.IO;
static class BuildCommand
{
private const string KEYSTORE_PASS = "KEYSTORE_PASS";
private const string KEY_ALIAS_PASS = "KEY_ALIAS_PASS";
private const string KEY_ALIAS_NAME = "KEY_ALIAS_NAME";
private const string KEYSTORE = "keystore.keystore";
private const string BUILD_OPTIONS_ENV_VAR = "BuildOptions";
private const string ANDROID_BUNDLE_VERSION_CODE = "BUNDLE_VERSION_CODE";
private const string ANDROID_APP_BUNDLE = "BUILD_APP_BUNDLE";
private const string SCRIPTING_BACKEND_ENV_VAR = "SCRIPTING_BACKEND";
static string GetArgument(string name)
{
string[] args = Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length; i++)
{
if (args[i].Contains(name))
{
return args[i + 1];
}
}
return null;
}
static string[] GetEnabledScenes()
{
return (
from scene in EditorBuildSettings.scenes
where scene.enabled
where !string.IsNullOrEmpty(scene.path)
select scene.path
).ToArray();
}
static BuildTarget GetBuildTarget()
{
string buildTargetName = GetArgument("customBuildTarget");
Console.WriteLine(":: Received customBuildTarget " + buildTargetName);
if (buildTargetName.ToLower() == "android")
{
#if !UNITY_5_6_OR_NEWER
// https://issuetracker.unity3d.com/issues/buildoptions-dot-acceptexternalmodificationstoplayer-causes-unityexception-unknown-project-type-0
// Fixed in Unity 5.6.0
// side effect to fix android build system:
EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Internal;
#endif
}
if (buildTargetName.TryConvertToEnum(out BuildTarget target))
return target;
Console.WriteLine($":: {nameof(buildTargetName)} \"{buildTargetName}\" not defined on enum {nameof(BuildTarget)}, using {nameof(BuildTarget.NoTarget)} enum to build");
return BuildTarget.NoTarget;
}
static string GetBuildPath()
{
string buildPath = GetArgument("customBuildPath");
Console.WriteLine(":: Received customBuildPath " + buildPath);
if (buildPath == "")
{
throw new Exception("customBuildPath argument is missing");
}
return buildPath;
}
static string GetBuildName()
{
string buildName = GetArgument("customBuildName");
Console.WriteLine(":: Received customBuildName " + buildName);
if (buildName == "")
{
throw new Exception("customBuildName argument is missing");
}
return buildName;
}
static string GetFixedBuildPath(BuildTarget buildTarget, string buildPath, string buildName)
{
if (buildTarget.ToString().ToLower().Contains("windows")) {
buildName += ".exe";
} else if (buildTarget == BuildTarget.Android) {
#if UNITY_2018_3_OR_NEWER
buildName += EditorUserBuildSettings.buildAppBundle ? ".aab" : ".apk";
#else
buildName += ".apk";
#endif
}
return buildPath + buildName;
}
static BuildOptions GetBuildOptions()
{
if (TryGetEnv(BUILD_OPTIONS_ENV_VAR, out string envVar)) {
string[] allOptionVars = envVar.Split(',');
BuildOptions allOptions = BuildOptions.None;
BuildOptions option;
string optionVar;
int length = allOptionVars.Length;
Console.WriteLine($":: Detecting {BUILD_OPTIONS_ENV_VAR} env var with {length} elements ({envVar})");
for (int i = 0; i < length; i++) {
optionVar = allOptionVars[i];
if (optionVar.TryConvertToEnum(out option)) {
allOptions |= option;
}
else {
Console.WriteLine($":: Cannot convert {optionVar} to {nameof(BuildOptions)} enum, skipping it.");
}
}
return allOptions;
}
return BuildOptions.None;
}
// https://stackoverflow.com/questions/1082532/how-to-tryparse-for-enum-value
static bool TryConvertToEnum<TEnum>(this string strEnumValue, out TEnum value)
{
if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
{
value = default;
return false;
}
value = (TEnum)Enum.Parse(typeof(TEnum), strEnumValue);
return true;
}
static bool TryGetEnv(string key, out string value)
{
value = Environment.GetEnvironmentVariable(key);
return !string.IsNullOrEmpty(value);
}
static void SetScriptingBackendFromEnv(BuildTarget platform) {
var targetGroup = BuildPipeline.GetBuildTargetGroup(platform);
if (TryGetEnv(SCRIPTING_BACKEND_ENV_VAR, out string scriptingBackend)) {
if (scriptingBackend.TryConvertToEnum(out ScriptingImplementation backend)) {
Console.WriteLine($":: Setting ScriptingBackend to {backend}");
PlayerSettings.SetScriptingBackend(targetGroup, backend);
} else {
string possibleValues = string.Join(", ", Enum.GetValues(typeof(ScriptingImplementation)).Cast<ScriptingImplementation>());
throw new Exception($"Could not find '{scriptingBackend}' in ScriptingImplementation enum. Possible values are: {possibleValues}");
}
} else {
var defaultBackend = PlayerSettings.GetDefaultScriptingBackend(targetGroup);
Console.WriteLine($":: Using project's configured ScriptingBackend (should be {defaultBackend} for tagetGroup {targetGroup}");
}
}
static void PerformBuild()
{
Console.WriteLine(":: Performing build");
var buildTarget = GetBuildTarget();
if (buildTarget == BuildTarget.Android) {
HandleAndroidAppBundle();
HandleAndroidBundleVersionCode();
HandleAndroidKeystore();
}
var buildPath = GetBuildPath();
var buildName = GetBuildName();
var buildOptions = GetBuildOptions();
var fixedBuildPath = GetFixedBuildPath(buildTarget, buildPath, buildName);
SetScriptingBackendFromEnv(buildTarget);
var buildReport = BuildPipeline.BuildPlayer(GetEnabledScenes(), fixedBuildPath, buildTarget, buildOptions);
if (buildReport.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
throw new Exception($"Build ended with {buildReport.summary.result} status");
Console.WriteLine(":: Done with build");
}
private static void HandleAndroidAppBundle()
{
if (TryGetEnv(ANDROID_APP_BUNDLE, out string value))
{
#if UNITY_2018_3_OR_NEWER
if (bool.TryParse(value, out bool buildAppBundle))
{
EditorUserBuildSettings.buildAppBundle = buildAppBundle;
Console.WriteLine($":: {ANDROID_APP_BUNDLE} env var detected, set buildAppBundle to {value}.");
}
else
{
Console.WriteLine($":: {ANDROID_APP_BUNDLE} env var detected but the value \"{value}\" is not a boolean.");
}
#else
Console.WriteLine($":: {ANDROID_APP_BUNDLE} env var detected but does not work with lower Unity version than 2018.3");
#endif
}
}
private static void HandleAndroidBundleVersionCode()
{
if (TryGetEnv(ANDROID_BUNDLE_VERSION_CODE, out string value))
{
if (int.TryParse(value, out int version))
{
PlayerSettings.Android.bundleVersionCode = version;
Console.WriteLine($":: {ANDROID_BUNDLE_VERSION_CODE} env var detected, set the bundle version code to {value}.");
}
else
Console.WriteLine($":: {ANDROID_BUNDLE_VERSION_CODE} env var detected but the version value \"{value}\" is not an integer.");
}
}
private static void HandleAndroidKeystore()
{
#if UNITY_2019_1_OR_NEWER
PlayerSettings.Android.useCustomKeystore = false;
#endif
if (!File.Exists(KEYSTORE)) {
Console.WriteLine($":: {KEYSTORE} not found, skipping setup, using Unity's default keystore");
return;
}
PlayerSettings.Android.keystoreName = KEYSTORE;
string keystorePass;
string keystoreAliasPass;
if (TryGetEnv(KEY_ALIAS_NAME, out string keyaliasName)) {
PlayerSettings.Android.keyaliasName = keyaliasName;
Console.WriteLine($":: using ${KEY_ALIAS_NAME} env var on PlayerSettings");
} else {
Console.WriteLine($":: ${KEY_ALIAS_NAME} env var not set, using Project's PlayerSettings");
}
if (!TryGetEnv(KEYSTORE_PASS, out keystorePass)) {
Console.WriteLine($":: ${KEYSTORE_PASS} env var not set, skipping setup, using Unity's default keystore");
return;
}
if (!TryGetEnv(KEY_ALIAS_PASS, out keystoreAliasPass)) {
Console.WriteLine($":: ${KEY_ALIAS_PASS} env var not set, skipping setup, using Unity's default keystore");
return;
}
#if UNITY_2019_1_OR_NEWER
PlayerSettings.Android.useCustomKeystore = true;
#endif
PlayerSettings.Android.keystorePass = keystorePass;
PlayerSettings.Android.keyaliasPass = keystoreAliasPass;
}
}
#!/usr/bin/env bash
set -e
set -x
mkdir -p /root/.cache/unity3d
mkdir -p /root/.local/share/unity3d/Unity/
set +x
UPPERCASE_BUILD_TARGET=${BUILD_TARGET^^};
if [ $UPPERCASE_BUILD_TARGET = "ANDROID" ]
then
if [ -n $ANDROID_KEYSTORE_BASE64 ]
then
echo '$ANDROID_KEYSTORE_BASE64 found, decoding content into keystore.keystore'
echo $ANDROID_KEYSTORE_BASE64 | base64 --decode > keystore.keystore
else
echo '$ANDROID_KEYSTORE_BASE64'" env var not found, building with Unity's default debug keystore"
fi
fi
LICENSE="UNITY_LICENSE_"$UPPERCASE_BUILD_TARGET
if [ -z "${!LICENSE}" ]
then
echo "$LICENSE env var not found, using default UNITY_LICENSE env var"
LICENSE=UNITY_LICENSE
else
echo "Using $LICENSE env var"
fi
echo "Writing $LICENSE to license file /root/.local/share/unity3d/Unity/Unity_lic.ulf"
echo "${!LICENSE}" | tr -d '\r' > /root/.local/share/unity3d/Unity/Unity_lic.ulf
set -x
#!/usr/bin/env bash
set -e
set -x
echo "Building for $BUILD_TARGET"
export BUILD_PATH=$UNITY_DIR/Builds/$BUILD_TARGET/
mkdir -p $BUILD_PATH
${UNITY_EXECUTABLE:-xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' unity-editor} \
-projectPath $UNITY_DIR \
-quit \
-batchmode \
-nographics \
-buildTarget $BUILD_TARGET \
-customBuildTarget $BUILD_TARGET \
-customBuildName $BUILD_NAME \
-customBuildPath $BUILD_PATH \
-executeMethod BuildCommand.PerformBuild \
-logFile /dev/stdout
UNITY_EXIT_CODE=$?
if [ $UNITY_EXIT_CODE -eq 0 ]; then
echo "Run succeeded, no failures occurred";
elif [ $UNITY_EXIT_CODE -eq 2 ]; then
echo "Run succeeded, some tests failed";
elif [ $UNITY_EXIT_CODE -eq 3 ]; then
echo "Run failure (other failure)";
else
echo "Unexpected exit code $UNITY_EXIT_CODE";
fi
ls -la $BUILD_PATH
[ -n "$(ls -A $BUILD_PATH)" ] # fail job if build folder is empty
#!/usr/bin/env bash
set -e
docker run \
-e BUILD_NAME \
-e UNITY_LICENSE \
-e BUILD_TARGET \
-e UNITY_USERNAME \
-e UNITY_PASSWORD \
-w /project/ \
-v $UNITY_DIR:/project/ \
$IMAGE_NAME \
/bin/bash -c "/project/ci/before_script.sh && /project/ci/build.sh"
#!/usr/bin/env bash
set -e
docker run \
-e UNITY_LICENSE \
-e TEST_PLATFORM \
-e UNITY_USERNAME \
-e UNITY_PASSWORD \
-w /project/ \
-v $UNITY_DIR:/project/ \
$IMAGE_NAME \
/bin/bash -c "/project/ci/before_script.sh && /project/ci/test.sh"
#!/usr/bin/env bash
activation_file=${UNITY_ACTIVATION_FILE:-./unity3d.alf}
if [[ -z "${UNITY_USERNAME}" ]] || [[ -z "${UNITY_PASSWORD}" ]]; then
echo "UNITY_USERNAME or UNITY_PASSWORD environment variables are not set, please refer to instructions in the readme and add these to your secret environment variables."
exit 1
fi
xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' \
unity-editor \
-logFile /dev/stdout \
-batchmode \
-nographics \
-username "$UNITY_USERNAME" -password "$UNITY_PASSWORD" |
tee ./unity-output.log
cat ./unity-output.log |
grep 'LICENSE SYSTEM .* Posting *' |
sed 's/.*Posting *//' > "${activation_file}"
# Fail job if unity.alf is empty
ls "${UNITY_ACTIVATION_FILE:-./unity3d.alf}"
exit_code=$?
if [[ ${exit_code} -eq 0 ]]; then
echo ""
echo ""
echo "### Congratulations! ###"
echo "${activation_file} was generated successfully!"
echo ""
echo "### Next steps ###"
echo ""
echo "Complete the activation process manually"
echo ""
echo " 1. Download the artifact which should contain ${activation_file}"
echo " 2. Visit https://license.unity3d.com/manual"
echo " 3. Upload ${activation_file} in the form"
echo " 4. Answer questions (unity pro vs personal edition, both will work, just pick the one you use)"
echo " 5. Download 'Unity_v2019.x.ulf' file (year should match your unity version here, 'Unity_v2018.x.ulf' for 2018, etc.)"
echo " 6. Copy the content of 'Unity_v2019.x.ulf' license file to your CI's environment variable 'UNITY_LICENSE'. (Open your project's parameters > CI/CD > Variables and add 'UNITY_LICENSE' as the key and paste the content of the license file into the value)"
echo ""
echo "Once you're done, hit retry on the pipeline where other jobs failed, or just push another commit. Things should be green"
echo ""
echo "(optional) For more details on why this is not fully automated, visit https://gitlab.com/gableroux/unity3d-gitlab-ci-example/issues/73"
else
echo "License file could not be found at ${UNITY_ACTIVATION_FILE:-./unity3d.alf}"
fi
exit $exit_code
#!/usr/bin/env bash