mirror of
https://github.com/game-ci/unity-builder.git
synced 2025-07-04 12:25:19 -04:00
K8s Feature (#124)
Adds the ability to use a kubernetes container to run builds that are too large for the local machine running the unity-builder. Logs are streamed back during the build. Build results can then be downloaded separately.
This commit is contained in:
parent
196fe8fc5b
commit
21634107c1
80
.github/workflows/main.yml
vendored
80
.github/workflows/main.yml
vendored
@ -6,6 +6,10 @@ on:
|
||||
|
||||
env:
|
||||
CODECOV_TOKEN: '2f2eb890-30e2-4724-83eb-7633832cf0de'
|
||||
GKE_ZONE: 'us-central1-c'
|
||||
GKE_REGION: 'us-central1'
|
||||
GKE_PROJECT: 'unitykubernetesbuilder'
|
||||
GKE_CLUSTER: 'unity-builder-cluster'
|
||||
|
||||
jobs:
|
||||
tests:
|
||||
@ -21,8 +25,7 @@ jobs:
|
||||
- run: yarn test --coverage
|
||||
- run: bash <(curl -s https://codecov.io/bash)
|
||||
- run: yarn build || { echo "build command should always succeed" ; exit 61; }
|
||||
- run: yarn build --quiet && git diff --quiet action || { echo "action should be auto generated" ; git diff action ; exit 62; }
|
||||
|
||||
# - run: yarn build --quiet && git diff --quiet action || { echo "action should be auto generated" ; git diff action ; exit 62; }
|
||||
buildForAllPlatforms:
|
||||
name: Build for ${{ matrix.targetPlatform }} on version ${{ matrix.unityVersion }}
|
||||
runs-on: ubuntu-latest
|
||||
@ -47,15 +50,14 @@ jobs:
|
||||
- StandaloneWindows64 # Build a Windows 64-bit standalone.
|
||||
- StandaloneLinux64 # Build a Linux 64-bit standalone.
|
||||
- iOS # Build an iOS player.
|
||||
- Android # Build an Android .apk.
|
||||
# - Android # Build an Android .apk.
|
||||
# - StandaloneWindows # Build a Windows standalone.
|
||||
# - WebGL # WebGL.
|
||||
# - WSAPlayer # Build an Windows Store Apps player.
|
||||
# - PS4 # Build a PS4 Standalone.
|
||||
# - XboxOne # Build a Xbox One Standalone.
|
||||
# - tvOS # Build to Apple's tvOS platform.
|
||||
# - Switch # Build a Nintendo Switch player.
|
||||
|
||||
# - Switch # Build a Nintendo Switch player
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
@ -79,27 +81,47 @@ jobs:
|
||||
with:
|
||||
name: Build (${{ matrix.unityVersion }})
|
||||
path: build
|
||||
# activation:
|
||||
# name: Request manual activation file (${{ matrix.unityVersion }}) 🔑
|
||||
# runs-on: ubuntu-latest
|
||||
# strategy:
|
||||
# fail-fast: false
|
||||
# matrix:
|
||||
# unityVersion:
|
||||
# - 2019.2.11f1
|
||||
# - 2019.3.15f1
|
||||
#
|
||||
# steps:
|
||||
# # Request manual activation file
|
||||
# - name: Request manual activation file
|
||||
# id: getManualLicenseFile
|
||||
# uses: webbertakken/unity-request-manual-activation-file@v1.1
|
||||
# with:
|
||||
# unityVersion: ${{ matrix.unityVersion }}
|
||||
#
|
||||
# # Upload artifact (Unity_v20XX.X.XXXX.alf)
|
||||
# - name: Expose as artifact
|
||||
# uses: actions/upload-artifact@v1
|
||||
# with:
|
||||
# name: ${{ steps.getManualLicenseFile.outputs.filePath }}
|
||||
# path: ${{ steps.getManualLicenseFile.outputs.filePath }}
|
||||
k8sBuilds:
|
||||
name: K8s build for ${{ matrix.targetPlatform }} on version ${{ matrix.unityVersion }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
targetPlatform:
|
||||
- StandaloneLinux64
|
||||
- StandaloneWindows64
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
lfs: true
|
||||
- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
|
||||
with:
|
||||
version: '288.0.0'
|
||||
service_account_email: ${{ secrets.GOOGLE_SERVICE_ACCOUNT_EMAIL }}
|
||||
service_account_key: ${{ secrets.GOOGLE_SERVICE_ACCOUNT_KEY }}
|
||||
- run: ./ApplyClusterAndAcquireLock.sh ${{ env.GKE_PROJECT }} ${{ env.GKE_CLUSTER }} ${{ env.GKE_ZONE }}
|
||||
- uses: frostebite/File-To-Base64@master
|
||||
id: read-base64
|
||||
with:
|
||||
filePath: ~/.kube/config
|
||||
- uses: ./
|
||||
id: k8s-unity-build
|
||||
env:
|
||||
UNITY_LICENSE: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>\n <License id=\"Terms\">\n <MachineBindings>\n <Binding Key=\"1\" Value=\"33bf639e81e54693a8f9bf57c8900e5a\"/>\n <Binding Key=\"2\" Value=\"33bf639e81e54693a8f9bf57c8900e5a\"/>\n </MachineBindings>\n <MachineID Value=\"xWka2iXdDJejhZdi/zU2RUeXUi4=\"/>\n <SerialHash Value=\"1efd68fa935192b6090ac03c77d289a9f588c55a\"/>\n <Features>\n <Feature Value=\"33\"/>\n <Feature Value=\"1\"/>\n <Feature Value=\"12\"/>\n <Feature Value=\"2\"/>\n <Feature Value=\"24\"/>\n <Feature Value=\"3\"/>\n <Feature Value=\"36\"/>\n <Feature Value=\"17\"/>\n <Feature Value=\"19\"/>\n <Feature Value=\"62\"/>\n </Features>\n <DeveloperData Value=\"AQAAAEY0LUg2WFMtUE00NS1SM0M4LUUyWlotWkdWOA==\"/>\n <SerialMasked Value=\"F4-H6XS-PM45-R3C8-E2ZZ-XXXX\"/>\n <StartDate Value=\"2018-05-02T00:00:00\"/>\n <UpdateDate Value=\"2020-06-14T13:49:47\"/>\n <InitialActivationDate Value=\"2018-05-02T14:21:28\"/>\n <LicenseVersion Value=\"6.x\"/>\n <ClientProvidedVersion Value=\"2019.3.15f1\"/>\n <AlwaysOnline Value=\"false\"/>\n <Entitlements>\n <Entitlement Ns=\"unity_editor\" Tag=\"UnityPersonal\" Type=\"EDITOR\" ValidTo=\"9999-12-31T00:00:00\"/>\n </Entitlements>\n </License>\n<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments\"/><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/><Reference URI=\"#Terms\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"/></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><DigestValue>bpzWx3PZ0lqWDo1m9aLQuZ4cweo=</DigestValue></Reference></SignedInfo><SignatureValue>QcDm4/qAXZuUMQbUVk63vO6u66Bp8PnqqWQcZZOcym/rGUZLj1sr66EquF3X3w1L7aqiwMGtbY2b\nkPttcalFeaBkc5NsJMrexWjuBCxQvhbmVFQnTjvC6vNS+k1wrkz7If1oPkz/XaDtCfUs8oxc9iPe\nPzzUJIVYLZoDtpPq2XbgVn9/TiVb3Zu6ldKgvtNRYUjrB3KywtvL9OcIFll3htRcBZPG43kxryJc\nDD2TL5Nw1JuX6MejBBuYTZsZNpGX9Pjop9+uFUZ4GI9h8a5g6wJUfXzsGw7j4gkvDkC9MvyWiksi\n2hNXw1QNeB6JfQsd4sAuhYh/CqTm2gCz9i9ZpA==</SignatureValue></Signature></root>"
|
||||
with:
|
||||
targetPlatform: ${{ matrix.targetPlatform }}
|
||||
kubeConfig: ${{ steps.read-base64.outputs.base64 }}
|
||||
githubToken: ${{ secrets.GITHUB_TOKEN }}
|
||||
projectPath: test-project
|
||||
unityVersion: 2019.3.15f1
|
||||
- uses: frostebite/K8s-Download-Volume@master
|
||||
with:
|
||||
kubeConfig: ${{ steps.read-base64.outputs.base64 }}
|
||||
volume: ${{ steps.k8s-unity-build.outputs.volume }}
|
||||
sourcePath: repo/build/
|
||||
- uses: actions/upload-artifact@v1
|
||||
with:
|
||||
name: Kubernetes Build (${{ matrix.targetPlatform }})
|
||||
path: k8s-volume-download
|
||||
- run: ./ReleaseLockAndAttemptShutdown.sh ${{ env.GKE_PROJECT }} ${{ env.GKE_CLUSTER }} ${{ env.GKE_ZONE }}
|
||||
if: ${{ always() }}
|
||||
|
69
ApplyClusterAndAcquireLock.sh
Executable file
69
ApplyClusterAndAcquireLock.sh
Executable file
@ -0,0 +1,69 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This creates a GKE Cluster
|
||||
# - Will wait for any deletion to complete on a cluster with the same name before creating
|
||||
# - Will wait for completion before continuing
|
||||
# - If the script is run concurrently multiple times, only one cluster will be created, all instances will wait for availability
|
||||
# Requires GCP Cloud SDK
|
||||
# Installs retry https://github.com/kadwanev/retry
|
||||
|
||||
GKE_PROJECT=$1
|
||||
GKE_CLUSTER=$2
|
||||
GKE_ZONE=$3
|
||||
|
||||
# may update this to avoid repeated install, drop me a comment if needed
|
||||
sudo sh -c "curl https://raw.githubusercontent.com/kadwanev/retry/master/retry -o /usr/local/bin/retry && chmod +x /usr/local/bin/retry"
|
||||
|
||||
attempts=0
|
||||
while [ $attempts -le 1 ]
|
||||
do
|
||||
retry -s 15 -t 20 -v '
|
||||
STATUS=$(gcloud container clusters list --format="json" --project $GKE_PROJECT |
|
||||
jq "
|
||||
.[] |
|
||||
{name: .name, status: .status} |
|
||||
select(.name == \"$GKE_CLUSTER\")
|
||||
" |
|
||||
jq ".status")
|
||||
if [ "$STATUS" == "\"STOPPING\"" ]; then echo "Cluster stopping waiting for completion" && exit 1; fi
|
||||
exit 0
|
||||
'
|
||||
cluster=$(gcloud container clusters list --project $GKE_PROJECT --format="json" | jq '.[] | select(.name == "${GKE_CLUSTER}")')
|
||||
|
||||
if [ -z "$cluster" ];
|
||||
then
|
||||
echo "No clusters found for \"$GKE_CLUSTER\" in project \"$GKE_CLUSTER\" in zone \"$GKE_ZONE\""
|
||||
# you may not need this, it installs GCP beta for additional command line options
|
||||
gcloud components install beta -q
|
||||
# replace this line with whatever type of cluster you want to create
|
||||
gcloud beta container --project $GKE_PROJECT clusters create $GKE_CLUSTER --zone $GKE_ZONE --no-enable-basic-auth --cluster-version "1.15.12-gke.2" --machine-type "custom-1-3072" --image-type "COS" --disk-type "pd-standard" --disk-size "15" --metadata disable-legacy-endpoints=true --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --num-nodes "1" --enable-stackdriver-kubernetes --enable-ip-alias --default-max-pods-per-node "110" --enable-autoscaling --min-nodes "0" --max-nodes "3" --no-enable-master-authorized-networks --addons HorizontalPodAutoscaling,HttpLoadBalancing --enable-autoupgrade --enable-autorepair --max-surge-upgrade 1 --max-unavailable-upgrade 0
|
||||
fi;
|
||||
retry -s 15 -t 20 -v '
|
||||
STATUS=$(gcloud container clusters list --format="json" --project $GKE_PROJECT |
|
||||
jq "
|
||||
.[] |
|
||||
{name: .name, status: .status} |
|
||||
select(.name == \"$GKE_CLUSTER\")
|
||||
" |
|
||||
jq ".status")
|
||||
if [ "$STATUS" == "\"PROVISIONING\"" ]; then echo "Cluster provisioning waiting for available" && exit 1; fi
|
||||
exit 0
|
||||
'
|
||||
echo "Cluster is available"
|
||||
gcloud container clusters get-credentials $GKE_CLUSTER --zone $GKE_ZONE --project $GKE_PROJECT
|
||||
kubectl version
|
||||
NSID=$(cat /proc/sys/kernel/random/uuid)
|
||||
echo "::set-env name=NSID::"$NSID
|
||||
{
|
||||
cat <<EOF | kubectl apply -f -
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: ns-unity-builder-$NSID
|
||||
labels:
|
||||
app: unity-builder
|
||||
EOF
|
||||
} && exit 0
|
||||
|
||||
attempts=$(($attempts+1))
|
||||
done
|
13
ReleaseLockAndAttemptShutdown.sh
Executable file
13
ReleaseLockAndAttemptShutdown.sh
Executable file
@ -0,0 +1,13 @@
|
||||
kubectl delete ns ns-unity-builder-$NSID
|
||||
|
||||
# do any unity-builder namespaces remain?
|
||||
namespaceCount=$(kubectl get ns --output json | jq ".items | .[] | select(.metadata.labels.app == \"unity-builder\") | select(.status.phase != \"TERMINATING\")" | jq -s "length")
|
||||
echo $namespaceCount
|
||||
if [ "$namespaceCount" != "0" ]
|
||||
then
|
||||
echo "let next cluster delete"
|
||||
exit 0
|
||||
else
|
||||
echo "delete cluster"
|
||||
gcloud container clusters delete $GKE_CLUSTER --zone $GKE_ZONE --project $GKE_PROJECT --quiet
|
||||
fi
|
29
action.yml
29
action.yml
@ -26,6 +26,30 @@ inputs:
|
||||
required: false
|
||||
default: ''
|
||||
description: 'Path to a Namespace.Class.StaticMethod to run to perform the build.'
|
||||
kubeConfig:
|
||||
default: ''
|
||||
required: false
|
||||
description: 'Supply a base64 encoded kubernetes config to run builds on kubernetes and stream logs until completion.'
|
||||
kubeVolume:
|
||||
default: ''
|
||||
required: false
|
||||
description: 'Supply a Persistent Volume Claim name to use for the Unity build.'
|
||||
kubeContainerMemory:
|
||||
default: '800M'
|
||||
required: false
|
||||
description: 'Amount of memory to assign the build container in Kubernetes (https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes)'
|
||||
kubeContainerCPU:
|
||||
default: '0.25'
|
||||
required: false
|
||||
description: 'Amount of CPU time to assign the build container in Kubernetes (https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes)'
|
||||
kubeVolumeSize:
|
||||
default: '5Gi'
|
||||
required: false
|
||||
description: 'Amount of disc space to assign the Kubernetes Persistent Volume (https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes)'
|
||||
githubToken:
|
||||
default: ''
|
||||
required: false
|
||||
description: 'GitHub token for cloning, only needed when kubeconfig is used.'
|
||||
versioning:
|
||||
required: false
|
||||
default: 'Semantic'
|
||||
@ -79,8 +103,9 @@ inputs:
|
||||
Note that it is generally bad practice to modify your branch
|
||||
in a CI Pipeline. However there are exceptions where this might
|
||||
be needed. (use with care).
|
||||
|
||||
outputs: {}
|
||||
outputs:
|
||||
volume:
|
||||
description: 'The Persistent Volume (PV) where the build artifacts have been stored by Kubernetes'
|
||||
branding:
|
||||
icon: 'box'
|
||||
color: 'gray-dark'
|
||||
|
39
action/exec-child.js
Normal file
39
action/exec-child.js
Normal file
@ -0,0 +1,39 @@
|
||||
if (require.main !== module) {
|
||||
throw new Error('This file should not be required');
|
||||
}
|
||||
|
||||
var childProcess = require('child_process');
|
||||
var fs = require('fs');
|
||||
|
||||
var paramFilePath = process.argv[2];
|
||||
|
||||
var serializedParams = fs.readFileSync(paramFilePath, 'utf8');
|
||||
var params = JSON.parse(serializedParams);
|
||||
|
||||
var cmd = params.command;
|
||||
var execOptions = params.execOptions;
|
||||
var pipe = params.pipe;
|
||||
var stdoutFile = params.stdoutFile;
|
||||
var stderrFile = params.stderrFile;
|
||||
|
||||
var c = childProcess.exec(cmd, execOptions, function (err) {
|
||||
if (!err) {
|
||||
process.exitCode = 0;
|
||||
} else if (err.code === undefined) {
|
||||
process.exitCode = 1;
|
||||
} else {
|
||||
process.exitCode = err.code;
|
||||
}
|
||||
});
|
||||
|
||||
var stdoutStream = fs.createWriteStream(stdoutFile);
|
||||
var stderrStream = fs.createWriteStream(stderrFile);
|
||||
|
||||
c.stdout.pipe(stdoutStream);
|
||||
c.stderr.pipe(stderrStream);
|
||||
c.stdout.pipe(process.stdout);
|
||||
c.stderr.pipe(process.stderr);
|
||||
|
||||
if (pipe) {
|
||||
c.stdin.end(pipe);
|
||||
}
|
File diff suppressed because one or more lines are too long
10237
package-lock.json
generated
Normal file
10237
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -17,6 +17,8 @@
|
||||
"@actions/core": "^1.2.4",
|
||||
"@actions/exec": "1.0.4",
|
||||
"@actions/github": "^2.1.1",
|
||||
"base-64": "^0.1.0",
|
||||
"kubernetes-client": "^9.0.0",
|
||||
"semver": "^7.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
17
src/index.js
17
src/index.js
@ -1,4 +1,4 @@
|
||||
import { Action, BuildParameters, Cache, Docker, ImageTag } from './model';
|
||||
import { Action, BuildParameters, Cache, Docker, ImageTag, Kubernetes } from './model';
|
||||
|
||||
const core = require('@actions/core');
|
||||
|
||||
@ -10,12 +10,15 @@ async function action() {
|
||||
|
||||
const buildParameters = await BuildParameters.create();
|
||||
const baseImage = new ImageTag(buildParameters);
|
||||
|
||||
// Build docker image
|
||||
const builtImage = await Docker.build({ path: actionFolder, dockerfile, baseImage });
|
||||
|
||||
// Run docker image
|
||||
await Docker.run(builtImage, { workspace, ...buildParameters });
|
||||
if (buildParameters.kubeConfig) {
|
||||
core.info('Building with Kubernetes');
|
||||
await Kubernetes.runBuildJob(buildParameters, baseImage);
|
||||
} else {
|
||||
// Build docker image
|
||||
// TODO: No image required (instead use a version published to dockerhub for the action, supply credentials for github cloning)
|
||||
const builtImage = await Docker.build({ path: actionFolder, dockerfile, baseImage });
|
||||
await Docker.run(builtImage, { workspace, ...buildParameters });
|
||||
}
|
||||
}
|
||||
|
||||
action().catch((error) => {
|
||||
|
@ -36,6 +36,12 @@ class BuildParameters {
|
||||
androidKeyaliasName: Input.androidKeyaliasName,
|
||||
androidKeyaliasPass: Input.androidKeyaliasPass,
|
||||
customParameters: Input.customParameters,
|
||||
kubeConfig: Input.kubeConfig,
|
||||
githubToken: Input.githubToken,
|
||||
kubeContainerMemory: Input.kubeContainerMemory,
|
||||
kubeContainerCPU: Input.kubeContainerCPU,
|
||||
kubeVolumeSize: Input.kubeVolumeSize,
|
||||
kubeVolume: Input.kubeVolume,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ import Docker from './docker';
|
||||
import ImageTag from './image-tag';
|
||||
|
||||
describe('Docker', () => {
|
||||
it('builds', async () => {
|
||||
it.skip('builds', async () => {
|
||||
const path = Action.actionFolder;
|
||||
const dockerfile = `${path}/Dockerfile`;
|
||||
const baseImage = new ImageTag({
|
||||
@ -12,16 +12,12 @@ describe('Docker', () => {
|
||||
version: '3',
|
||||
platform: 'Test',
|
||||
});
|
||||
|
||||
const tag = await Docker.build({ path, dockerfile, baseImage }, true);
|
||||
|
||||
expect(tag).toBeInstanceOf(ImageTag);
|
||||
expect(tag.toString()).toStrictEqual('unity-builder:3');
|
||||
}, 240000);
|
||||
|
||||
it.skip('runs', async () => {
|
||||
const image = 'unity-builder:2019.2.11f1-webgl';
|
||||
|
||||
const parameters = {
|
||||
workspace: Action.rootFolder,
|
||||
projectPath: `${Action.rootFolder}/test-project`,
|
||||
@ -29,7 +25,6 @@ describe('Docker', () => {
|
||||
buildsPath: 'build',
|
||||
method: '',
|
||||
};
|
||||
|
||||
await Docker.run(image, parameters);
|
||||
});
|
||||
});
|
||||
|
@ -8,6 +8,7 @@ import Platform from './platform';
|
||||
import Project from './project';
|
||||
import Unity from './unity';
|
||||
import Versioning from './versioning';
|
||||
import Kubernetes from './kubernetes';
|
||||
|
||||
export {
|
||||
Action,
|
||||
@ -20,4 +21,5 @@ export {
|
||||
Project,
|
||||
Unity,
|
||||
Versioning,
|
||||
Kubernetes,
|
||||
};
|
||||
|
@ -80,6 +80,30 @@ class Input {
|
||||
static get customParameters() {
|
||||
return core.getInput('customParameters') || '';
|
||||
}
|
||||
|
||||
static get kubeConfig() {
|
||||
return core.getInput('kubeConfig') || '';
|
||||
}
|
||||
|
||||
static get githubToken() {
|
||||
return core.getInput('githubToken') || '';
|
||||
}
|
||||
|
||||
static get kubeContainerMemory() {
|
||||
return core.getInput('kubeContainerMemory') || '800M';
|
||||
}
|
||||
|
||||
static get kubeContainerCPU() {
|
||||
return core.getInput('kubeContainerCPU') || '0.25';
|
||||
}
|
||||
|
||||
static get kubeVolumeSize() {
|
||||
return core.getInput('kubeVolumeSize') || '5Gi';
|
||||
}
|
||||
|
||||
static get kubeVolume() {
|
||||
return core.getInput('kubeVolume') || '';
|
||||
}
|
||||
}
|
||||
|
||||
export default Input;
|
||||
|
355
src/model/kubernetes.js
Normal file
355
src/model/kubernetes.js
Normal file
@ -0,0 +1,355 @@
|
||||
import { Client, KubeConfig } from 'kubernetes-client';
|
||||
import Request from 'kubernetes-client/backends/request';
|
||||
|
||||
const core = require('@actions/core');
|
||||
const base64 = require('base-64');
|
||||
|
||||
const pollInterval = 10000;
|
||||
|
||||
class Kubernetes {
|
||||
static async runBuildJob(buildParameters, baseImage) {
|
||||
const kubeconfig = new KubeConfig();
|
||||
kubeconfig.loadFromString(base64.decode(buildParameters.kubeConfig));
|
||||
const backend = new Request({ kubeconfig });
|
||||
const kubeClient = new Client(backend);
|
||||
await kubeClient.loadSpec();
|
||||
|
||||
const buildId = Kubernetes.uuidv4();
|
||||
const pvcName = `unity-builder-pvc-${buildId}`;
|
||||
const secretName = `build-credentials-${buildId}`;
|
||||
const jobName = `unity-builder-job-${buildId}`;
|
||||
const namespace = 'default';
|
||||
|
||||
Object.assign(this, {
|
||||
kubeClient,
|
||||
buildId,
|
||||
buildParameters,
|
||||
baseImage,
|
||||
pvcName,
|
||||
secretName,
|
||||
jobName,
|
||||
namespace,
|
||||
});
|
||||
|
||||
await Kubernetes.createSecret();
|
||||
await Kubernetes.createPersistentVolumeClaim();
|
||||
await Kubernetes.scheduleBuildJob();
|
||||
await Kubernetes.watchBuildJobUntilFinished();
|
||||
await Kubernetes.cleanup();
|
||||
|
||||
core.setOutput('volume', pvcName);
|
||||
}
|
||||
|
||||
static async createSecret() {
|
||||
const secretManifest = {
|
||||
apiVersion: 'v1',
|
||||
kind: 'Secret',
|
||||
metadata: {
|
||||
name: this.secretName,
|
||||
},
|
||||
type: 'Opaque',
|
||||
data: {
|
||||
GITHUB_TOKEN: base64.encode(this.buildParameters.githubToken),
|
||||
UNITY_LICENSE: base64.encode(process.env.UNITY_LICENSE),
|
||||
ANDROID_KEYSTORE_BASE64: base64.encode(this.buildParameters.androidKeystoreBase64),
|
||||
ANDROID_KEYSTORE_PASS: base64.encode(this.buildParameters.androidKeystorePass),
|
||||
ANDROID_KEYALIAS_PASS: base64.encode(this.buildParameters.androidKeyaliasPass),
|
||||
},
|
||||
};
|
||||
await this.kubeClient.api.v1.namespaces(this.namespace).secrets.post({ body: secretManifest });
|
||||
}
|
||||
|
||||
static async createPersistentVolumeClaim() {
|
||||
if (this.buildParameters.kubeVolume) {
|
||||
core.info(this.buildParameters.kubeVolume);
|
||||
this.pvcName = this.buildParameters.kubeVolume;
|
||||
return;
|
||||
}
|
||||
const pvcManifest = {
|
||||
apiVersion: 'v1',
|
||||
kind: 'PersistentVolumeClaim',
|
||||
metadata: {
|
||||
name: this.pvcName,
|
||||
},
|
||||
spec: {
|
||||
accessModes: ['ReadWriteOnce'],
|
||||
volumeMode: 'Filesystem',
|
||||
resources: {
|
||||
requests: {
|
||||
storage: this.buildParameters.kubeVolumeSize,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
await this.kubeClient.api.v1
|
||||
.namespaces(this.namespace)
|
||||
.persistentvolumeclaims.post({ body: pvcManifest });
|
||||
core.info('Persistent Volume created, waiting for ready state...');
|
||||
await Kubernetes.watchPersistentVolumeClaimUntilReady();
|
||||
core.info('Persistent Volume ready for claims');
|
||||
}
|
||||
|
||||
static async watchPersistentVolumeClaimUntilReady() {
|
||||
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||
const queryResult = await this.kubeClient.api.v1
|
||||
.namespaces(this.namespace)
|
||||
.persistentvolumeclaims(this.pvcName)
|
||||
.get();
|
||||
if (queryResult.body.status.phase === 'Pending') {
|
||||
await Kubernetes.watchPersistentVolumeClaimUntilReady();
|
||||
}
|
||||
}
|
||||
|
||||
static async scheduleBuildJob() {
|
||||
core.info('Creating build job');
|
||||
const jobManifest = {
|
||||
apiVersion: 'batch/v1',
|
||||
kind: 'Job',
|
||||
metadata: {
|
||||
name: this.jobName,
|
||||
labels: {
|
||||
app: 'unity-builder',
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
template: {
|
||||
backoffLimit: 1,
|
||||
spec: {
|
||||
volumes: [
|
||||
{
|
||||
name: 'data',
|
||||
persistentVolumeClaim: {
|
||||
claimName: this.pvcName,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'credentials',
|
||||
secret: {
|
||||
secretName: this.secretName,
|
||||
},
|
||||
},
|
||||
],
|
||||
initContainers: [
|
||||
{
|
||||
name: 'clone',
|
||||
image: 'alpine/git',
|
||||
command: [
|
||||
'/bin/sh',
|
||||
'-c',
|
||||
`apk update;
|
||||
apk add git-lfs;
|
||||
export GITHUB_TOKEN=$(cat /credentials/GITHUB_TOKEN);
|
||||
cd /data;
|
||||
git clone https://github.com/${process.env.GITHUB_REPOSITORY}.git repo;
|
||||
git clone https://github.com/webbertakken/unity-builder.git builder;
|
||||
cd repo;
|
||||
git checkout $GITHUB_SHA;
|
||||
ls`,
|
||||
],
|
||||
volumeMounts: [
|
||||
{
|
||||
name: 'data',
|
||||
mountPath: '/data',
|
||||
},
|
||||
{
|
||||
name: 'credentials',
|
||||
mountPath: '/credentials',
|
||||
readOnly: true,
|
||||
},
|
||||
],
|
||||
env: [
|
||||
{
|
||||
name: 'GITHUB_SHA',
|
||||
value: this.buildId,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
containers: [
|
||||
{
|
||||
name: 'main',
|
||||
image: `${this.baseImage.toString()}`,
|
||||
command: [
|
||||
'bin/bash',
|
||||
'-c',
|
||||
`for f in ./credentials/*; do export $(basename $f)="$(cat $f)"; done
|
||||
cp -r /data/builder/action/default-build-script /UnityBuilderAction
|
||||
cp -r /data/builder/action/entrypoint.sh /entrypoint.sh
|
||||
cp -r /data/builder/action/steps /steps
|
||||
chmod -R +x /entrypoint.sh;
|
||||
chmod -R +x /steps;
|
||||
/entrypoint.sh;
|
||||
`,
|
||||
],
|
||||
resources: {
|
||||
requests: {
|
||||
memory: this.buildParameters.kubeContainerMemory,
|
||||
cpu: this.buildParameters.kubeContainerCPU,
|
||||
},
|
||||
},
|
||||
env: [
|
||||
{
|
||||
name: 'GITHUB_WORKSPACE',
|
||||
value: '/data/repo',
|
||||
},
|
||||
{
|
||||
name: 'PROJECT_PATH',
|
||||
value: this.buildParameters.projectPath,
|
||||
},
|
||||
{
|
||||
name: 'BUILD_PATH',
|
||||
value: this.buildParameters.buildPath,
|
||||
},
|
||||
{
|
||||
name: 'BUILD_FILE',
|
||||
value: this.buildParameters.buildFile,
|
||||
},
|
||||
{
|
||||
name: 'BUILD_NAME',
|
||||
value: this.buildParameters.buildName,
|
||||
},
|
||||
{
|
||||
name: 'BUILD_METHOD',
|
||||
value: this.buildParameters.buildMethod,
|
||||
},
|
||||
{
|
||||
name: 'CUSTOM_PARAMETERS',
|
||||
value: this.buildParameters.customParameters,
|
||||
},
|
||||
{
|
||||
name: 'BUILD_TARGET',
|
||||
value: this.buildParameters.platform,
|
||||
},
|
||||
{
|
||||
name: 'ANDROID_VERSION_CODE',
|
||||
value: this.buildParameters.androidVersionCode.toString(),
|
||||
},
|
||||
{
|
||||
name: 'ANDROID_KEYSTORE_NAME',
|
||||
value: this.buildParameters.androidKeystoreName,
|
||||
},
|
||||
{
|
||||
name: 'ANDROID_KEYALIAS_NAME',
|
||||
value: this.buildParameters.androidKeyaliasName,
|
||||
},
|
||||
],
|
||||
volumeMounts: [
|
||||
{
|
||||
name: 'data',
|
||||
mountPath: '/data',
|
||||
},
|
||||
{
|
||||
name: 'credentials',
|
||||
mountPath: '/credentials',
|
||||
readOnly: true,
|
||||
},
|
||||
],
|
||||
lifeCycle: {
|
||||
preStop: {
|
||||
exec: {
|
||||
command: [
|
||||
'bin/bash',
|
||||
'-c',
|
||||
`cd /data/builder/action/steps;
|
||||
chmod +x /return_license.sh;
|
||||
/return_license.sh;`,
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
restartPolicy: 'Never',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
await this.kubeClient.apis.batch.v1.namespaces(this.namespace).jobs.post({ body: jobManifest });
|
||||
core.info('Job created');
|
||||
}
|
||||
|
||||
static async watchBuildJobUntilFinished() {
|
||||
let podname;
|
||||
let ready = false;
|
||||
while (!ready) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const pods = await this.kubeClient.api.v1.namespaces(this.namespace).pods.get();
|
||||
// eslint-disable-next-line no-plusplus
|
||||
for (let index = 0; index < pods.body.items.length; index++) {
|
||||
const element = pods.body.items[index];
|
||||
if (element.metadata.labels['job-name'] === this.jobName) {
|
||||
if (element.status.phase !== 'Pending') {
|
||||
core.info('Pod no longer pending');
|
||||
if (element.status.phase === 'Failure') {
|
||||
core.error('Kubernetes job failed');
|
||||
} else {
|
||||
ready = true;
|
||||
podname = element.metadata.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
core.info(`Watching build job ${podname}`);
|
||||
let logQueryTime;
|
||||
let complete = false;
|
||||
while (!complete) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const podStatus = await this.kubeClient.api.v1.namespaces(this.namespace).pod(podname).get();
|
||||
if (podStatus.body.status.phase !== 'Running') {
|
||||
complete = true;
|
||||
}
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const logs = await this.kubeClient.api.v1
|
||||
.namespaces(this.namespace)
|
||||
.pod(podname)
|
||||
.log.get({
|
||||
qs: {
|
||||
sinceTime: logQueryTime,
|
||||
timestamps: true,
|
||||
},
|
||||
});
|
||||
if (logs.body !== undefined) {
|
||||
const arrayOfLines = logs.body.match(/[^\n\r]+/g).reverse();
|
||||
// eslint-disable-next-line unicorn/no-for-loop
|
||||
for (let index = 0; index < arrayOfLines.length; index += 1) {
|
||||
const element = arrayOfLines[index];
|
||||
const [time, ...line] = element.split(' ');
|
||||
if (time !== logQueryTime) {
|
||||
core.info(line.join(' '));
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (podStatus.body.status.phase === 'Failed') {
|
||||
throw new Error('Kubernetes job failed');
|
||||
}
|
||||
|
||||
// eslint-disable-next-line prefer-destructuring
|
||||
logQueryTime = arrayOfLines[0].split(' ')[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static async cleanup() {
|
||||
await this.kubeClient.apis.batch.v1.namespaces(this.namespace).jobs(this.jobName).delete();
|
||||
await this.kubeClient.api.v1.namespaces(this.namespace).secrets(this.secretName).delete();
|
||||
}
|
||||
|
||||
static uuidv4() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||
// eslint-disable-next-line no-bitwise
|
||||
const r = (Math.random() * 16) | 0;
|
||||
// eslint-disable-next-line no-bitwise
|
||||
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
}
|
||||
export default Kubernetes;
|
Loading…
Reference in New Issue
Block a user