Kubernetes refactoring

pull/273/head
Frostebite 2021-06-06 21:32:24 +01:00
parent 045c4217d1
commit 8f79ef98f8
4 changed files with 148 additions and 142 deletions

View File

@ -46,10 +46,6 @@ jobs:
###########################
# Build #
###########################
# - uses: frostebite/File-To-Base64@master
# id: read-base64
# with:
# filePath: ~/.kube/config
- uses: ./
id: k8s-unity-build
with:

245
dist/index.js vendored
View File

@ -838,134 +838,137 @@ class Kubernetes {
core.info('Persistent Volume created, waiting for ready state...');
});
}
runJob(command, image) {
return __awaiter(this, void 0, void 0, function* () {
core.info('Creating build job');
const job = new k8s.V1Job();
job.apiVersion = 'batch/v1';
job.kind = 'Job';
job.metadata = {
name: this.jobName,
labels: {
app: 'unity-builder',
},
};
job.spec = {
template: {
spec: {
volumes: [
{
name: 'data',
persistentVolumeClaim: {
claimName: this.pvcName,
getJobSpec(command, image) {
const job = new k8s.V1Job();
job.apiVersion = 'batch/v1';
job.kind = 'Job';
job.metadata = {
name: this.jobName,
labels: {
app: 'unity-builder',
},
};
job.spec = {
template: {
spec: {
volumes: [
{
name: 'data',
persistentVolumeClaim: {
claimName: this.pvcName,
},
},
{
name: 'credentials',
secret: {
secretName: this.secretName,
},
},
],
containers: [
{
name: 'main',
image,
command,
resources: {
requests: {
memory: this.buildParameters.remoteBuildMemory,
cpu: this.buildParameters.remoteBuildCpu,
},
},
{
name: 'credentials',
secret: {
secretName: this.secretName,
env: [
{
name: 'GITHUB_SHA',
value: this.buildId,
},
},
],
containers: [
{
name: 'main',
image,
command,
resources: {
requests: {
memory: this.buildParameters.remoteBuildMemory,
cpu: this.buildParameters.remoteBuildCpu,
},
{
name: 'GITHUB_WORKSPACE',
value: '/data/repo',
},
env: [
{
name: 'GITHUB_SHA',
value: this.buildId,
},
{
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: 'CHOWN_FILES_TO',
value: this.buildParameters.chownFilesTo,
},
{
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;
{
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: 'CHOWN_FILES_TO',
value: this.buildParameters.chownFilesTo,
},
{
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',
},
},
],
restartPolicy: 'Never',
},
};
job.spec.backoffLimit = 1;
yield this.kubeClientBatch.createNamespacedJob(this.namespace, job);
core.info('Job created');
},
};
job.spec.backoffLimit = 1;
return job;
}
runJob(jobSpec) {
return __awaiter(this, void 0, void 0, function* () {
try {
core.info('Creating build job');
yield this.kubeClientBatch.createNamespacedJob(this.namespace, jobSpec);
core.info('Job created');
// We watch the PVC first to allow some time for K8s to notice the job we created and setup a pod.
yield this.watchPersistentVolumeClaimUntilReady();
// TODO: Wait for something more reliable so we don't potentially get the pod before k8s has created it based on the job.
@ -994,7 +997,7 @@ class Kubernetes {
}
runCloneJob() {
return __awaiter(this, void 0, void 0, function* () {
yield this.runJob([
yield this.runJob(this.getJobSpec([
'/bin/ash',
'-c',
`apk update;
@ -1008,12 +1011,12 @@ class Kubernetes {
git checkout $GITHUB_SHA;
ls
echo "end"`,
], 'alpine/git');
], 'alpine/git'));
});
}
runBuildJob() {
return __awaiter(this, void 0, void 0, function* () {
yield this.runJob([
yield this.runJob(this.getJobSpec([
'bin/bash',
'-c',
`ls
@ -1028,7 +1031,7 @@ class Kubernetes {
chmod -R +x /steps
/entrypoint.sh
`,
], this.baseImage.toString());
], this.baseImage.toString()));
});
}
watchPersistentVolumeClaimUntilReady() {

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -125,8 +125,7 @@ class Kubernetes implements RemoteBuilderProviderInterface {
core.info('Persistent Volume created, waiting for ready state...');
}
async runJob(command: string[], image: string) {
core.info('Creating build job');
getJobSpec(command: string[], image: string) {
const job = new k8s.V1Job();
job.apiVersion = 'batch/v1';
job.kind = 'Job';
@ -249,10 +248,14 @@ class Kubernetes implements RemoteBuilderProviderInterface {
},
};
job.spec.backoffLimit = 1;
await this.kubeClientBatch.createNamespacedJob(this.namespace, job);
core.info('Job created');
return job;
}
async runJob(jobSpec: k8s.V1Job) {
try {
core.info('Creating build job');
await this.kubeClientBatch.createNamespacedJob(this.namespace, jobSpec);
core.info('Job created');
// We watch the PVC first to allow some time for K8s to notice the job we created and setup a pod.
await this.watchPersistentVolumeClaimUntilReady();
@ -281,10 +284,11 @@ class Kubernetes implements RemoteBuilderProviderInterface {
async runCloneJob() {
await this.runJob(
[
'/bin/ash',
'-c',
`apk update;
this.getJobSpec(
[
'/bin/ash',
'-c',
`apk update;
apk add git-lfs;
ls /credentials/
export GITHUB_TOKEN=$(cat /credentials/GITHUB_TOKEN);
@ -295,17 +299,19 @@ class Kubernetes implements RemoteBuilderProviderInterface {
git checkout $GITHUB_SHA;
ls
echo "end"`,
],
'alpine/git',
],
'alpine/git',
),
);
}
async runBuildJob() {
await this.runJob(
[
'bin/bash',
'-c',
`ls
this.getJobSpec(
[
'bin/bash',
'-c',
`ls
for f in ./credentials/*; do export $(basename $f)="$(cat $f)"; done
ls /data
ls /data/builder
@ -317,8 +323,9 @@ class Kubernetes implements RemoteBuilderProviderInterface {
chmod -R +x /steps
/entrypoint.sh
`,
],
this.baseImage.toString(),
],
this.baseImage.toString(),
),
);
}