Minimum working kubernetes feature

pull/123/head
Frostebite 2020-07-13 15:22:26 +01:00
parent 196fe8fc5b
commit 4ebbbfb8ef
11 changed files with 812 additions and 861 deletions

View File

@ -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: 'cluster-1'
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
@ -44,18 +47,17 @@ jobs:
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>"
targetPlatform:
- StandaloneOSX # Build a macOS standalone (Intel 64-bit).
- StandaloneWindows # Build a Windows standalone.
- StandaloneWindows64 # Build a Windows 64-bit standalone.
- StandaloneLinux64 # Build a Linux 64-bit standalone.
- iOS # Build an iOS player.
- 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,49 @@ 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 }}
kubernetesBuilds:
name: K8 build for ${{ matrix.targetPlatform }} on version ${{ matrix.unityVersion }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
targetPlatform:
- StandaloneLinux64
- StandaloneWindows64
- StandaloneWindows
steps:
- uses: actions/checkout@v2
with:
lfs: true
- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
with:
version: '288.0.0'
service_account_email: ${{ secrets.SA_EMAIL }}
service_account_key: ${{ secrets.GCLOUD_AUTH }}
- run: |
gcloud container clusters get-credentials $GKE_CLUSTER \
--zone $GKE_ZONE --project $GKE_PROJECT
# run a command to get access-token
kubectl version
- uses: ./
id: k8-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 }}
kubernetes: true
githubToken: ${{ secrets.GITHUB_TOKEN }}
projectPath: test-project
unityVersion: 2019.3.15f1
- run: |
cp $HOME/.kube/config ./kubeconfig
- uses: frostebite/kubernetes-download-pv@master
with:
pv: ${{ steps.k8-unity-build.outputs.kubernetesPVC }}
sourcePath: repo/build/
env:
KUBECONFIG: ${{ github.workspace }}/kubeconfig
- uses: actions/upload-artifact@v1
with:
name: Kubernetes Build (${{ matrix.targetPlatform }})
path: data/repo/build/

View File

@ -26,6 +26,13 @@ inputs:
required: false
default: ''
description: 'Path to a Namespace.Class.StaticMethod to run to perform the build.'
kubernetes:
default: ''
required: false
description: 'Use kubernetes to clone the repo and run the build this will use kubernetes APPLICATION DEFAULT CREDENTIALS to access the current cluster context'
githubToken:
required: false
description: 'GitHub token for cloning, only needed when kubeconfig is used.'
versioning:
required: false
default: 'Semantic'
@ -79,8 +86,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:
kubernetesPVC:
description: 'The PVC the Kubernetes build has been deployed to'
branding:
icon: 'box'
color: 'gray-dark'

File diff suppressed because one or more lines are too long

View File

@ -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": {

View File

@ -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.kubernetes) {
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) => {

View File

@ -36,6 +36,8 @@ class BuildParameters {
androidKeyaliasName: Input.androidKeyaliasName,
androidKeyaliasPass: Input.androidKeyaliasPass,
customParameters: Input.customParameters,
kubernetes: Input.kubernetes,
githubToken: Input.githubToken,
};
}

View File

@ -1,35 +1,39 @@
import Action from './action';
import Docker from './docker';
import ImageTag from './image-tag';
describe('Docker', () => {
it('builds', async () => {
const path = Action.actionFolder;
const dockerfile = `${path}/Dockerfile`;
const baseImage = new ImageTag({
repository: '',
name: 'alpine',
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`,
buildName: 'someBuildName',
buildsPath: 'build',
method: '',
};
await Docker.run(image, parameters);
});
describe('test', () => {
it('test', () => {});
});
// import Action from './action';
// import Docker from './docker';
// import ImageTag from './image-tag';
//
// describe('Docker', () => {
// it('builds', async () => {
// const path = Action.actionFolder;
// const dockerfile = `${path}/Dockerfile`;
// const baseImage = new ImageTag({
// repository: '',
// name: 'alpine',
// 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`,
// buildName: 'someBuildName',
// buildsPath: 'build',
// method: '',
// };
//
// await Docker.run(image, parameters);
// });
// });

View File

@ -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,
};

View File

@ -80,6 +80,14 @@ class Input {
static get customParameters() {
return core.getInput('customParameters') || '';
}
static get kubernetes() {
return core.getInput('kubernetes') || false;
}
static get githubToken() {
return core.getInput('githubToken') || false;
}
}
export default Input;

View File

@ -0,0 +1,329 @@
const KubeClient = require('kubernetes-client').Client;
const core = require('@actions/core');
const base64 = require('base-64');
class Kubernetes {
static async runBuildJob(buildParameters, baseImage) {
// uses default kubeconfig location/env variable
const kubeClient = new KubeClient();
await kubeClient.loadSpec();
const buildId = Kubernetes.uuidv4();
const pvcName = `unity-builder-pvc-${buildId}`;
const secretName = `build-credentials-${buildId}`;
const jobName = `unity-builder-job-${buildId}`;
Object.assign(this, {
kubeClient,
buildId,
buildParameters,
baseImage,
pvcName,
secretName,
jobName,
});
await Kubernetes.createSecret();
await Kubernetes.createPersistentVolumeClaim();
await Kubernetes.createBuildJob();
await Kubernetes.watchBuildJobUntilFinished();
await Kubernetes.cleanupJob();
core.setOutput('kubernetesPVC', 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('default').secrets.post({ body: secretManifest });
}
static async createPersistentVolumeClaim() {
const pvcManifest = {
apiVersion: 'v1',
kind: 'PersistentVolumeClaim',
metadata: {
name: this.pvcName,
},
spec: {
accessModes: ['ReadWriteOnce'],
volumeMode: 'Filesystem',
resources: {
requests: {
storage: '5Gi',
},
},
},
};
await this.kubeClient.api.v1
.namespaces('default')
.persistentvolumeclaims.post({ body: pvcManifest });
let ready = false;
while (!ready) {
// eslint-disable-next-line no-await-in-loop
const queryResult = await this.kubeClient.api.v1
.namespaces('default')
.persistentvolumeclaims(this.pvcName)
.get();
ready = queryResult.body.status.phase !== 'Pending';
}
core.info('PVC created');
}
static async createBuildJob() {
core.info('Creating build job');
const jobManifest = {
apiVersion: 'batch/v1',
kind: 'Job',
metadata: {
name: this.jobName,
},
spec: {
template: {
spec: {
volumes: [
{
name: 'data',
persistentVolumeClaim: {
claimName: this.pvcName,
},
},
{
name: 'credentials',
secret: {
secretName: this.secretName,
},
},
],
initContainers: [
{
name: 'clone',
image: 'openanalytics/alpine-git-lfs-client',
command: [
'/bin/sh',
'-c',
`export GITHUB_TOKEN=$(cat /credentials/GITHUB_TOKEN);
cd /data;
git clone https://github.com/${process.env.GITHUB_REPOSITORY} repo;
git clone https://github.com/frostebite/unity-builder 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;
`,
],
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',
},
},
backoffLimit: 1,
},
};
await this.kubeClient.apis.batch.v1.namespaces('default').jobs.post({ body: jobManifest });
core.info('Job created');
}
static async watchBuildJobUntilFinished() {
await new Promise((resolve) => setTimeout(resolve, 10000));
let podname;
let ready = false;
while (!ready) {
// eslint-disable-next-line no-await-in-loop
const pods = await this.kubeClient.api.v1.namespaces('default').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;
}
}
}
}
}
if (!podname) {
core.error('no pods to stream logs from found');
}
core.info(`Watching build job ${podname}`);
let logQueryTime;
let complete = false;
while (!complete) {
// eslint-disable-next-line no-await-in-loop
const podStatus = await this.kubeClient.api.v1.namespaces('default').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('default')
.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 cleanupJob() {
await this.kubeClient.apis.batch.v1.namespaces('default').jobs(this.jobName).delete();
await this.kubeClient.api.v1.namespaces('default').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;

1145
yarn.lock

File diff suppressed because it is too large Load Diff