Skip to content

Commit

Permalink
fix: unity cloud build cache improvements (#1640)
Browse files Browse the repository at this point in the history
* don't delete the targets on healty builds

* Testing cache options

* Updateing default values

* Updated the defaults

* Testing workspace caching

* Testing workspace caching

* Testing cache and adding logs

* Use cache from existing target

* Fixed compilation error

* Updated Build Weekly Release to not use cache and make clean build

* Removed debug options

* Setting default values

* Fixes spacing issue

* Fixing defaults values

* Fixing compilation error
  • Loading branch information
aixaCode authored Aug 23, 2024
1 parent 86d56bb commit d502d7e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-release-weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
with:
profile: none
cache: false
cache_strategy: none
version: ${{ needs.get-info.outputs.full_version }}
sentry_enabled: true
secrets: inherit
44 changes: 42 additions & 2 deletions .github/workflows/build-unitycloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ on:
- deep
cache:
description: 'Enable to use workspace caching in cloud runners'
required: true
required: false
default: true
type: boolean
cache_strategy:
description: 'Select cache strategy'
required: true
default: 'workspace'
type: choice
options:
- none
- library
- workspace
- inherit
version:
description: 'Override for build version to use'
required: false
Expand All @@ -40,9 +50,15 @@ on:
profile:
required: true
type: string
default: 'none'
cache:
required: true
default: true
type: boolean
cache_strategy:
required: true
default: 'workspace'
type: string
version:
required: true
type: string
Expand Down Expand Up @@ -71,6 +87,8 @@ jobs:
sentry_environment: ${{ steps.get_sentry.outputs.environment }}
sentry_upload_symbols: ${{ steps.get_sentry.outputs.upload_symbols }}
sentry_enabled: ${{ steps.get_sentry.outputs.sentry_enabled }}
use_cache: ${{ steps.set_defaults.outputs.use_cache }}
cache_strategy: ${{ steps.set_defaults.outputs.cache_strategy }}
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down Expand Up @@ -101,6 +119,27 @@ jobs:
echo "sentry_enabled=false" >> "$GITHUB_OUTPUT"
fi
- name: Set default values
id: set_defaults
run: |
if [ "${{ github.event.inputs.cache }}" ]; then
use_cache=${{ github.event.inputs.cache }}
elif [ "${{ inputs.cache }}" ]; then
use_cache=${{ inputs.cache }}
else
use_cache='true'
fi
echo "use_cache=${use_cache}" >> $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='workspace'
fi
echo "cache_strategy=${cache_strategy}" >> $GITHUB_OUTPUT
- name: Get BuildOptions
id: get_options
run: |
Expand Down Expand Up @@ -160,9 +199,10 @@ jobs:
POLL_TIME: 60 # In seconds (int)
TARGET: t_${{ matrix.target }}
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
USE_CACHE: ${{ github.event.inputs.cache || inputs.cache }}
COMMIT_SHA: ${{ needs.prebuild.outputs.commit_sha }}
BUILD_OPTIONS: ${{ needs.prebuild.outputs.options }}
USE_CACHE: ${{ needs.prebuild.outputs.use_cache }}
CACHE_STRATEGY: ${{ needs.prebuild.outputs.cache_strategy }} #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"
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/pr-closure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Handle PR Closure

on:
pull_request:
types:
- closed

jobs:
handle-pr-closure:
name: Handle PR Closure
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Check if PR is merged
id: check_if_merged
run: echo "PR_MERGED=${{ github.event.pull_request.merged }}" >> $GITHUB_ENV

- name: Delete Unity Cloud target if merged
if: env.PR_MERGED == 'true'
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 --delete

- name: Delete Unity Cloud target if closed without merging
if: env.PR_MERGED != 'true'
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 --delete
37 changes: 21 additions & 16 deletions scripts/cloudbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
parser = argparse.ArgumentParser()
parser.add_argument('--resume', help='Resume tracking a running build stored in build_info.json', action='store_true')
parser.add_argument('--cancel', help='Cancel a running build stored in build_info.json', action='store_true')
parser.add_argument('--delete', help='Delete build target after PR is closed or merged', action='store_true')

def get_target(target):
response = requests.get(f'{URL}/buildtargets/{target}', headers=HEADERS)
Expand All @@ -40,19 +41,19 @@ def get_target(target):
# So we *always* create a new target, no matter what
# by appending the commit's SHA
def clone_current_target():
def generate_body(template_target, name, branch, options, cache):
def generate_body(template_target, name, branch, options, remoteCacheStrategy):
body = get_target(template_target)

body['name'] = name
body['settings']['scm']['branch'] = branch
body['settings']['advanced']['unity']['playerExporter']['buildOptions'] = options
body['settings']['remoteCacheStrategy'] = remoteCacheStrategy

# Copy cache check
if cache:
body['settings']['buildTargetCopyCache'] = template_target
else:
if 'buildTargetCopyCache' in body['settings']:
del body['settings']['buildTargetCopyCache']
print(f"Using cache strategy target: {remoteCacheStrategy}")

# Remove cache for new targets
if 'buildTargetCopyCache' in body['settings']:
del body['settings']['buildTargetCopyCache']

return body

Expand All @@ -66,15 +67,19 @@ def generate_body(template_target, name, branch, options, cache):
new_target_name,
os.getenv('BRANCH_NAME'),
os.getenv('BUILD_OPTIONS').split(','),
False)
os.getenv('CACHE_STRATEGY'))

existing_target = get_target(new_target_name)

if 'error' in existing_target:
# Create new target
# Create new target without cache
response = requests.post(f'{URL}/buildtargets', headers=HEADERS, json=body)
print("No cache build target used for new target")
else:
# Target exists, update it
# Target exists, update it and use cache based on new_target_name
if os.getenv('USE_CACHE'):
body['settings']['buildTargetCopyCache'] = new_target_name
print(f"Using cache build target: {new_target_name}")
response = requests.put(f'{URL}/buildtargets/{new_target_name}', headers=HEADERS, json=body)

if response.status_code == 200 or response.status_code == 201:
Expand Down Expand Up @@ -321,8 +326,11 @@ def delete_current_target():
# Entrypoint here ->
args = parser.parse_args()

# MODE: Delete
if args.delete:
delete_current_target()
# MODE: Resume
if args.resume or args.cancel:
elif args.resume or args.cancel:
build_info = utils.read_build_info()
if build_info is None:
sys.exit(1)
Expand Down Expand Up @@ -377,10 +385,7 @@ def delete_current_target():
sys.exit(1)

# Cleanup (only if build is healthy)
if get_any_running_builds(os.getenv('TARGET')):
delete_build(id)
else:
# Deleting the parent target also removes all builds
delete_current_target()
# We only delete all artifacts, not the build target
delete_build(id)

utils.delete_build_info()

0 comments on commit d502d7e

Please sign in to comment.