Skip to content

Commit 0ce8949

Browse files
author
Kovacs Alex
committed
misc: see long description
- provide information as the action runs - don't fail in certain situations (if the action can't run npm ci or if it can't commit new changes because there aren't any)
1 parent 99bffa1 commit 0ce8949

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/index.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
const core = require('@actions/core');
24
const github = require('@actions/github');
35
const io = require('@actions/io');
@@ -8,16 +10,10 @@ const { join } = require('path');
810
// Inputs
911
const pushToBranch = Boolean(core.getInput('pushToBranch'));
1012
const branchName = core.getInput('branch');
11-
let author = core.getInput('author');
1213
const githubToken = core.getInput('githubToken');
1314
const directory = process.env.GITHUB_WORKSPACE;
1415

15-
// Checks
16-
if (pushToBranch) {
17-
if (typeof branchName !== 'string') return exit('The branch input is supposed to be a string (example: "dist")');
18-
if ((author && typeof author !== 'string') || !author) author = github.context.actor;
19-
if (!githubToken || typeof githubToken !== 'string') return exit('A GitHub secret token is a required input for pushing code (hint: use ${{ secrets.GITHUB_TOKEN }} )');
20-
}
16+
if (pushToBranch && !githubToken) return exit('A GitHub secret token is a required input for pushing code (hint: use ${{ secrets.GITHUB_TOKEN }} )');
2117

2218
(async () => {
2319
const tsconfigPath = join(directory, 'tsconfig.json');
@@ -27,10 +23,14 @@ if (pushToBranch) {
2723

2824
const tsconfig = require(tsconfigPath);
2925
const outDir = tsconfig.compilerOptions.outDir ? tsconfig.compilerOptions.outDir : directory;
30-
// Install TSC
26+
// Install tsc
27+
core.info('Installing tsc');
3128
await exec('npm i --g typescript');
32-
// Install dependencies
33-
await exec(`npm ci`, [], { cwd: directory });
29+
30+
core.info('Installing dependencies (if package-lock.json is present)');
31+
// We use the catch here because not everyone will have a package-lock.json
32+
await exec(`npm ci`, [], { cwd: directory }).catch(_err => { });
33+
3434
// Build project
3535
const build = await exec(`tsc`, [], { cwd: directory });
3636
if (build !== 0) return exit('Something went wrong while building.');
@@ -46,23 +46,32 @@ if (pushToBranch) {
4646

4747
const branchExists = branches.data.some(branch => branch.name.toLowerCase() === branchName);
4848
// Set up Git user
49-
await exec(`git config --global user.name ${author}`);
49+
core.info('Configuring Git user');
50+
await exec(`git config --global user.name actions-user`);
5051
await exec(`git config --global user.email action@github.com`);
52+
53+
core.info('Cloning branch')
5154
const clone = await exec(`git clone https://${github.context.actor}:${githubToken}@github.com/${owner}/${repo}.git branch-${branchName}`);
55+
if (clone !== 0) return exit('Something went wrong while cloning the repository.');
5256
// Check out to branch
5357
await exec(`${branchExists ? `git checkout ${branchName}` : `git checkout --orphan ${branchName}`}`, [], { cwd: `branch-${branchName}` });
54-
if (clone !== 0) return exit('Something went wrong while cloning the repository.');
5558

5659
// Copy compiled files and package* files
60+
core.info('Copying compiled files and package* files');
5761
await io.cp(join(directory, outDir), `branch-${branchName}`, { recursive: true });
5862
await io.cp(join(directory, 'package.json'), `branch-${branchName}`);
5963
await io.cp(join(directory, 'package-lock.json'), `branch-${branchName}`);
6064

6165
// Commit files
66+
core.info('Adding and commiting files');
6267
await exec(`git add ."`, [], { cwd: `branch-${branchName}` });
63-
await exec(`git commit -m "build: ${github.context.sha}"`, [], { cwd: `branch-${branchName}` });
68+
// We use the catch here because sometimes the code itself may not have changed
69+
await exec(`git commit -m "build: ${github.context.sha}"`, [], { cwd: `branch-${branchName}` }).catch(_err => core.warning("Couldn't commit new changes because there aren't any"));
70+
6471
// Push files
72+
core.info('Pushing new changes');
6573
await exec(`git push origin HEAD:${branchName}`, [], { cwd: `branch-${branchName}` });
74+
6675
process.exit(0);
6776
} catch (error) {
6877
exit(`Something went wrong: ${error}`);
@@ -72,4 +81,4 @@ if (pushToBranch) {
7281
function exit(error) {
7382
core.setFailed(error);
7483
process.exit();
75-
}
84+
}

0 commit comments

Comments
 (0)