Skip to content

Commit

Permalink
perf: avoid building docker image (game-ci#365)
Browse files Browse the repository at this point in the history
* avoid building a custom image

* fix: remove unnecessary double-dash

* Rebuild with -- fix

* linting

* Remove unused variable

* support windows as well

* Fix -- command not found

* Fix unused import, remove docker build test

* no dockerfile anymore

Co-authored-by: Webber Takken <[email protected]>
  • Loading branch information
paulpach and webbertakken authored Mar 30, 2022
1 parent 9a40b89 commit b72639a
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 193 deletions.
108 changes: 54 additions & 54 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions dist/platforms/ubuntu/Dockerfile

This file was deleted.

19 changes: 0 additions & 19 deletions dist/platforms/windows/Dockerfile

This file was deleted.

7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ async function runMain() {
Action.checkCompatibility();
Cache.verify();

const { dockerfile, workspace, actionFolder } = Action;
const { workspace, actionFolder } = Action;

const buildParameters = await BuildParameters.create();
const baseImage = new ImageTag(buildParameters);

let builtImage;

if (
buildParameters.cloudRunnerCluster &&
buildParameters.cloudRunnerCluster !== '' &&
Expand All @@ -27,8 +25,7 @@ async function runMain() {
if (process.platform === 'darwin') {
MacBuilder.run(actionFolder, workspace, buildParameters);
} else {
builtImage = await Docker.build({ path: actionFolder, dockerfile, baseImage });
await Docker.run(builtImage, { workspace, ...buildParameters });
await Docker.run(baseImage, { workspace, actionFolder, ...buildParameters });
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/model/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,4 @@ describe('Action', () => {
expect(path.basename(actionFolder)).toStrictEqual('dist');
expect(fs.existsSync(actionFolder)).toStrictEqual(true);
});

it('returns the docker file', () => {
const { dockerfile } = Action;

expect(path.basename(dockerfile)).toStrictEqual('Dockerfile');
expect(fs.existsSync(dockerfile)).toStrictEqual(true);
});
});
14 changes: 0 additions & 14 deletions src/model/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ class Action {
return `${Action.rootFolder}/dist`;
}

static get dockerfile() {
const currentPlatform = process.platform;
switch (currentPlatform) {
case 'linux':
return `${Action.actionFolder}/platforms/ubuntu/Dockerfile`;
case 'win32':
return `${Action.actionFolder}/platforms/windows/Dockerfile`;
case 'darwin':
return 'unused'; //Mac doesn't use a container
default:
throw new Error(`No Dockerfile for currently unsupported platform: ${currentPlatform}`);
}
}

static get workspace() {
return process.env.GITHUB_WORKSPACE;
}
Expand Down
14 changes: 0 additions & 14 deletions src/model/docker.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
import Action from './action';
import Docker from './docker';
import ImageTag from './image-tag';

describe('Docker', () => {
it.skip('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 = {
Expand Down
111 changes: 52 additions & 59 deletions src/model/docker.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,67 @@
import { exec } from '@actions/exec';
import ImageTag from './image-tag';
import ImageEnvironmentFactory from './image-environment-factory';
import { existsSync, mkdirSync } from 'fs';
import path from 'path';

class Docker {
static async build(buildParameters, silent = false) {
const { path: buildPath, dockerfile, baseImage } = buildParameters;
const { version, platform } = baseImage;

const tag = new ImageTag({ repository: '', name: 'unity-builder', version, platform });
const command = `docker build ${buildPath} \
--file ${dockerfile} \
--build-arg IMAGE=${baseImage} \
--tag ${tag}`;

await exec(command, undefined, { silent });

return tag;
}

static async run(image, parameters, silent = false) {
const { workspace, unitySerial, runnerTempPath, sshAgent } = parameters;
let runCommand = '';
switch (process.platform) {
case 'linux':
runCommand = this.getLinuxCommand(image, parameters);
break;
case 'win32':
runCommand = this.getWindowsCommand(image, parameters);
}
await exec(runCommand, undefined, { silent });
}

const baseOsSpecificArguments = this.getBaseOsSpecificArguments(
process.platform,
workspace,
unitySerial,
runnerTempPath,
sshAgent,
);
static getLinuxCommand(image, parameters): string {
const { workspace, actionFolder, runnerTempPath, sshAgent } = parameters;

const runCommand = `docker run \
--workdir /github/workspace \
--rm \
${ImageEnvironmentFactory.getEnvVarString(parameters)} \
${baseOsSpecificArguments} \
${image}`;
const githubHome = path.join(runnerTempPath, '_github_home');
if (!existsSync(githubHome)) mkdirSync(githubHome);
const githubWorkflow = path.join(runnerTempPath, '_github_workflow');
if (!existsSync(githubWorkflow)) mkdirSync(githubWorkflow);

await exec(runCommand, undefined, { silent });
return `docker run \
--workdir /github/workspace \
--rm \
${ImageEnvironmentFactory.getEnvVarString(parameters)} \
--env UNITY_SERIAL \
--env GITHUB_WORKSPACE=/github/workspace \
${sshAgent ? '--env SSH_AUTH_SOCK=/ssh-agent' : ''} \
--volume "${githubHome}":"/root:z" \
--volume "${githubWorkflow}":"/github/workflow:z" \
--volume "${workspace}":"/github/workspace:z" \
--volume "${actionFolder}/default-build-script:/UnityBuilderAction:z" \
--volume "${actionFolder}/platforms/ubuntu/steps:/steps:z" \
--volume "${actionFolder}/platforms/ubuntu/entrypoint.sh:/entrypoint.sh:z" \
${sshAgent ? `--volume ${sshAgent}:/ssh-agent` : ''} \
${sshAgent ? '--volume /home/runner/.ssh/known_hosts:/root/.ssh/known_hosts:ro' : ''} \
${image} \
/bin/bash -c /entrypoint.sh`;
}

static getBaseOsSpecificArguments(baseOs, workspace, unitySerial, runnerTemporaryPath, sshAgent): string {
switch (baseOs) {
case 'linux': {
const githubHome = path.join(runnerTemporaryPath, '_github_home');
if (!existsSync(githubHome)) mkdirSync(githubHome);
const githubWorkflow = path.join(runnerTemporaryPath, '_github_workflow');
if (!existsSync(githubWorkflow)) mkdirSync(githubWorkflow);

return `--env UNITY_SERIAL \
--env GITHUB_WORKSPACE=/github/workspace \
${sshAgent ? '--env SSH_AUTH_SOCK=/ssh-agent' : ''} \
--volume "${githubHome}":"/root:z" \
--volume "${githubWorkflow}":"/github/workflow:z" \
--volume "${workspace}":"/github/workspace:z" \
${sshAgent ? `--volume ${sshAgent}:/ssh-agent` : ''} \
${sshAgent ? '--volume /home/runner/.ssh/known_hosts:/root/.ssh/known_hosts:ro' : ''}`;
}
case 'win32':
return `--env UNITY_SERIAL="${unitySerial}" \
--env GITHUB_WORKSPACE=c:/github/workspace \
--volume "${workspace}":"c:/github/workspace" \
--volume "c:/regkeys":"c:/regkeys" \
--volume "C:/Program Files (x86)/Microsoft Visual Studio":"C:/Program Files (x86)/Microsoft Visual Studio" \
--volume "C:/Program Files (x86)/Windows Kits":"C:/Program Files (x86)/Windows Kits" \
--volume "C:/ProgramData/Microsoft/VisualStudio":"C:/ProgramData/Microsoft/VisualStudio"`;
//Note: When upgrading to Server 2022, we will need to move to just "program files" since VS will be 64-bit
}
return '';
static getWindowsCommand(image: any, parameters: any): string {
const { workspace, actionFolder, unitySerial } = parameters;
return `docker run \
--workdir /github/workspace \
--rm \
${ImageEnvironmentFactory.getEnvVarString(parameters)} \
--env UNITY_SERIAL="${unitySerial}" \
--env GITHUB_WORKSPACE=c:/github/workspace \
--volume "${workspace}":"c:/github/workspace" \
--volume "c:/regkeys":"c:/regkeys" \
--volume "C:/Program Files (x86)/Microsoft Visual Studio":"C:/Program Files (x86)/Microsoft Visual Studio" \
--volume "C:/Program Files (x86)/Windows Kits":"C:/Program Files (x86)/Windows Kits" \
--volume "C:/ProgramData/Microsoft/VisualStudio":"C:/ProgramData/Microsoft/VisualStudio" \
--volume "${actionFolder}/default-build-script":"c:/UnityBuilderAction" \
--volume "${actionFolder}/platforms/ubuntu/steps":"c:/steps" \
--volume "${actionFolder}/platforms/windows/entrypoint.ps1":"c:/entrypoint.ps1" \
--volume "${actionFolder}/BlankProject":"c:/BlankProject" \
${image} \
powershell c:/entrypoint.ps1`;
}
}

Expand Down

0 comments on commit b72639a

Please sign in to comment.