Skip to content

Unity Cloud Build

Unity Cloud Build #5

name: Unity Cloud Build
on:
# push:
# branches:
# - main
workflow_dispatch:
inputs:
profile:
description: 'Select level of profiling'
required: true
default: 'none'
type: choice
options:
- none
- profile
- deep
clean_build:
description: 'Clean Build'
required: false
default: false
type: boolean
cache_strategy:
description: 'Select cache strategy'
required: true
default: 'library'
type: choice
options:
- none
- library
- workspace
- inherit
jobs:
prebuild:
name: Prebuild
runs-on: ubuntu-latest
timeout-minutes: 10
# if: |
# (github.ref == 'refs/heads/dev') ||
# (github.ref == 'refs/heads/main') ||
# (github.event_name == 'pull_request' && !github.event.pull_request.draft) ||
# (github.event_name == 'pull_request' && github.event.action == 'labeled' && github.event.label.name == 'force-build') ||
# (github.event_name == 'pull_request' && github.event.action == 'labeled' && github.event.label.name == 'clean-build') ||
# (github.event_name == 'workflow_dispatch')
outputs:
commit_sha: ${{ steps.get_commit_sha.outputs.commit_sha }}
options: ${{ steps.get_options.outputs.options }}
version: ${{ github.event.inputs.version || inputs.version || steps.get_version.outputs.full_version }}
clean_build: ${{ steps.set_defaults.outputs.clean_build }}
cache_strategy: ${{ steps.set_defaults.outputs.cache_strategy }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get commit SHA
id: get_commit_sha
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "commit_sha=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT
else
echo "commit_sha=$GITHUB_SHA" >> $GITHUB_OUTPUT
fi
- name: Debug Commit SHA
run: |
echo "Ref from Pull Request: ${{ github.event.pull_request.head.sha }}"
echo "Full Commit from git rev-parse: $(git rev-parse $GITHUB_SHA)"
echo "HEAD Commit: $(git rev-parse HEAD)"
# - name: Get version
# id: get_version
# if: ${{ github.event.inputs.version == '' && inputs.version == '' }}
# uses: ./.github/actions/version
# with:
# commit_sha: ${{ steps.get_commit_sha.outputs.commit_sha }}
- name: Set default values
id: set_defaults
run: |
# Clean build logic
if [ "${{ github.event.inputs.clean_build }}" ]; then
clean_build=${{ github.event.inputs.clean_build }}
elif [ "${{ inputs.clean_build }}" ]; then
clean_build=${{ inputs.clean_build }}
else
clean_build=false
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "Checking PR labels: ${{ join(github.event.pull_request.labels.*.name, ', ') }}"
# Proper label check with spaces
clean_build=$(echo "${{ join(github.event.pull_request.labels.*.name, ' ') }}" | grep -qw 'clean-build' && echo true || echo false)
fi
fi
echo "Set clean_build to: $clean_build"
echo "clean_build=${clean_build}" >> $GITHUB_OUTPUT
if [ "${{ github.event.inputs.cache_strategy }}" ]; then
cache_strategy=${{ github.event.inputs.cache_strategy }}
elif [ "${{ inputs.cache_strategy }}" ]; then
cache_strategy=${{ inputs.cache_strategy }}
else
cache_strategy='library'
fi
echo "Set cache_strategy to: $cache_strategy"
echo "cache_strategy=${cache_strategy}" >> $GITHUB_OUTPUT
if [ "${{ github.event.inputs.install_source }}" ]; then
install_source=${{ github.event.inputs.install_source }}
elif [ "${{ inputs.install_source }}" ]; then
install_source=${{ inputs.install_source }}
else
install_source='launcher'
fi
echo "Set install_source to: $install_source"
echo "install_source=${install_source}" >> $GITHUB_OUTPUT
- name: Get BuildOptions
id: get_options
run: |
#!/bin/bash
#options=("DetailedBuildReport")
options=()
# input.profile
profile="${{ github.event.inputs.profile || inputs.profile }}"
if [[ "$profile" == "profile" || "$profile" == "deep" ]]; then
options+=("Development")
options+=("ConnectWithProfiler")
fi
if [[ "$profile" == "deep" ]]; then
options+=("EnableDeepProfilingSupport")
fi
# Write the array as a comma-separated string
# Set the Internal Field Separator to comma
IFS=,
echo "options=${options[*]}" >> "$GITHUB_OUTPUT"
build:
name: Build
runs-on: ubuntu-latest
needs: prebuild
timeout-minutes: 360
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref || github.ref_name }}
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: 3.12.3
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r .github/scripts/requirements.txt
- name: Execute Unity Cloud build
uses: nick-fields/retry@v3
with:
timeout_minutes: 180 # matches your GLOBAL_TIMEOUT = 10800s
max_attempts: 3
retry_on: any
retry_wait_seconds: 30
on_retry_command: |
echo "::warning::🔁 Unity Cloud Build retry triggered at $(date '+%Y-%m-%d %H:%M:%S')"
command: |
echo "🔧 Starting Unity Cloud Build attempt at $(date '+%Y-%m-%d %H:%M:%S')"
python -u .github/scripts/build.py
env:
API_KEY: ${{ secrets.UNITY_CLOUD_API_KEY }}
ORG_ID: ${{ secrets.UNITY_CLOUD_ORG_ID }}
PROJECT_ID: ${{ secrets.UNITY_CLOUD_PROJECT_ID }}
POLL_TIME: 60 # Set the polling time in seconds
GLOBAL_TIMEOUT: 10800 # Set the global timeout in seconds (e.g., 3 hours)
TARGET: t_web
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
COMMIT_SHA: ${{ needs.prebuild.outputs.commit_sha }}
BUILD_OPTIONS: ${{ needs.prebuild.outputs.options }}
CLEAN_BUILD: ${{ needs.prebuild.outputs.clean_build }}
CACHE_STRATEGY: ${{ needs.prebuild.outputs.cache_strategy }}
IS_RELEASE_BUILD: ${{ inputs.is_release_build }}
# TAG_VERSION: ${{ inputs.tag_version }} TODO
TAG_VERSION: 0.0.1-test
#Possible values: { none, library, workspace, inherit }
# Any ENV variables starting with "PARAM_" will be passed to Unity without the prefix
# (The "PARAM_" prefix exists to allow any future values config-free)
# e.g.: PARAM_ALLOW_DEBUG -> In Unity will be available as "ALLOW_DEBUG"
# e.g.: Editor.CloudBuild.Parameters["ALLOW_DEBUG"]
# PARAM_BUILD_VERSION: ${{ needs.prebuild.outputs.version }} TODO
PARAM_BUILD_VERSION: 0.0.1-test
PARAM_UNITY_EXTRA_PARAMS: '-disable-assembly-updater'
- name: Set artifact name
id: set_artifact_name
run: |
echo "artifact_name=aang-renderer" >> $GITHUB_ENV
- name: Upload artifact
id: upload-artifact
uses: actions/upload-artifact@v4
with:
name: ${{ env.artifact_name }}
path: |
build
!build/**/*_BackUpThisFolder_ButDontShipItWithYourGame
!build/**/*_BurstDebugInformation_DoNotShip
if-no-files-found: error
- name: Compress the build folder to upload it to S3
run: |
mkdir upload_to_s3/ && cd build && zip -r ../upload_to_s3/${{ env.artifact_name }}.zip . -x "*_BackUpThisFolder_ButDontShipItWithYourGame**" -x "*_BurstDebugInformation_DoNotShip**"
- name: Set SHA, branch, and build prefix
run: |
echo "SHA_SHORT=$(echo ${{ needs.prebuild.outputs.commit_sha }} | cut -c1-7)" >> $GITHUB_ENV
echo "SAFE_BRANCH_NAME=${GITHUB_HEAD_REF:-${GITHUB_REF_NAME}}" >> $GITHUB_ENV
EVENT_NAME="${{ github.event_name }}"
echo "Detected event: $EVENT_NAME"
case "$EVENT_NAME" in
pull_request) echo "BUILD_PREFIX=pr" >> $GITHUB_ENV ;;
push) echo "BUILD_PREFIX=pu" >> $GITHUB_ENV ;;
merge_group) echo "BUILD_PREFIX=mg" >> $GITHUB_ENV ;;
workflow_dispatch) echo "BUILD_PREFIX=wd" >> $GITHUB_ENV ;;
workflow_call) echo "BUILD_PREFIX=wc" >> $GITHUB_ENV ;;
schedule) echo "BUILD_PREFIX=sc" >> $GITHUB_ENV ;;
*) echo "BUILD_PREFIX=gn" >> $GITHUB_ENV ;;
esac
- name: Upload artifact to S3
env:
AWS_ACCESS_KEY_ID: ${{ secrets.EXPLORER_TEAM_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.EXPLORER_TEAM_AWS_SECRET_ACCESS_KEY }}
EXPLORER_TEAM_S3_BUCKET: ${{ secrets.EXPLORER_TEAM_S3_BUCKET }}
DESTINATION_PATH: "${{
inputs.is_release_build && format('@dcl/{0}/releases/{1}', github.event.repository.name, inputs.tag_version)
|| format('@dcl/{0}/branch/{1}/{2}-{3}-{4}', github.event.repository.name, env.SAFE_BRANCH_NAME, env.BUILD_PREFIX, github.run_number, env.SHA_SHORT)
}}"
run: |
npx @dcl/cdn-uploader@next \
--bucket $EXPLORER_TEAM_S3_BUCKET \
--local-folder upload_to_s3 \
--bucket-folder $DESTINATION_PATH
# Will run always (even if failing)
- name: Upload cloud logs
if: always()
uses: actions/upload-artifact@v4
with:
name: web_${{ needs.prebuild.outputs.install_source }}_unity_log
path: unity_cloud_log.log
if-no-files-found: error
# Will run always (even if failing)
- name: Print cloud logs
if: always()
run: cat unity_cloud_log.log
- name: Extract and display errors
if: always()
run: |
echo "=== Extracted Errors for ${{ matrix.target }} ${{ needs.prebuild.outputs.install_source }} ==="
grep -iE "error c|fatal" unity_cloud_log.log | sed 's/^/\x1b[31m/' | sed 's/$/\x1b[0m/' || echo "No 'error c' or 'fatal' errors found in ${{ matrix.target }} log."
- name: Generate Shader Compilation Report
shell: pwsh
run: |
./scripts/Generate-ShaderReport.ps1 -InputLog "unity_cloud_log.log" -OutputReport "shader_compilation_report.log"
- name: Upload Shader Compilation Report
uses: actions/upload-artifact@v4
with:
name: web_${{ needs.prebuild.outputs.install_source }}_shader_compilation_report
path: shader_compilation_report.log
if-no-files-found: warn
# Will run on cancel or timeout only
- name: Cancel Unity Cloud build
if: ${{ cancelled() }}
env:
API_KEY: ${{ secrets.UNITY_CLOUD_API_KEY }}
ORG_ID: ${{ secrets.UNITY_CLOUD_ORG_ID }}
PROJECT_ID: ${{ secrets.UNITY_CLOUD_PROJECT_ID }}
run: python -u scripts/cloudbuild/build.py --cancel