Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow custom arguments and executeMethod #36

Merged
merged 11 commits into from
Sep 22, 2022
49 changes: 49 additions & 0 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions src/commands/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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) >>
Expand Down
17 changes: 17 additions & 0 deletions src/jobs/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 >>

Expand All @@ -93,6 +108,8 @@ steps:
store-artifacts: <<parameters.store-artifacts>>
compress: <<parameters.compress>>
no_output_timeout: << parameters.no_output_timeout>>
build-method: <<parameters.build-method>>
custom-parameters: <<parameters.custom-parameters>>
- when:
condition: <<parameters.return-license>>
steps:
Expand Down
15 changes: 12 additions & 3 deletions src/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/scripts/macos/build.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/scripts/windows/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" .'
Expand Down