perf: avoid building docker image (#365)

* 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 <webber.nl@gmail.com>
pull/367/head
Paul Pacheco 2022-03-30 18:16:30 -05:00 committed by GitHub
parent 9a40b8903e
commit b72639aac0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 110 additions and 194 deletions

76
dist/index.js generated vendored
View File

@ -51,7 +51,6 @@ function runMain() {
const { dockerfile, workspace, actionFolder } = model_1.Action;
const buildParameters = yield model_1.BuildParameters.create();
const baseImage = new model_1.ImageTag(buildParameters);
let builtImage;
if (buildParameters.cloudRunnerCluster &&
buildParameters.cloudRunnerCluster !== '' &&
buildParameters.cloudRunnerCluster !== 'local') {
@ -64,8 +63,7 @@ function runMain() {
mac_builder_1.default.run(actionFolder, workspace, buildParameters);
}
else {
builtImage = yield model_1.Docker.build({ path: actionFolder, dockerfile, baseImage });
yield model_1.Docker.run(builtImage, Object.assign({ workspace }, buildParameters));
yield model_1.Docker.run(baseImage, Object.assign({ workspace, actionFolder }, buildParameters));
}
}
// Set output
@ -3287,66 +3285,68 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const exec_1 = __nccwpck_require__(71514);
const image_tag_1 = __importDefault(__nccwpck_require__(57648));
const image_environment_factory_1 = __importDefault(__nccwpck_require__(25145));
const fs_1 = __nccwpck_require__(57147);
const path_1 = __importDefault(__nccwpck_require__(71017));
class Docker {
static build(buildParameters, silent = false) {
return __awaiter(this, void 0, void 0, function* () {
const { path: buildPath, dockerfile, baseImage } = buildParameters;
const { version, platform } = baseImage;
const tag = new image_tag_1.default({ repository: '', name: 'unity-builder', version, platform });
const command = `docker build ${buildPath} \
--file ${dockerfile} \
--build-arg IMAGE=${baseImage} \
--tag ${tag}`;
yield exec_1.exec(command, undefined, { silent });
return tag;
});
}
static run(image, parameters, silent = false) {
return __awaiter(this, void 0, void 0, function* () {
const { workspace, unitySerial, runnerTempPath, sshAgent } = parameters;
const baseOsSpecificArguments = this.getBaseOsSpecificArguments(process.platform, workspace, unitySerial, runnerTempPath, sshAgent);
const runCommand = `docker run \
--workdir /github/workspace \
--rm \
${image_environment_factory_1.default.getEnvVarString(parameters)} \
${baseOsSpecificArguments} \
${image}`;
let runCommand = '';
switch (process.platform) {
case 'linux':
runCommand = this.getLinuxCommand(image, parameters);
break;
case 'win32':
runCommand = this.getWindowsCommand(image, parameters);
}
yield exec_1.exec(runCommand, undefined, { silent });
});
}
static getBaseOsSpecificArguments(baseOs, workspace, unitySerial, runnerTemporaryPath, sshAgent) {
switch (baseOs) {
case 'linux': {
const githubHome = path_1.default.join(runnerTemporaryPath, '_github_home');
static getLinuxCommand(image, parameters) {
const { workspace, actionFolder, runnerTempPath, sshAgent } = parameters;
const githubHome = path_1.default.join(runnerTempPath, '_github_home');
if (!fs_1.existsSync(githubHome))
fs_1.mkdirSync(githubHome);
const githubWorkflow = path_1.default.join(runnerTemporaryPath, '_github_workflow');
const githubWorkflow = path_1.default.join(runnerTempPath, '_github_workflow');
if (!fs_1.existsSync(githubWorkflow))
fs_1.mkdirSync(githubWorkflow);
return `--env UNITY_SERIAL \
return `docker run \
--workdir /github/workspace \
--rm \
${image_environment_factory_1.default.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' : ''}`;
${sshAgent ? '--volume /home/runner/.ssh/known_hosts:/root/.ssh/known_hosts:ro' : ''} \
${image} \
/bin/bash -c /entrypoint.sh`;
}
case 'win32':
return `--env UNITY_SERIAL="${unitySerial}" \
static getWindowsCommand(image, parameters) {
const { workspace, actionFolder, unitySerial } = parameters;
return `docker run \
--workdir /github/workspace \
--rm \
${image_environment_factory_1.default.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"`;
//Note: When upgrading to Server 2022, we will need to move to just "program files" since VS will be 64-bit
}
return '';
--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`;
}
}
exports["default"] = Docker;

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -1,20 +0,0 @@
ARG IMAGE
FROM $IMAGE
LABEL "com.github.actions.name"="Unity - Builder"
LABEL "com.github.actions.description"="Build Unity projects for different platforms."
LABEL "com.github.actions.icon"="box"
LABEL "com.github.actions.color"="gray-dark"
LABEL "repository"="http://github.com/game-ci/unity-actions"
LABEL "homepage"="http://github.com/game-ci/unity-actions"
LABEL "maintainer"="Webber Takken <webber@takken.io>"
COPY default-build-script /UnityBuilderAction
COPY platforms/ubuntu/steps /steps
RUN chmod -R +x /steps
COPY platforms/ubuntu/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
RUN ls
ENTRYPOINT ["/entrypoint.sh"]

View File

@ -1,19 +0,0 @@
ARG IMAGE
FROM $IMAGE
LABEL "com.github.actions.name"="Unity - Builder"
LABEL "com.github.actions.description"="Build Unity projects for different platforms."
LABEL "com.github.actions.icon"="box"
LABEL "com.github.actions.color"="gray-dark"
LABEL "repository"="http://github.com/game-ci/unity-actions"
LABEL "homepage"="http://github.com/game-ci/unity-actions"
LABEL "maintainer"="Webber Takken <webber@takken.io>"
COPY default-build-script c:/UnityBuilderAction
COPY platforms/windows/steps c:/steps
COPY platforms/windows/entrypoint.ps1 c:/entrypoint.ps1
COPY BlankProject c:/BlankProject
RUN dir
ENTRYPOINT ["powershell", "c:/entrypoint.ps1"]

View File

@ -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 !== '' &&
@ -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 });
}
}

View File

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

View File

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

View File

@ -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 = {

View File

@ -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;
const baseOsSpecificArguments = this.getBaseOsSpecificArguments(
process.platform,
workspace,
unitySerial,
runnerTempPath,
sshAgent,
);
const runCommand = `docker run \
--workdir /github/workspace \
--rm \
${ImageEnvironmentFactory.getEnvVarString(parameters)} \
${baseOsSpecificArguments} \
${image}`;
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 });
}
static getBaseOsSpecificArguments(baseOs, workspace, unitySerial, runnerTemporaryPath, sshAgent): string {
switch (baseOs) {
case 'linux': {
const githubHome = path.join(runnerTemporaryPath, '_github_home');
static getLinuxCommand(image, parameters): string {
const { workspace, actionFolder, runnerTempPath, sshAgent } = parameters;
const githubHome = path.join(runnerTempPath, '_github_home');
if (!existsSync(githubHome)) mkdirSync(githubHome);
const githubWorkflow = path.join(runnerTemporaryPath, '_github_workflow');
const githubWorkflow = path.join(runnerTempPath, '_github_workflow');
if (!existsSync(githubWorkflow)) mkdirSync(githubWorkflow);
return `--env UNITY_SERIAL \
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' : ''}`;
${sshAgent ? '--volume /home/runner/.ssh/known_hosts:/root/.ssh/known_hosts:ro' : ''} \
${image} \
/bin/bash -c /entrypoint.sh`;
}
case 'win32':
return `--env UNITY_SERIAL="${unitySerial}" \
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"`;
//Note: When upgrading to Server 2022, we will need to move to just "program files" since VS will be 64-bit
}
return '';
--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`;
}
}