diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index c10fca6..c213b9d 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -32,6 +32,18 @@ pre-steps: # Move the demo project to the project folder mv ../Unity2D-Demo-Game-CI-CD ./Unity2D-Demo-Game-CI-CD + custom-build-method: &custom-build-method + - run: + name: "Clone the demo project" + command: | + # Clone the demo project with custom build method + git clone --branch custom-build-method --single-branch https://github.com/EricRibeiro/Unity2D-Demo-Game-CI-CD.git ../Unity2D-Demo-Game-CI-CD + + # Clone the Unity orb + git clone https://github.com/game-ci/unity-orb.git . + + # Move the demo project to the project folder + mv ../Unity2D-Demo-Game-CI-CD ./Unity2D-Demo-Game-CI-CD workflows: test-build: @@ -281,6 +293,41 @@ workflows: filters: *filters context: orb-testing-unity pre-steps: *mono + - unity/build: + name: "build-with-custom-method-windows" + step-name: "Build with custom method in Windows" + unity-license-var-name: "UNITY_ENCODED_LICENSE_2021" + unity-username-var-name: "UNITY_USERNAME" + unity-password-var-name: "UNITY_PASSWORD" + executor: + name: "unity/windows-2019" + size: "large" + editor_version: "2021.3.2f1" + target_platform: "windows-il2cpp" + project-path: "Unity2D-Demo-Game-CI-CD/src" + build-target: StandaloneWindows64 + build-method: "MyCustomBuildCommand.MyCustomBuildMethod" + custom-parameters: "-customParam1 potato -customParam2 tomato -customParam3 $CIRCLE_WORKFLOW_ID -DetailedBuildReport" + filters: *filters + context: orb-testing-unity + pre-steps: *custom-build-method + - unity/build: + name: "build-with-custom-method-osx" + step-name: "Build with custom method in macOS" + unity-license-var-name: "UNITY_ENCODED_LICENSE_2021" + unity-username-var-name: "UNITY_USERNAME" + unity-password-var-name: "UNITY_PASSWORD" + executor: + name: "unity/macos" + editor_version: "2021.3.1f1" + resource_class: "large" + project-path: "Unity2D-Demo-Game-CI-CD/src" + build-target: StandaloneOSX + build-method: "MyCustomBuildCommand.MyCustomBuildMethod" + custom-parameters: "-customParam1 potato -customParam2 tomato -customParam3 $CIRCLE_WORKFLOW_ID -DetailedBuildReport" + filters: *filters + context: orb-testing-unity + pre-steps: *custom-build-method - orb-tools/pack: filters: *filters @@ -304,6 +351,8 @@ workflows: - build-ios - build-tvOS - build-without-artifacts + - build-with-custom-method-windows + - build-with-custom-method-osx context: orb-publishing filters: branches: diff --git a/src/commands/build.yml b/src/commands/build.yml index 92d7b20..041e98c 100644 --- a/src/commands/build.yml +++ b/src/commands/build.yml @@ -51,6 +51,21 @@ parameters: type: string default: "10m" description: Elapsed time the command can run without output. + build-method: + type: string + default: "" + description: | + The method used as argument for the Unity CLI "executeMethod" parameter. + It must be a valid path to a static method and the class exist in the Assets/Editor directory. + If left empty, the default build method will be used. + custom-parameters: + type: string + default: "" + description: | + Additional arguments for the Unity CLI. + Use it to pass arguments defined on your custom "build-method" or Unity's build options. Environment variables are supported. + The parameters must be separated by space and must be in the format "-key value" or "-key" for booleans. + Example: "-customArg1 WebGL -EnableHeadlessMode -customArg2 $CIRCLE_WORKFLOW_ID". steps: - run: @@ -61,6 +76,8 @@ steps: PARAM_BUILD_TARGET: << parameters.build-target >> PARAM_PROJECT_PATH: << parameters.project-path >> PARAM_COMPRESS: << parameters.compress >> + PARAM_BUILD_METHOD: << parameters.build-method >> + PARAM_CUSTOM_PARAMETERS: << parameters.custom-parameters >> SCRIPT_BUILD_WINDOWS: << include(scripts/windows/build.sh) >> SCRIPT_BUILD_LINUX: << include(scripts/linux/build.sh) >> SCRIPT_BUILD_MACOS: << include(scripts/macos/build.sh) >> diff --git a/src/jobs/build.yml b/src/jobs/build.yml index 7d5c516..9e3d49d 100755 --- a/src/jobs/build.yml +++ b/src/jobs/build.yml @@ -73,6 +73,21 @@ parameters: type: string default: "20m" description: Elapsed time the command can run without output. + build-method: + type: string + default: "" + description: | + The method used as argument for the Unity CLI "executeMethod" parameter. + It must be a valid path to a static method and the class exist in the Assets/Editor directory. + If left empty, the default build method will be used. + custom-parameters: + type: string + default: "" + description: | + Additional arguments for the Unity CLI. + Use it to pass arguments defined on your custom "build-method" or Unity's build options. Environment variables are supported. + The parameters must be separated by space and must be in the format "-key value" or "-key" for booleans. + Example: "-customArg1 WebGL -EnableHeadlessMode -customArg2 $CIRCLE_WORKFLOW_ID". executor: << parameters.executor >> @@ -93,6 +108,8 @@ steps: store-artifacts: <> compress: <> no_output_timeout: << parameters.no_output_timeout>> + build-method: <> + custom-parameters: <> - when: condition: <> steps: diff --git a/src/scripts/build.sh b/src/scripts/build.sh index d44b919..89e2327 100644 --- a/src/scripts/build.sh +++ b/src/scripts/build.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2034 readonly base_dir="${CIRCLE_WORKING_DIRECTORY/\~/$HOME}" readonly unity_project_full_path="$base_dir/$PARAM_PROJECT_PATH" @@ -9,9 +10,17 @@ eval "$SCRIPT_UTILS" # Detect host OS. detect-os -# Copy builder to project directory. -mkdir -p "$unity_project_full_path/Assets/Editor/" -printf '%s\n' "$DEPENDENCY_UNITY_BUILDER" > "$unity_project_full_path/Assets/Editor/BuildCommand.cs" +# Copy builder to project directory if a custom isn't specified. +build_method="$PARAM_BUILD_METHOD" +if [ -z "$PARAM_BUILD_METHOD" ]; then + printf '%s\n' "The \"build-method\" parameter is empty. Falling back to the default build script." + mkdir -p "$unity_project_full_path/Assets/Editor/" + printf '%s\n' "$DEPENDENCY_UNITY_BUILDER" > "$unity_project_full_path/Assets/Editor/BuildCommand.cs" + build_method="BuildCommand.PerformBuild" +fi + +# Expand custom parameters, if any. +custom_parameters="$(eval echo "$PARAM_CUSTOM_PARAMETERS")" # If "build_name" is blank, use the build target. if [ -z "$PARAM_BUILD_NAME" ]; then PARAM_BUILD_NAME="$PARAM_BUILD_TARGET"; fi diff --git a/src/scripts/macos/build.sh b/src/scripts/macos/build.sh index 828dd2b..3501d39 100644 --- a/src/scripts/macos/build.sh +++ b/src/scripts/macos/build.sh @@ -1,9 +1,8 @@ #!/bin/false # shellcheck shell=bash -# shellcheck disable=SC2154 +# shellcheck disable=SC2048,SC2154,SC2086 readonly build_path="$unity_project_full_path/Builds/$PARAM_BUILD_TARGET" - mkdir -p "$build_path" set -x @@ -16,9 +15,10 @@ set -x -buildTarget "$PARAM_BUILD_TARGET" \ -customBuildTarget "$PARAM_BUILD_TARGET" \ -customBuildPath "$build_path/$PARAM_BUILD_NAME" \ - -executeMethod "BuildCommand.PerformBuild" \ + -executeMethod "$build_method" \ -buildVersion "1.0.0" \ - -logfile /dev/stdout + -logfile /dev/stdout \ + $custom_parameters # Needs to be unquoted. Otherwise it will be treated as a single parameter. set +x if [ "$PARAM_COMPRESS" -eq 1 ]; then diff --git a/src/scripts/windows/build.sh b/src/scripts/windows/build.sh index 74e1bb0..fc2428d 100644 --- a/src/scripts/windows/build.sh +++ b/src/scripts/windows/build.sh @@ -22,11 +22,15 @@ docker exec "$CONTAINER_NAME" powershell mkdir C:/build # Add the build target and build name in the environment variables. docker exec "$CONTAINER_NAME" powershell "[System.Environment]::SetEnvironmentVariable('BUILD_NAME','$PARAM_BUILD_NAME', [System.EnvironmentVariableTarget]::Machine)" docker exec "$CONTAINER_NAME" powershell "[System.Environment]::SetEnvironmentVariable('BUILD_TARGET','$PARAM_BUILD_TARGET', [System.EnvironmentVariableTarget]::Machine)" +docker exec "$CONTAINER_NAME" powershell "[System.Environment]::SetEnvironmentVariable('BUILD_METHOD','$build_method', [System.EnvironmentVariableTarget]::Machine)" +docker exec "$CONTAINER_NAME" powershell "[System.Environment]::SetEnvironmentVariable('CUSTOM_PARAMS','$custom_parameters', [System.EnvironmentVariableTarget]::Machine)" # Build the project # Versioning of the project needs work. This is how it's done in the GHA: # https://github.com/game-ci/unity-builder/blob/main/src/model/versioning.ts -docker exec "$CONTAINER_NAME" powershell '& "C:\Program Files\Unity\Hub\Editor\*\Editor\Unity.exe" -batchmode -quit -nographics -projectPath $Env:PROJECT_PATH -buildTarget $Env:BUILD_TARGET -customBuildTarget $Env:BUILD_TARGET -customBuildName $Env:BUILD_NAME -customBuildPath "C:/build/" -executeMethod BuildCommand.PerformBuild -logfile | Out-Host' +set -x +docker exec "$CONTAINER_NAME" powershell '& "C:\Program Files\Unity\Hub\Editor\*\Editor\Unity.exe" -batchmode -quit -nographics -projectPath $Env:PROJECT_PATH -buildTarget $Env:BUILD_TARGET -customBuildTarget $Env:BUILD_TARGET -customBuildName $Env:BUILD_NAME -customBuildPath "C:/build/" -executeMethod $Env:BUILD_METHOD $Env:CUSTOM_PARAMS.split() -logfile | Out-Host' +set +x # Compress the build folder. docker exec "$CONTAINER_NAME" powershell 'tar -czf "C:/$Env:BUILD_NAME-$Env:BUILD_TARGET.tar.gz" -C "C:/build" .'