diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1b9c1a6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +# Order ignore, include +* + +# Files required for the action +!entrypoint.sh +!action.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..22fca4c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM gableroux/unity3d:2019.2.11f1 + +LABEL "com.github.actions.name"="Unity - Test runner" +LABEL "com.github.actions.description"="Run tests for any Unity project." +LABEL "com.github.actions.icon"="box" +LABEL "com.github.actions.color"="gray-dark" + +LABEL "repository"="http://github.com/webbertakken/unity-actions" +LABEL "homepage"="http://github.com/webbertakken/unity-actions" +LABEL "maintainer"="Webber Takken " + +ADD entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] diff --git a/README.md b/README.md index e9cda09..92f190b 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,56 @@ action from the collection. Requires the [activation](https://github.com/marketplace/actions/unity-activate) step. + +## Usage + +Create or edit the file called `.github/workflows/activation.yml` and add a job to it. + +```yaml +name: Test project +on: [push] +jobs: + testRunnerInAllModes: + name: Test all modes ✨ + runs-on: ubuntu-latest + steps: +``` + +Configure the test runner as follows: + +```yaml + # Configure test runner + - name: Run tests + id: myTestStep + uses: webbertakken/unity-test-runner@v1 + env: + UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }} + + # Choose: "all", "playmode", "editmode" + TEST_MODE: all + + # Optional: Folder of your project, leave blank for "./" + UNITY_PROJECT_PATH: relative/path/to/your/project + + # Optional: Artifacts folder, leave blank for "artifacts" + ARTIFACTS_FOLDER: store/artifacts/here +``` + +You use the id to **upload the artifacts** like so: + +```yaml + # Upload artifacts + - name: Upload test results + uses: actions/upload-artifact@v1 + with: + name: Test results + path: ${{ steps.myTestStep.outputs.artifactsPath }} +``` + +Commit and push your workflow definition. + +## More actions + +Visit +[Unity Actions](https://github.com/webbertakken/unity-actions) +to find related actions for Unity. diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..c933792 --- /dev/null +++ b/action.yml @@ -0,0 +1,13 @@ +name: 'Unity - Test runner' +author: Webber Takken +description: 'Run tests for any Unity project.' +inputs: {} +outputs: + artifactsPath: + description: 'Path where the artifacts are stored' +runs: + using: 'docker' + image: 'Dockerfile' +branding: + icon: 'box' + color: 'gray-dark' diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..3a922f5 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,191 @@ +#!/usr/bin/env bash + +# Set the license file path +LICENSE_FILE_PATH=UnityLicenseFile.ulf +UNITY_PROJECT_PATH=$GITHUB_WORKSPACE/$UNITY_PROJECT_PATH + +# Set the artifacts path +if [ -z "$ARTIFACTS_FOLDER" ]; then + ARTIFACTS_FOLDER=artifacts +fi +ARTIFACTS_PATH=$GITHUB_WORKSPACE/$ARTIFACTS_FOLDER + +# Set the modes for testing +case $TEST_MODE in + editmode) + echo "Edit mode selected for testing." + EDIT_MODE=true + ;; + playmode) + echo "Play mode selected for testing." + PLAY_MODE=true + ;; + *) + echo "All modes selected for testing." + EDIT_MODE=true + PLAY_MODE=true + ;; +esac + +# Copy license file from Github variables +echo "$UNITY_LICENSE" | tr -d '\r' > $LICENSE_FILE_PATH +echo "$UNITY_LICENSE" | tr -d '\r' > /root/.local/share/unity3d/Unity/Unity_lic.ulf +# TODO - test if this line has any effect +echo "$UNITY_LICENSE" | tr -d '\r' > /root/.local/share/unity3d/Unity/Unity_v2019.x.ulf + +## +## Activate license +## +echo "Requesting activation" +xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' \ + /opt/Unity/Editor/Unity \ + -batchmode \ + -nographics \ + -logFile /dev/stdout \ + -quit \ + -manualLicenseFile $LICENSE_FILE_PATH +# This is expected to always exit with code 1 (both success and failure). +# Convert to exit code 0 by echoing the current exit code. +echo $? +# Exit code is now 0 + + +# The following tests are 2019 mode (requires Unity 2019.2.11f1 or later) +# Reference: https://docs.unity3d.com/2019.3/Documentation/Manual/CommandLineArguments.html + +# +# Overall info +# + +echo "" +echo "###########################" +echo "# Artifacts folder #" +echo "###########################" +echo "" +echo "Creating \"$ARTIFACTS_PATH\" if it does not exist." +mkdir -p $ARTIFACTS_PATH + +echo "" +echo "###########################" +echo "# Project directory #" +echo "###########################" +echo "" +ls -alh $UNITY_PROJECT_PATH + +# +# Testing in EditMode +# + +if [ $EDIT_MODE = true ]; then + echo "" + echo "###########################" + echo "# Testing in EditMode #" + echo "###########################" + echo "" + xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' \ + /opt/Unity/Editor/Unity \ + -batchmode \ + -logfile /dev/stdout \ + -projectPath "$UNITY_PROJECT_PATH" \ + -runTests \ + -testPlatform editmode \ + -testResults "$ARTIFACTS_PATH/editmode-results.xml" + + # Catch exit code + EDIT_MODE_EXIT_CODE=$? + + # Display results + if [ $EDIT_MODE_EXIT_CODE -eq 0 ]; then + echo "Run succeeded, no failures occurred"; + elif [ $EDIT_MODE_EXIT_CODE -eq 2 ]; then + echo "Run succeeded, some tests failed"; + elif [ $EDIT_MODE_EXIT_CODE -eq 3 ]; then + echo "Run failure (other failure)"; + else + echo "Unexpected exit code $EDIT_MODE_EXIT_CODE"; + fi +fi + +# +# Testing in PlayMode +# + +if [ $PLAY_MODE = true ]; then + echo "" + echo "###########################" + echo "# Testing in PlayMode #" + echo "###########################" + echo "" + xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' \ + /opt/Unity/Editor/Unity \ + -batchmode \ + -logfile /dev/stdout \ + -projectPath "$UNITY_PROJECT_PATH" \ + -runTests \ + -testPlatform playmode \ + -testResults "$ARTIFACTS_PATH/playmode-results.xml" + + # Catch exit code + PLAY_MODE_EXIT_CODE=$? + + # Display results + if [ $PLAY_MODE_EXIT_CODE -eq 0 ]; then + echo "Run succeeded, no failures occurred"; + elif [ $PLAY_MODE_EXIT_CODE -eq 2 ]; then + echo "Run succeeded, some tests failed"; + elif [ $PLAY_MODE_EXIT_CODE -eq 3 ]; then + echo "Run failure (other failure)"; + else + echo "Unexpected exit code $PLAY_MODE_EXIT_CODE"; + fi +fi + +# +# Results +# + +echo "" +echo "###########################" +echo "# Project directory #" +echo "###########################" +echo "" +ls -alh $UNITY_PROJECT_PATH + +if [ $EDIT_MODE = true ]; then + echo "" + echo "###########################" + echo "# Edit Mode Results #" + echo "###########################" + echo "" + cat "$ARTIFACTS_PATH/editmode-results.xml" + cat "$ARTIFACTS_PATH/editmode-results.xml" | grep test-run | grep Passed +fi + +if [ $PLAY_MODE = true ]; then + echo "" + echo "###########################" + echo "# Edit Mode Results #" + echo "###########################" + echo "" + cat "$ARTIFACTS_PATH/playmode-results.xml" + cat "$ARTIFACTS_PATH/playmode-results.xml" | grep test-run | grep Passed +fi + +# +# Output variables +# + +# Set resulting name as output variable +echo ::set-output name=artifactsPath::$ARTIFACTS_PATH + +# +# Exit +# + +if [ $EDIT_MODE_EXIT_CODE -gt 0 ]; then + exit $EDIT_MODE_EXIT_CODE +fi + +if [ $PLAY_MODE_EXIT_CODE -gt 0 ]; then + exit $PLAY_MODE_EXIT_CODE +fi