Skip to content

Commit 4cb5f80

Browse files
committed
Add action logic
1 parent f46fdbf commit 4cb5f80

File tree

13 files changed

+261
-27
lines changed

13 files changed

+261
-27
lines changed

README.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
# Unity - Request Activation File
2+
23
[![Actions status](https://github.com/webbertakken/unity-request-manual-activation-file/workflows/Actions%20%F0%9F%98%8E/badge.svg)](https://github.com/webbertakken/unity-request-manual-activation-file/actions?query=branch%3Amaster+workflow%3A"Actions+😎")
34

45
---
56

67
GitHub Action for
78
[requesting the manual activation file](https://github.com/marketplace/actions/unity-request-activation-file) for Unity.
89

9-
Part of the
10-
[Unity Actions](https://github.com/webbertakken/unity-actions)
10+
Part of the
11+
[Unity Actions](https://github.com/webbertakken/unity-actions)
1112
collection.
1213

1314
---
1415

1516
Use this action to acquire a Unity personal license. Required in order to
16-
use the
17+
use the
1718
[Activate](https://github.com/webbertakken/unity-actions#activate),
1819
[Test](https://github.com/webbertakken/unity-actions#test) and
1920
[Build](https://github.com/webbertakken/unity-actions#build)
2021
actions.
2122

2223
### Documentation
2324

24-
See the
25+
See the
2526
[Unity Actions](https://github.com/webbertakken/unity-actions)
2627
collection repository for workflow documentation and reference implementation.
2728

2829
## Usage
2930

3031
Create a file called `.github/workflows/activation.yml` and add a job to it.
31-
32+
3233
```yaml
3334
name: Acquire activation file
3435
on: [push]
@@ -42,21 +43,23 @@ jobs:
4243
To **configure** this action, add this step and set the id.
4344
4445
```yaml
45-
# Request manual activation file
46-
- name: Request manual activation file
47-
uses: webbertakken/unity-request-manual-activation-file@v1
48-
id: getManualLicenseFile
46+
# Request manual activation file
47+
- name: Request manual activation file
48+
id: getManualLicenseFile
49+
uses: webbertakken/unity-request-manual-activation-file@v1
50+
with:
51+
unityVersion: 2019.2.11f1
4952
```
5053
51-
You use the id to **upload the output file** like so:
54+
You use the id to **upload the output file** like so:
5255
5356
```yaml
54-
# Upload artifact (Unity_v20XX.X.XXXX.alf)
55-
- name: Expose as artifact
56-
uses: actions/upload-artifact@v1
57-
with:
58-
name: ${{ steps.getManualLicenseFile.outputs.filePath }}
59-
path: ${{ steps.getManualLicenseFile.outputs.filePath }}
57+
# Upload artifact (Unity_v20XX.X.XXXX.alf)
58+
- name: Expose as artifact
59+
uses: actions/upload-artifact@v1
60+
with:
61+
name: ${{ steps.getManualLicenseFile.outputs.filePath }}
62+
path: ${{ steps.getManualLicenseFile.outputs.filePath }}
6063
```
6164
6265
Commit and push your workflow definition.
@@ -71,20 +74,20 @@ Follow these (one-time) steps for simple activation.
7174
- Open `Github` > `Your repository` > `Settings` > `Secrets`.
7275
- Add a new secret called `UNITY_LICENSE` and copy the contents your license file into it.
7376

74-
You can now use the
77+
You can now use the
7578
[Activate](https://github.com/webbertakken/unity-actions#activate),
7679
[Test](https://github.com/webbertakken/unity-actions#test) and
7780
[Build](https://github.com/webbertakken/unity-actions#build)
7881
actions.
7982

8083
## More actions
8184

82-
Visit
83-
[Unity Actions](https://github.com/webbertakken/unity-actions)
85+
Visit
86+
[Unity Actions](https://github.com/webbertakken/unity-actions)
8487
to find related actions for Unity.
8588

8689
Feel free to contribute.
8790

88-
## Licence
91+
## Licence
8992

9093
[MIT](./LICENSE)

action/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Action, Docker, Input, ImageTag } from './model';
2+
3+
const core = require('@actions/core');
4+
5+
async function action() {
6+
Action.checkCompatibility();
7+
8+
const { dockerfile, workspace, actionFolder } = Action;
9+
const { unityVersion } = Input.getFromUser();
10+
const baseImage = ImageTag.createForBase(unityVersion);
11+
12+
// Build docker image
13+
const actionImage = await Docker.build({ path: actionFolder, dockerfile, baseImage });
14+
15+
// Run docker image
16+
await Docker.run(actionImage, { workspace, unityVersion });
17+
}
18+
19+
action().catch(error => {
20+
core.setFailed(error.message);
21+
});

src/model/action.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import path from 'path';
2+
3+
class Action {
4+
static get supportedPlatforms() {
5+
return ['linux'];
6+
}
7+
8+
static get isRunningLocally() {
9+
return process.env.RUNNER_WORKSPACE === undefined;
10+
}
11+
12+
static get isRunningFromSource() {
13+
return path.basename(__dirname) === 'model';
14+
}
15+
16+
static get name() {
17+
return 'unity-request-manual-activation-file';
18+
}
19+
20+
static get rootFolder() {
21+
if (Action.isRunningFromSource) {
22+
return path.dirname(path.dirname(path.dirname(__filename)));
23+
}
24+
25+
return path.dirname(path.dirname(__filename));
26+
}
27+
28+
static get actionFolder() {
29+
return `${Action.rootFolder}/action`;
30+
}
31+
32+
static get dockerfile() {
33+
return `${Action.actionFolder}/Dockerfile`;
34+
}
35+
36+
static get workspace() {
37+
return process.env.GITHUB_WORKSPACE;
38+
}
39+
40+
static checkCompatibility() {
41+
const currentPlatform = process.platform;
42+
if (!Action.supportedPlatforms.includes(currentPlatform)) {
43+
throw new Error(`Currently ${currentPlatform}-platform is not supported`);
44+
}
45+
}
46+
}
47+
48+
export default Action;

src/model/action.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import Action from './action';
4+
5+
describe('Action', () => {
6+
describe('compatibility check', () => {
7+
it('throws for anything other than linux', () => {
8+
if (process.platform !== 'linux') {
9+
expect(() => Action.checkCompatibility()).toThrow();
10+
} else {
11+
expect(() => Action.checkCompatibility()).not.toThrow();
12+
}
13+
});
14+
});
15+
16+
it('returns the root folder of the action', () => {
17+
const { rootFolder, name } = Action;
18+
19+
expect(path.basename(rootFolder)).toStrictEqual(name);
20+
expect(fs.existsSync(rootFolder)).toStrictEqual(true);
21+
});
22+
23+
it('returns the action folder', () => {
24+
const { actionFolder } = Action;
25+
26+
expect(path.basename(actionFolder)).toStrictEqual('action');
27+
expect(fs.existsSync(actionFolder)).toStrictEqual(true);
28+
});
29+
30+
it('returns the docker file', () => {
31+
const { dockerfile } = Action;
32+
33+
expect(path.basename(dockerfile)).toStrictEqual('Dockerfile');
34+
expect(fs.existsSync(dockerfile)).toStrictEqual(true);
35+
});
36+
});

src/model/docker.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { exec } from '@actions/exec';
2+
import ImageTag from './image-tag';
3+
4+
class Docker {
5+
static async build(buildParameters, silent = false) {
6+
const { path, dockerfile, baseImage } = buildParameters;
7+
const { version } = baseImage;
8+
9+
const tag = ImageTag.createForAction(version);
10+
const command = `docker build ${path} \
11+
--file ${dockerfile} \
12+
--build-arg IMAGE=${baseImage} \
13+
--tag ${tag}`;
14+
15+
await exec(command, null, { silent });
16+
17+
return tag;
18+
}
19+
20+
static async run(image, parameters, silent = false) {
21+
const { unityVersion, workspace } = parameters;
22+
23+
const command = `docker run \
24+
--workdir /github/workspace \
25+
--rm \
26+
--env UNITY_VERSION=${unityVersion} \
27+
--env HOME=/github/home \
28+
--env GITHUB_REF \
29+
--env GITHUB_SHA \
30+
--env GITHUB_REPOSITORY \
31+
--env GITHUB_ACTOR \
32+
--env GITHUB_WORKFLOW \
33+
--env GITHUB_HEAD_REF \
34+
--env GITHUB_BASE_REF \
35+
--env GITHUB_EVENT_NAME \
36+
--env GITHUB_WORKSPACE=/github/workspace \
37+
--env GITHUB_ACTION \
38+
--env GITHUB_EVENT_PATH \
39+
--env RUNNER_OS \
40+
--env RUNNER_TOOL_CACHE \
41+
--env RUNNER_TEMP \
42+
--env RUNNER_WORKSPACE \
43+
--volume "/var/run/docker.sock":"/var/run/docker.sock" \
44+
--volume "/home/runner/work/_temp/_github_home":"/github/home" \
45+
--volume "/home/runner/work/_temp/_github_workflow":"/github/workflow" \
46+
--volume "${workspace}":"/github/workspace" \
47+
${image}`;
48+
49+
await exec(command, null, { silent });
50+
}
51+
}
52+
53+
export default Docker;

src/model/docker.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Action from './action';
2+
import Docker from './docker';
3+
import ImageTag from './image-tag';
4+
5+
describe('Docker', () => {
6+
it('builds', async () => {
7+
const path = Action.actionFolder;
8+
const dockerfile = `${path}/Dockerfile`;
9+
const baseImage = new ImageTag({
10+
repository: '',
11+
name: 'alpine',
12+
version: '3',
13+
});
14+
15+
const tag = await Docker.build({ path, dockerfile, baseImage }, true);
16+
17+
expect(tag).toBeInstanceOf(ImageTag);
18+
expect(tag.toString()).toStrictEqual('unity-action:3');
19+
}, 240000);
20+
});

src/model/image-tag.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import { trimStart } from 'lodash-es';
22

33
class ImageTag {
4-
constructor({ version }) {
4+
static createForBase(version) {
5+
const repository = 'gableroux';
6+
const name = 'unity3d';
7+
return new this({ repository, name, version });
8+
}
9+
10+
static createForAction(version) {
11+
const repository = '';
12+
const name = 'unity-action';
13+
return new this({ repository, name, version });
14+
}
15+
16+
constructor({ repository = '', name, version }) {
517
if (!ImageTag.versionPattern.test(version)) {
618
throw new Error(`Invalid version "${version}".`);
719
}
820

9-
this.repository = 'gableroux';
10-
this.name = 'unity3d';
11-
this.version = version;
21+
Object.assign(this, { repository, name, version });
1222
}
1323

1424
static get versionPattern() {

src/model/image-tag.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ImageTag from './image-tag';
33
describe('UnityImageVersion', () => {
44
describe('constructor', () => {
55
const some = {
6+
name: 'someName',
67
version: '2020.0.00f0',
78
};
89

@@ -13,8 +14,8 @@ describe('UnityImageVersion', () => {
1314
it('accepts parameters and sets the right properties', () => {
1415
const image = new ImageTag(some);
1516

16-
expect(image.repository).toStrictEqual('gableroux');
17-
expect(image.name).toStrictEqual('unity3d');
17+
expect(image.repository).toStrictEqual('');
18+
expect(image.name).toStrictEqual(some.name);
1819
expect(image.version).toStrictEqual(some.version);
1920
});
2021

@@ -29,7 +30,7 @@ describe('UnityImageVersion', () => {
2930

3031
describe('toString', () => {
3132
it('returns the correct version', () => {
32-
const image = new ImageTag({ version: '2099.1.1111' });
33+
const image = ImageTag.createForBase('2099.1.1111');
3334

3435
expect(image.toString()).toStrictEqual(`gableroux/unity3d:2099.1.1111`);
3536
});

src/model/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Action from './action';
2+
import Docker from './docker';
3+
import Input from './input';
4+
import ImageTag from './image-tag';
5+
6+
export { Action, Docker, Input, ImageTag };

0 commit comments

Comments
 (0)