Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![codecov](https://codecov.io/gh/widoz/github-artifacts-action/graph/badge.svg?token=TF80AM1DUZ)](https://codecov.io/gh/widoz/github-artifacts-action)
![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/widoz/github-artifacts-action?utm_source=oss&utm_medium=github&utm_campaign=widoz%2Fgithub-artifacts-action&labelColor=171717&color=FF570A&link=https%3A%2F%2Fcoderabbit.ai&label=CodeRabbit+Reviews)

Assets Artifacts, is designed to compile the assets of your project and commit and push these assets back into the repository. It also moves the tags to point to the commit where the assets are pushed.
Assets Artifacts is designed to compile the assets of your project and commit and push these assets back into the repository. It also moves the tags to point to the commit where the assets are pushed.

## What it does

Expand All @@ -20,6 +20,7 @@ Assets Artifacts, is designed to compile the assets of your project and commit a

- `command` Pass the command the action has to use to build the artifacts. Default to `yarn build`.
- `target-dir` Pass the director where the action has to store the artifacts. Default to `build`.
- `can-push` Pass the boolean indicating if the assets can be pushed or not. Default to `true`.

## Workflow Example

Expand Down Expand Up @@ -66,6 +67,7 @@ jobs:
with:
command: 'npm run build'
target-dir: './dist'
can-pus: 'false'
```

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`.
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ runs:
inputs:
command:
description: 'Command to execute'
required: false
target-dir:
description: 'Target directory to upload'
required: false
can-push:
description: 'Whenever the assets shall be pushed'
required: false
default: 'true'
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export class Configuration {
public get isTag(): boolean {
return (this.env['GITHUB_REF'] ?? '').startsWith('refs/tags/');
}

public get canPush(): boolean {
return this.read('can-push') === 'true';
}
}
5 changes: 5 additions & 0 deletions src/model/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export class Artifacts {
}

private async push(): Promise<void> {
if (!this.configuration.canPush) {
core.info('Skipping pushing artifacts.');
return;
}

const pushingResult = await this.git.push();
const messages = pushingResult.remoteMessages.all.join('\n');

Expand Down
54 changes: 50 additions & 4 deletions tests/unit/model/artifacts.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SimpleGit } from 'simple-git';
import type { Tags } from '@model/tags';
import { info } from '@actions/core';
import type { getInput } from '@actions/core';

import { it, jest, describe, expect } from '@jest/globals';
Expand All @@ -21,6 +22,13 @@ jest.mock('@model/tags', () => ({
},
}));

jest.mock('@actions/core', () => ({
getInput: jest.fn(),
info: jest.fn(),
startGroup: jest.fn(),
endGroup: jest.fn()
}))

describe('Artifacts', () => {
it('Compile the assets and Deploy when finished', async () => {
const git = fromPartial<SimpleGit>({
Expand Down Expand Up @@ -85,6 +93,31 @@ describe('Artifacts', () => {
await expect(artifacts.update()).rejects.toThrow('Failed creating artifacts: Failed to push');
});

it('Do not push when the action is not configured to do so', async () => {
const push = jest.fn();
const git = fromPartial<SimpleGit>({
commit: jest.fn(() =>
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
),
push,
});
const tags = fromPartial<Tags>({ collect: jest.fn(), move: jest.fn() });
const artifacts = new Artifacts(
git,
tags,
configuration(undefined, {
'can-push': 'false',
})
);

jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));

await artifacts.update();

expect(push).not.toHaveBeenCalled();
expect(info).toHaveBeenCalledWith('Skipping pushing artifacts.');
});

it('Throw an error when failing to git-add', async () => {
const git = fromPartial<SimpleGit>({});
const tags = fromPartial<Tags>({ collect: jest.fn() });
Expand Down Expand Up @@ -168,7 +201,12 @@ describe('Artifacts', () => {
});
});

function configuration(env?: Readonly<NodeJS.ProcessEnv>): Configuration {
type InputsConfiguration = Readonly<Record<string, unknown>>;

function configuration(
env?: Readonly<NodeJS.ProcessEnv>,
inputsConfiguration: InputsConfiguration = {}
): Configuration {
let _env = env;

if (!_env) {
Expand All @@ -177,11 +215,19 @@ function configuration(env?: Readonly<NodeJS.ProcessEnv>): Configuration {
};
}

return new Configuration(stubGetInput(), _env);
return new Configuration(
stubGetInput({
command: 'yarn build',
'target-dir': './build',
'can-push': 'true',
...inputsConfiguration,
}),
_env
);
}

function stubGetInput(): typeof getInput {
function stubGetInput(inputsConfiguration: InputsConfiguration): typeof getInput {
return jest.fn((name: string): string => {
return name === 'command' ? 'yarn build' : './build';
return String(Object.hasOwn(inputsConfiguration, name) ? inputsConfiguration[name] : undefined);
});
}