Skip to content

Commit f4c02bd

Browse files
authored
Add Command and Target Directory to the Actions
Instead of keeping the command to run and the destination to where upload the artifacts harcoded, we can use the action `inputs` and fallback to the built-in configuration. These changes introduce a Data Structure called `Configuration` holding the options coming from the external. The options are lazy in the sense that, the `Configuration` class has a dependency over the `core.getInput` so that we only call the function when we actually need the value. The `Configuration` properties are consumed via their getters.
1 parent e2c00ec commit f4c02bd

File tree

6 files changed

+71
-16
lines changed

6 files changed

+71
-16
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Assets Artifacts, is designed to compile the assets of your project and commit a
1414

1515
## Configuration
1616

17-
The configuration for this action is mostly hardcoded for now. The command used for compiling the assets is always `yarn build` and the target directory is always `./build`.
17+
- `command` Pass the command the action has to use to build the artifacts. Default to `yarn build`.
18+
- `target-dir` Pass the director where the action has to store the artifacts. Default to `build`.
1819

1920
## Workflow Example
2021

@@ -51,10 +52,13 @@ jobs:
5152
GIT_USER: ${{ secrets.GIT_USER }}
5253
GIT_EMAIL: ${{ secrets.GIT_EMAIL }}
5354
HUSKY: 0
55+
with:
56+
command: 'npm run build'
57+
target-dir: './dist'
5458
```
5559
5660
In this workflow, the action is triggered on every push event that includes a tag. The workflow runs on the latest version of Ubuntu and will not run if the commit message contains `--skip-assets-artifacts`.
5761

58-
The workflow includes steps to checkout the repository, setup Node.js with a specified version and cache configuration, install dependencies using `yarn install`, and finally, build the artifacts using the `widoz/github-artifacts-action@v1` action.
62+
The workflow includes steps to check out the repository, setup Node.js with a specified version and cache configuration, install dependencies using `yarn install`, and finally, build the artifacts using the `widoz/github-artifacts-action@v1` action.
5963

6064
The `GIT_USER` and `GIT_EMAIL` environment variables are used for the commit and should be stored as secrets in your GitHub repository. The `HUSKY` environment variable is set to `0` to disable Husky during the action run.

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ description: 'Upload assets to GitHub Artifacts'
44
runs:
55
using: 'node20'
66
main: './dist/index.js'
7+
inputs:
8+
command:
9+
description: 'Command to execute'
10+
target-dir:
11+
description: 'Target directory to upload'

dist/index.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31581,6 +31581,32 @@ const main_1 = __importDefault(__nccwpck_require__(399));
3158131581
(0, main_1.default)();
3158231582

3158331583

31584+
/***/ }),
31585+
31586+
/***/ 5778:
31587+
/***/ ((__unused_webpack_module, exports) => {
31588+
31589+
"use strict";
31590+
31591+
Object.defineProperty(exports, "__esModule", ({ value: true }));
31592+
exports.Configuration = void 0;
31593+
class Configuration {
31594+
read;
31595+
static COMMAND = 'yarn build';
31596+
static TARGET_DIR = './build';
31597+
constructor(read) {
31598+
this.read = read;
31599+
}
31600+
get command() {
31601+
return this.read('command') || Configuration.COMMAND;
31602+
}
31603+
get targetDir() {
31604+
return this.read('target-dir') || Configuration.TARGET_DIR;
31605+
}
31606+
}
31607+
exports.Configuration = Configuration;
31608+
31609+
3158431610
/***/ }),
3158531611

3158631612
/***/ 6704:
@@ -31669,10 +31695,12 @@ const artifacts_1 = __nccwpck_require__(1870);
3166931695
const tags_1 = __nccwpck_require__(7816);
3167031696
const create_git_1 = __nccwpck_require__(6704);
3167131697
const temporary_branch_1 = __nccwpck_require__(2786);
31698+
const configuration_1 = __nccwpck_require__(5778);
3167231699
async function main() {
31700+
const configuration = new configuration_1.Configuration(core.getInput.bind(core));
3167331701
const git = (0, create_git_1.createGit)();
3167431702
const tags = new tags_1.Tags();
31675-
const artifacts = new artifacts_1.Artifacts(git, tags);
31703+
const artifacts = new artifacts_1.Artifacts(git, tags, configuration);
3167631704
const temporaryBranch = new temporary_branch_1.TemporaryBranch(git);
3167731705
Promise.resolve()
3167831706
.then(() => temporaryBranch.create())
@@ -31722,11 +31750,11 @@ const exec = __importStar(__nccwpck_require__(1514));
3172231750
class Artifacts {
3172331751
git;
3172431752
tags;
31725-
static COMMAND = 'yarn build';
31726-
static TARGET_DIR = './build';
31727-
constructor(git, tags) {
31753+
configuration;
31754+
constructor(git, tags, configuration) {
3172831755
this.git = git;
3172931756
this.tags = tags;
31757+
this.configuration = configuration;
3173031758
}
3173131759
async update() {
3173231760
core.startGroup('📦 Creating artifacts');
@@ -31742,7 +31770,7 @@ class Artifacts {
3174231770
core.endGroup();
3174331771
}
3174431772
async compile() {
31745-
const result = await exec.exec(Artifacts.COMMAND);
31773+
const result = await exec.exec(this.configuration.command);
3174631774
if (result !== 0) {
3174731775
throw new Error('Failing to compile artifacts. Process exited with non-zero code.');
3174831776
}
@@ -31755,7 +31783,7 @@ class Artifacts {
3175531783
await this.tags.move();
3175631784
}
3175731785
async add() {
31758-
const result = await exec.exec(`git add -f ${Artifacts.TARGET_DIR}/*`);
31786+
const result = await exec.exec(`git add -f ${this.configuration.targetDir}/*`);
3175931787
if (result !== 0) {
3176031788
throw new Error('Failing to git-add the artifacts build. Process exited with non-zero code.');
3176131789
}

src/configuration.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { getInput } from '@actions/core';
2+
3+
export class Configuration {
4+
private static readonly COMMAND = 'yarn build';
5+
private static readonly TARGET_DIR = './build';
6+
7+
constructor(private readonly read: typeof getInput) {}
8+
9+
public get command(): string {
10+
return this.read('command') || Configuration.COMMAND;
11+
}
12+
13+
public get targetDir(): string {
14+
return this.read('target-dir') || Configuration.TARGET_DIR;
15+
}
16+
}

src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import { Artifacts } from './model/artifacts';
33
import { Tags } from './model/tags';
44
import { createGit } from './create-git';
55
import { TemporaryBranch } from './model/temporary-branch';
6+
import { Configuration } from './configuration';
67

78
async function main(): Promise<void> {
9+
const configuration = new Configuration(core.getInput.bind(core));
10+
811
const git = createGit();
912
const tags = new Tags();
10-
const artifacts = new Artifacts(git, tags);
13+
const artifacts = new Artifacts(git, tags, configuration);
1114
const temporaryBranch = new TemporaryBranch(git);
1215

1316
Promise.resolve()

src/model/artifacts.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import type { SimpleGit } from 'simple-git';
2-
import type { Tags } from './tags';
32
import * as core from '@actions/core';
43
import * as exec from '@actions/exec';
4+
import type { Tags } from './tags';
5+
import type { Configuration } from '../configuration';
56

67
export class Artifacts {
7-
private static readonly COMMAND = 'yarn build';
8-
private static readonly TARGET_DIR = './build';
9-
108
constructor(
119
private readonly git: Readonly<SimpleGit>,
12-
private readonly tags: Readonly<Tags>
10+
private readonly tags: Readonly<Tags>,
11+
private readonly configuration: Readonly<Configuration>
1312
) {}
1413

1514
public async update(): Promise<void> {
@@ -28,7 +27,7 @@ export class Artifacts {
2827
}
2928

3029
private async compile(): Promise<void> {
31-
const result = await exec.exec(Artifacts.COMMAND);
30+
const result = await exec.exec(this.configuration.command);
3231
if (result !== 0) {
3332
throw new Error('Failing to compile artifacts. Process exited with non-zero code.');
3433
}
@@ -44,7 +43,7 @@ export class Artifacts {
4443
}
4544

4645
private async add(): Promise<void> {
47-
const result = await exec.exec(`git add -f ${Artifacts.TARGET_DIR}/*`);
46+
const result = await exec.exec(`git add -f ${this.configuration.targetDir}/*`);
4847
if (result !== 0) {
4948
throw new Error('Failing to git-add the artifacts build. Process exited with non-zero code.');
5049
}

0 commit comments

Comments
 (0)