Skip to content

Commit 803a625

Browse files
committed
Add Release CI & change release flow (#256)
* add release ci test changelog ci add changelog ci add release ci * fix typo & add npm i * set config before run changelog ci * fix git push error
1 parent e98bb5b commit 803a625

File tree

6 files changed

+158
-2
lines changed

6 files changed

+158
-2
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Changelog CI
2+
3+
# Controls when the action will run. Triggers the workflow on a pull request
4+
on:
5+
pull_request:
6+
# types: [opened, reopened]
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
with:
15+
ref: ${{ github.event.pull_request.head.ref }}
16+
fetch-depth: 0
17+
- name: Install Dependency
18+
run: npm i
19+
20+
- name: Config Internal Git
21+
run: |
22+
git config --global user.email "action@github.com"
23+
git config --global user.name "GitHub Action"
24+
25+
- name: Run Changelog CI & Bump npm version
26+
if: ${{ startsWith(github.event.pull_request.title, 'Release') }}
27+
run: npm run generate-changelog
28+
29+
- name: Copy & Deploy
30+
if: ${{ startsWith(github.event.pull_request.title, 'Release') }}
31+
run: |
32+
git add -A
33+
git commit -m '(Changelog CI) Added Changelog'
34+
git push

.github/workflows/release.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Release Node.js Package
2+
on:
3+
release:
4+
types: [created]
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
# Setup .npmrc file to publish to GitHub Packages
11+
- uses: actions/setup-node@v1
12+
with:
13+
node-version: 14
14+
registry-url: 'https://registry.npmjs.org'
15+
- run: npm install
16+
- run: npm run release
17+
env:
18+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 7.0.0 (15 June 202)
1+
## 7.0.0 (15 June 2020)
22

33
### Breaking Changes
44
* Node.js: drop 8 & adopt 14 (#222)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"build": "tsc",
2323
"docs": "vuepress dev docs",
2424
"docs:build": "vuepress build docs",
25-
"docs:deploy": "./deploy-docs.sh",
25+
"docs:deploy": "./scripts/deploy-docs.sh",
26+
"generate-changelog": "ts-node ./scripts/generate-changelog.ts",
2627
"release": "npm run build && npm publish --access public"
2728
},
2829
"repository": {
File renamed without changes.

scripts/generate-changelog.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { execSync } from "child_process";
2+
import { readFileSync, writeFileSync } from "fs";
3+
import { resolve } from "path";
4+
const { version: lastVersion } = require("../package.json");
5+
6+
const changeLogPath = resolve(__dirname, "../CHANGELOG.md");
7+
8+
let newVersion = lastVersion;
9+
10+
console.log("Gets Release Version from GITHUB_EVENT_PATH");
11+
if (process.env.GITHUB_EVENT_PATH) {
12+
const {
13+
pull_request: { title },
14+
} = require(process.env.GITHUB_EVENT_PATH);
15+
16+
if (/^release/i.test(title))
17+
newVersion = (title as string).match(/release ([\d\.]+)/i)[1];
18+
else {
19+
console.log("Not target pull request, exiting");
20+
process.exit(0);
21+
}
22+
}
23+
console.log(`New Version: ${newVersion}`);
24+
25+
console.log("Bump Version");
26+
execSync(`npm version ${newVersion}`);
27+
28+
const gitLogOutput = execSync(
29+
`git log v${lastVersion}... --format=%s`
30+
).toString("utf-8");
31+
32+
const commitsArray = gitLogOutput
33+
.split("\n")
34+
.filter((message) => message && message !== "");
35+
36+
const category = {
37+
miscs: [] as string[],
38+
features: [] as string[],
39+
bugFixes: [] as string[],
40+
};
41+
42+
commitsArray.forEach((message) => {
43+
let cat: keyof typeof category;
44+
if (message.includes("test")) {
45+
cat = "miscs";
46+
} else if (/(add)|(support)/i.test(message)) {
47+
cat = "features";
48+
} else if (/fix/i.test(message)) {
49+
cat = "bugFixes";
50+
} else {
51+
cat = "miscs";
52+
}
53+
category[cat].push(`* ${message}`);
54+
});
55+
56+
const now = new Date();
57+
const MonthText = [
58+
"Jan",
59+
"Feb",
60+
"Mar",
61+
"Apr",
62+
"May",
63+
"Jun",
64+
"Jul",
65+
"Aug",
66+
"Sep",
67+
"Oct",
68+
"Nov",
69+
"Dec",
70+
];
71+
let newChangelog = `## ${newVersion} (${now.getDate()} ${
72+
MonthText[now.getMonth()]
73+
} ${now.getFullYear()})
74+
`;
75+
76+
if (category.features.length > 0) {
77+
newChangelog += `
78+
### Feature
79+
${category.features.join("\n")}
80+
`;
81+
}
82+
83+
if (category.bugFixes.length > 0) {
84+
newChangelog += `
85+
### Bug fix
86+
${category.bugFixes.join("\n")}
87+
`;
88+
}
89+
90+
if (category.miscs.length > 0) {
91+
newChangelog += `
92+
### Misc
93+
${category.miscs.join("\n")}
94+
`;
95+
}
96+
97+
const currentChangelog = readFileSync(changeLogPath, "utf-8");
98+
99+
writeFileSync(
100+
changeLogPath,
101+
`${newChangelog}
102+
${currentChangelog}`
103+
);

0 commit comments

Comments
 (0)