Skip to content

Commit 8ca1282

Browse files
authoredNov 25, 2023
Allow Running Container as Runner Host User (#600)
- Added `runAsHostUser` to allow running the container as the same user as the host system. This fixes most permissions issues on self-hosted runners. - Perform android sdk setup during entrypoint.sh to ensure it has root permissions if the user switches to a non-root user - Automatically detect android sdk target version if parameters are not already provided to configure the sdk - Generate a new uuid for machineID to ensure separate containers are unique to reduce license activation errors - Add exponential retry strategy for Ubuntu license activations
1 parent 8da77ac commit 8ca1282

15 files changed

+174
-97
lines changed
 

‎action.yml

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ inputs:
101101
required: false
102102
default: ''
103103
description: '[CloudRunner] GitHub owner name or organization/team name'
104+
runAsHostUser:
105+
required: false
106+
default: 'false'
107+
description:
108+
'Whether to run as a user that matches the host system or the default root container user. Only applicable to
109+
Linux hosts and containers. This is useful for fixing permission errors on Self-Hosted runners.'
104110
chownFilesTo:
105111
required: false
106112
default: ''

‎dist/BlankProject/ProjectSettings/XRSettings.asset

-10
This file was deleted.

‎dist/index.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/platforms/mac/entrypoint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ echo ""
4141
echo "Please note that the exit code is not very descriptive."
4242
echo "Most likely it will not help you solve the issue."
4343
echo ""
44-
echo "To find the reason for failure: please search for errors in the log above."
44+
echo "To find the reason for failure: please search for errors in the log above and check for annotations in the summary view."
4545
echo ""
4646
fi;
4747

‎dist/platforms/mac/steps/activate.sh

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ echo "Requesting activation"
2020
# Store the exit code from the verify command
2121
UNITY_EXIT_CODE=$?
2222

23-
if [ ! -f "/Library/Application Support/Unity/Unity_lic.ulf" ]; then
24-
echo "::error ::There was an error while trying to activate the Unity license."
25-
fi
26-
2723
#
2824
# Display information about the result
2925
#

‎dist/platforms/ubuntu/entrypoint.sh

+64-35
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,76 @@
11
#!/usr/bin/env bash
22

3+
# Ensure machine ID is randomized
4+
dbus-uuidgen > /etc/machine-id && mkdir -p /var/lib/dbus/ && ln -sf /etc/machine-id /var/lib/dbus/machine-id
5+
36
#
4-
# Create directory for license activation
7+
# Prepare Android SDK, if needed
8+
# We do this here to ensure it has root permissions
59
#
610

7-
ACTIVATE_LICENSE_PATH="$GITHUB_WORKSPACE/_activate-license~"
8-
mkdir -p "$ACTIVATE_LICENSE_PATH"
11+
fullProjectPath="$GITHUB_WORKSPACE/$PROJECT_PATH"
912

10-
#
11-
# Run steps
12-
#
13-
source /steps/set_extra_git_configs.sh
14-
source /steps/set_gitcredential.sh
15-
source /steps/activate.sh
16-
source /steps/build.sh
17-
source /steps/return_license.sh
13+
if [[ "$BUILD_TARGET" == "Android" ]]; then
14+
export JAVA_HOME="$(awk -F'=' '/JAVA_HOME=/{print $2}' /usr/bin/unity-editor.d/*)"
15+
ANDROID_HOME_DIRECTORY="$(awk -F'=' '/ANDROID_HOME=/{print $2}' /usr/bin/unity-editor.d/*)"
16+
SDKMANAGER=$(find $ANDROID_HOME_DIRECTORY/cmdline-tools -name sdkmanager)
17+
if [ -z "${SDKMANAGER}" ]
18+
then
19+
echo "No sdkmanager found"
20+
exit 1
21+
fi
1822

19-
#
20-
# Remove license activation directory
21-
#
23+
if [[ -n "$ANDROID_SDK_MANAGER_PARAMETERS" ]]; then
24+
echo "Updating Android SDK with parameters: $ANDROID_SDK_MANAGER_PARAMETERS"
25+
$SDKMANAGER "$ANDROID_SDK_MANAGER_PARAMETERS"
26+
else
27+
echo "Updating Android SDK with auto detected target API version"
28+
# Read the line containing AndroidTargetSdkVersion from the file
29+
targetAPILine=$(grep 'AndroidTargetSdkVersion' "$fullProjectPath/ProjectSettings/ProjectSettings.asset")
2230

23-
rm -r "$ACTIVATE_LICENSE_PATH"
24-
chmod -R 777 "/BlankProject"
31+
# Extract the number after the semicolon
32+
targetAPI=$(echo "$targetAPILine" | cut -d':' -f2 | tr -d '[:space:]')
2533

26-
#
27-
# Instructions for debugging
28-
#
34+
$SDKMANAGER "platforms;android-$targetAPI"
35+
fi
2936

30-
if [[ $BUILD_EXIT_CODE -gt 0 ]]; then
31-
echo ""
32-
echo "###########################"
33-
echo "# Failure #"
34-
echo "###########################"
35-
echo ""
36-
echo "Please note that the exit code is not very descriptive."
37-
echo "Most likely it will not help you solve the issue."
38-
echo ""
39-
echo "To find the reason for failure: please search for errors in the log above."
40-
echo ""
41-
fi;
37+
echo "Updated Android SDK."
38+
else
39+
echo "Not updating Android SDK."
40+
fi
4241

43-
#
44-
# Exit with code from the build step.
45-
#
42+
if [[ "$RUN_AS_HOST_USER" == "true" ]]; then
43+
echo "Running as host user"
44+
45+
# Stop on error if we can't set up the user
46+
set -e
47+
48+
# Get host user/group info so we create files with the correct ownership
49+
USERNAME=$(stat -c '%U' "$fullProjectPath")
50+
USERID=$(stat -c '%u' "$fullProjectPath")
51+
GROUPNAME=$(stat -c '%G' "$fullProjectPath")
52+
GROUPID=$(stat -c '%g' "$fullProjectPath")
53+
54+
groupadd -g $GROUPID $GROUPNAME
55+
useradd -u $USERID -g $GROUPID $USERNAME
56+
usermod -aG $GROUPNAME $USERNAME
57+
mkdir -p "/home/$USERNAME"
58+
chown $USERNAME:$GROUPNAME "/home/$USERNAME"
59+
60+
# Normally need root permissions to access when using su
61+
chmod 777 /dev/stdout
62+
chmod 777 /dev/stderr
63+
64+
# Don't stop on error when running our scripts as error handling is baked in
65+
set +e
66+
67+
# Switch to the host user so we can create files with the correct ownership
68+
su $USERNAME -c "$SHELL -c 'source /steps/runsteps.sh'"
69+
else
70+
echo "Running as root"
71+
72+
# Run as root
73+
source /steps/runsteps.sh
74+
fi
4675

47-
exit $BUILD_EXIT_CODE
76+
exit $?

‎dist/platforms/ubuntu/steps/activate.sh

+44-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,54 @@
11
#!/usr/bin/env bash
22

3-
# Run in ACTIVATE_LICENSE_PATH directory
4-
echo "Changing to \"$ACTIVATE_LICENSE_PATH\" directory."
5-
pushd "$ACTIVATE_LICENSE_PATH"
6-
73
if [[ -n "$UNITY_SERIAL" && -n "$UNITY_EMAIL" && -n "$UNITY_PASSWORD" ]]; then
84
#
95
# SERIAL LICENSE MODE
106
#
11-
# This will activate unity, using the activating process.
7+
# This will activate unity, using the serial activation process.
128
#
139
echo "Requesting activation"
1410

15-
# Activate license
16-
unity-editor \
17-
-logFile /dev/stdout \
18-
-quit \
19-
-serial "$UNITY_SERIAL" \
20-
-username "$UNITY_EMAIL" \
21-
-password "$UNITY_PASSWORD" \
22-
-projectPath "/BlankProject"
11+
# Loop the unity-editor call until the license is activated with exponential backoff and a maximum of 5 retries
12+
retry_count=0
2313

24-
# Store the exit code from the verify command
25-
UNITY_EXIT_CODE=$?
14+
# Initialize delay to 15 seconds
15+
delay=15
16+
17+
# Loop until UNITY_EXIT_CODE is 0 or retry count reaches 5
18+
while [[ $retry_count -lt 5 ]]
19+
do
20+
# Activate license
21+
unity-editor \
22+
-logFile /dev/stdout \
23+
-quit \
24+
-serial "$UNITY_SERIAL" \
25+
-username "$UNITY_EMAIL" \
26+
-password "$UNITY_PASSWORD" \
27+
-projectPath "/BlankProject"
28+
29+
# Store the exit code from the verify command
30+
UNITY_EXIT_CODE=$?
31+
32+
# Check if UNITY_EXIT_CODE is 0
33+
if [[ $UNITY_EXIT_CODE -eq 0 ]]
34+
then
35+
echo "Activation successful"
36+
break
37+
else
38+
echo "Activation failed, retrying in $delay seconds..."
39+
sleep $delay
40+
41+
# Increment retry count
42+
((retry_count++))
43+
44+
# Double the delay for the next iteration
45+
delay=$((delay * 2))
46+
fi
47+
done
2648

27-
if [ ! -f "~/.local/share/unity3d/Unity/Unity_lic.ulf" ]; then
28-
echo "::error ::There was an error while trying to activate the Unity license."
49+
if [[ $retry_count -eq 5 ]]
50+
then
51+
echo "Activation failed after 5 retries"
2952
fi
3053

3154
elif [[ -n "$UNITY_LICENSING_SERVER" ]]; then
@@ -54,8 +77,9 @@ else
5477
echo "Visit https://game.ci/docs/github/getting-started for more"
5578
echo "details on how to set up one of the possible activation strategies."
5679

57-
echo "::error ::No valid license activation strategy could be determined."
58-
# Immediately exit as no UNITY_EXIT_CODE can be derrived.
80+
echo "::error ::No valid license activation strategy could be determined. Make sure to provide UNITY_EMAIL, UNITY_PASSWORD, and either a UNITY_SERIAL \
81+
or UNITY_LICENSE. Otherwise please use UNITY_LICENSING_SERVER."
82+
# Immediately exit as no UNITY_EXIT_CODE can be derived.
5983
exit 1;
6084

6185
fi
@@ -70,6 +94,7 @@ else
7094
# Activation failed so exit with the code from the license verification step
7195
echo "Unclassified error occured while trying to activate license."
7296
echo "Exit code was: $UNITY_EXIT_CODE"
97+
echo "::error ::There was an error while trying to activate the Unity license."
7398
exit $UNITY_EXIT_CODE
7499
fi
75100

‎dist/platforms/ubuntu/steps/build.sh

-21
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,6 @@ else
6262
#
6363
fi
6464

65-
#
66-
# Prepare Android SDK, if needed
67-
#
68-
69-
if [[ "$BUILD_TARGET" == "Android" && -n "$ANDROID_SDK_MANAGER_PARAMETERS" ]]; then
70-
echo "Updating Android SDK with parameters: $ANDROID_SDK_MANAGER_PARAMETERS"
71-
export JAVA_HOME="$(awk -F'=' '/JAVA_HOME=/{print $2}' /usr/bin/unity-editor.d/*)"
72-
ANDROID_HOME_DIRECTORY="$(awk -F'=' '/ANDROID_HOME=/{print $2}' /usr/bin/unity-editor.d/*)"
73-
SDKMANAGER=$(find $ANDROID_HOME_DIRECTORY/cmdline-tools -name sdkmanager)
74-
if [ -z "${SDKMANAGER}" ]
75-
then
76-
echo "No sdkmanager found"
77-
exit 1
78-
fi
79-
80-
$SDKMANAGER "$ANDROID_SDK_MANAGER_PARAMETERS"
81-
echo "Updated Android SDK."
82-
else
83-
echo "Not updating Android SDK."
84-
fi
85-
8665
#
8766
# Pre-build debug information
8867
#
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# Run steps
5+
#
6+
source /steps/set_extra_git_configs.sh
7+
source /steps/set_gitcredential.sh
8+
source /steps/activate.sh
9+
10+
# If we didn't activate successfully, exit with the exit code from the activation step.
11+
if [[ $UNITY_EXIT_CODE -ne 0 ]]; then
12+
exit $UNITY_EXIT_CODE
13+
fi
14+
15+
source /steps/build.sh
16+
source /steps/return_license.sh
17+
18+
#
19+
# Instructions for debugging
20+
#
21+
22+
if [[ $BUILD_EXIT_CODE -gt 0 ]]; then
23+
echo ""
24+
echo "###########################"
25+
echo "# Failure #"
26+
echo "###########################"
27+
echo ""
28+
echo "Please note that the exit code is not very descriptive."
29+
echo "Most likely it will not help you solve the issue."
30+
echo ""
31+
echo "To find the reason for failure: please search for errors in the log above and check for annotations in the summary view."
32+
echo ""
33+
fi;
34+
35+
#
36+
# Exit with code from the build step.
37+
#
38+
39+
# Exiting su
40+
exit $BUILD_EXIT_CODE

‎dist/platforms/windows/activate.ps1

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,4 @@ Write-Output ""
1313
-projectPath "c:/BlankProject" `
1414
-logfile - | Out-Host
1515

16-
if(-not(Test-path "C:/ProgramData/Unity/Unity_lic.ulf" -PathType leaf))
17-
{
18-
Write-Output "::error ::There was an error while trying to activate the Unity license."
19-
}
16+
$ACTIVATION_EXIT_CODE = $LASTEXITCODE

‎dist/platforms/windows/entrypoint.ps1

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Get-Process
2-
Start-Sleep -Seconds 3
32

43
# Import any necessary registry keys, ie: location of windows 10 sdk
54
# No guarantee that there will be any necessary registry keys, ie: tvOS
@@ -17,13 +16,17 @@ Get-Process -Name regsvr32 | ForEach-Object { Stop-Process -Id $_.Id -Force }
1716
# Activate Unity
1817
. "c:\steps\activate.ps1"
1918

19+
# If we didn't activate successfully, exit with the exit code from the activation step.
20+
if ($ACTIVATION_EXIT_CODE -ne 0) {
21+
exit $ACTIVATION_EXIT_CODE
22+
}
23+
2024
# Build the project
2125
. "c:\steps\build.ps1"
2226

2327
# Free the seat for the activated license
2428
. "c:\steps\return_license.ps1"
2529

26-
Start-Sleep -Seconds 3
2730
Get-Process
2831

2932
exit $BUILD_EXIT_CODE

‎src/model/build-parameters.ts

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class BuildParameters {
5959
public kubeVolumeSize!: string;
6060
public kubeVolume!: string;
6161
public kubeStorageClass!: string;
62+
public runAsHostUser!: String;
6263
public chownFilesTo!: string;
6364
public commandHooks!: string;
6465
public pullInputList!: string[];
@@ -168,6 +169,7 @@ class BuildParameters {
168169
sshAgent: Input.sshAgent,
169170
sshPublicKeysDirectoryPath: Input.sshPublicKeysDirectoryPath,
170171
gitPrivateToken: Input.gitPrivateToken || (await GithubCliReader.GetGitHubAuthToken()),
172+
runAsHostUser: Input.runAsHostUser,
171173
chownFilesTo: Input.chownFilesTo,
172174
dockerCpuLimit: Input.dockerCpuLimit,
173175
dockerMemoryLimit: Input.dockerMemoryLimit,

0 commit comments

Comments
 (0)