Skip to content

Commit 9334458

Browse files
Merge pull request #21 from technote-space/release/v1.0.3
release/v1.0.3
2 parents de966c6 + 6eb17ef commit 9334458

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
runs-on: ubuntu-latest
2020
steps:
2121
- name: Release GitHub Actions
22-
uses: technote-space/[email protected].2
22+
uses: technote-space/[email protected].3
2323
with:
2424
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
2525
```

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"@actions/core": "^1.0.0",
2323
"@actions/github": "^1.0.0",
2424
"js-yaml": "^3.13.1",
25-
"rimraf": "^3.0.0",
2625
"signale": "^1.4.0"
2726
},
2827
"devDependencies": {

src/utils/command.ts

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import rimraf from 'rimraf';
43
import signale from 'signale';
54
import {exec} from 'child_process';
65
import {Context} from '@actions/github/lib/context';
@@ -12,41 +11,54 @@ export const deploy = async (branch: string, context: Context) => {
1211
const pushDir = path.resolve(workDir, 'push');
1312
signale.info(`Deploying branch %s to %s`, branch, getRepository(context));
1413

15-
rimraf(workDir, () => {
16-
});
1714
fs.mkdirSync(pushDir, {recursive: true});
18-
await prepareFiles(buildDir, pushDir, context);
19-
await cloneForBranch(pushDir, branch, context);
20-
await copyFiles(buildDir, pushDir);
21-
await config(pushDir);
22-
await commit(pushDir);
15+
if (!await cloneForBranch(pushDir, branch, context)) return;
16+
if (!await prepareFiles(buildDir, pushDir, context)) return;
17+
if (!await copyFiles(buildDir, pushDir)) return;
18+
if (!await config(pushDir)) return;
19+
if (!await commit(pushDir)) return;
2320
await push(pushDir, branch, context);
2421
};
2522

26-
export const prepareFiles = async (buildDir: string, pushDir: string, context: Context) => {
23+
export const prepareFiles = async (buildDir: string, pushDir: string, context: Context): Promise<boolean> => {
2724
signale.info('Preparing files for release');
2825

2926
fs.mkdirSync(buildDir, {recursive: true});
3027
await cloneForBuild(buildDir, context);
3128
await runBuild(buildDir);
29+
return true;
3230
};
3331

34-
const cloneForBranch = async (pushDir: string, branch: string, context: Context) => {
32+
const cloneForBranch = async (pushDir: string, branch: string, context: Context): Promise<boolean> => {
3533
signale.info(`Cloning the branch %s from the remote repo`, branch);
3634

3735
const url = getGitUrl(context);
3836
await execAsync(`git -C ${pushDir} clone --quiet --branch=${branch} --depth=1 ${url} .`, true, 'git clone', true);
39-
if (!fs.existsSync(path.resolve(pushDir, '.git'))) {
37+
38+
const clonedBranch = await getBranchName(pushDir);
39+
if (branch !== clonedBranch) {
40+
signale.info(`remote branch ${branch} not found.`);
41+
signale.info(`now branch: ${clonedBranch}`);
42+
43+
await execAsync(`rm -rdf ${pushDir}`);
44+
fs.mkdirSync(pushDir, {recursive: true});
4045
await gitInit(pushDir);
4146
await gitCheckout(pushDir, branch);
4247
}
48+
return true;
49+
};
50+
51+
const getBranchName = async (pushDir: string): Promise<string> => {
52+
if (!fs.existsSync(path.resolve(pushDir, '.git'))) {
53+
return '';
54+
}
55+
return (await execAsync(`git -C ${pushDir} branch -a | grep -E '^\\*' | cut -b 3-`)).trim();
4356
};
4457

4558
const gitInit = async (pushDir: string) => {
4659
signale.info('Initializing local git repo');
4760

4861
await execAsync(`git -C ${pushDir} init .`);
49-
5062
};
5163

5264
const gitCheckout = async (pushDir: string, branch: string) => {
@@ -55,27 +67,33 @@ const gitCheckout = async (pushDir: string, branch: string) => {
5567
await execAsync(`git -C ${pushDir} checkout --orphan "${branch}"`);
5668
};
5769

58-
const config = async (pushDir: string) => {
70+
const config = async (pushDir: string): Promise<boolean> => {
5971
const name = getCommitName();
6072
const email = getCommitEmail();
6173
signale.info('Configuring git committer to be %s <%s>', name, email);
6274

6375
await execAsync(`git -C ${pushDir} config user.name "${name}"`);
6476
await execAsync(`git -C ${pushDir} config user.email "${email}"`);
77+
return true;
6578
};
6679

67-
const commit = async (pushDir: string) => {
80+
const commit = async (pushDir: string): Promise<boolean> => {
6881
const message = getCommitMessage();
6982
await execAsync(`git -C ${pushDir} add --all --force`);
83+
if (!await checkDiff(pushDir)) {
84+
signale.info('There is no diff.');
85+
return false;
86+
}
7087
await execAsync(`git -C ${pushDir} commit -qm "${message}"`);
7188
await execAsync(`git -C ${pushDir} show --stat-count=10 HEAD`);
89+
return true;
7290
};
7391

7492
const push = async (pushDir: string, branch: string, context: Context) => {
7593
signale.info('Pushing to %s@%s', getRepository(context), branch);
7694

7795
const url = getGitUrl(context);
78-
await execAsync(`git -C ${pushDir} push --quiet "${url}" "${branch}":"${branch}"`, true, 'git push');
96+
await execAsync(`git -C ${pushDir} push --quiet "${url}" "${branch}":"refs/heads/${branch}"`, true, 'git push');
7997
};
8098

8199
const cloneForBuild = async (buildDir: string, context: Context) => {
@@ -97,19 +115,25 @@ const runBuild = async (buildDir: string) => {
97115
await execAsync(`cd ${current}`);
98116
};
99117

100-
const copyFiles = async (buildDir: string, pushDir: string) => {
118+
const copyFiles = async (buildDir: string, pushDir: string): Promise<boolean> => {
101119
signale.info('=== Copying %s contents to %s ===', buildDir, pushDir);
102120

103121
await execAsync(`rsync -rl --exclude .git --delete "${buildDir}/" ${pushDir}`);
122+
return true;
123+
};
124+
125+
const checkDiff = async (pushDir: string): Promise<boolean> => {
126+
return (await execAsync(`git -C ${pushDir} status --short -uno`)).split(/\r\n|\n/).filter(line => line.match(/^[MDA]\s+/)).length > 0;
104127
};
105128

106129
const execAsync = (command: string, quiet: boolean = false, altCommand: string | null = null, suppressError: boolean = false) => new Promise<string>((resolve, reject) => {
107130
if ('string' === typeof altCommand) signale.info(`Run command: ${altCommand}`);
108131
else if (!quiet) signale.info(`Run command: ${command}`);
109132
exec(command + (quiet ? ' > /dev/null 2>&1' : '') + (suppressError ? ' || :' : ''), (error, stdout) => {
110133
if (error) {
111-
if ('string' === typeof altCommand) reject(new Error(`command [${altCommand}] exited with code ${error.code}.`));
112-
else if (!quiet) reject(new Error(`command [${command}] exited with code ${error.code}.`));
134+
if ('string' === typeof altCommand && !quiet) reject(new Error(`command [${altCommand}] exited with code ${error.code}. message: ${error.message}`));
135+
else if ('string' === typeof altCommand) reject(new Error(`command [${altCommand}] exited with code ${error.code}.`));
136+
else if (!quiet) reject(new Error(`command [${command}] exited with code ${error.code}. message: ${error.message}`));
113137
else reject(new Error(`command exited with code ${error.code}.`));
114138
} else {
115139
if (!quiet) console.log(stdout);

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,13 +3119,6 @@ rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.3:
31193119
dependencies:
31203120
glob "^7.1.3"
31213121

3122-
rimraf@^3.0.0:
3123-
version "3.0.0"
3124-
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.0.tgz#614176d4b3010b75e5c390eb0ee96f6dc0cebb9b"
3125-
integrity sha512-NDGVxTsjqfunkds7CqsOiEnxln4Bo7Nddl3XhS4pXg5OzwkLqJ971ZVAAnB+DDLnF76N+VnDEiBHaVV8I06SUg==
3126-
dependencies:
3127-
glob "^7.1.3"
3128-
31293122
rsvp@^4.8.4:
31303123
version "4.8.5"
31313124
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.5.tgz#c8f155311d167f68f21e168df71ec5b083113734"

0 commit comments

Comments
 (0)