Skip to content

Commit

Permalink
Add Release CI & change release flow (#256)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xingoxu committed Sep 18, 2020
1 parent e98bb5b commit a2560a5
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 2 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/commit-changelog.yml
@@ -0,0 +1,33 @@
name: Changelog CI

# Controls when the action will run. Triggers the workflow on a pull request
on:
pull_request:
# types: [opened, reopened]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install Dependency
run: npm i

- name: Config Internal Git
run: |
git config --global user.email "action@github.com"
git config --global user.name "GitHub Action"
- name: Run Changelog CI & Bump npm version
if: ${{ startsWith(github.event.pull_request.title, 'Release') }}
run: npm run generate-changelog

- name: Copy & Deploy
if: ${{ startsWith(github.event.pull_request.title, 'Release') }}
run: |
git add -A
git commit -m '(Changelog CI) Added Changelog'
git push
18 changes: 18 additions & 0 deletions .github/workflows/release.yml
@@ -0,0 +1,18 @@
name: Release Node.js Package
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Setup .npmrc file to publish to GitHub Packages
- uses: actions/setup-node@v1
with:
node-version: 14
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm run release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion CHANGELOG.md
@@ -1,4 +1,4 @@
## 7.0.0 (15 June 202)
## 7.0.0 (15 June 2020)

### Breaking Changes
* Node.js: drop 8 & adopt 14 (#222)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -22,7 +22,8 @@
"build": "tsc",
"docs": "vuepress dev docs",
"docs:build": "vuepress build docs",
"docs:deploy": "./deploy-docs.sh",
"docs:deploy": "./scripts/deploy-docs.sh",
"generate-changelog": "ts-node ./scripts/generate-changelog.ts",
"release": "npm run build && npm publish --access public"
},
"repository": {
Expand Down
File renamed without changes.
103 changes: 103 additions & 0 deletions scripts/generate-changelog.ts
@@ -0,0 +1,103 @@
import { execSync } from "child_process";
import { readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
const { version: lastVersion } = require("../package.json");

const changeLogPath = resolve(__dirname, "../CHANGELOG.md");

let newVersion = lastVersion;

console.log("Gets Release Version from GITHUB_EVENT_PATH");
if (process.env.GITHUB_EVENT_PATH) {
const {
pull_request: { title },
} = require(process.env.GITHUB_EVENT_PATH);

if (/^release/i.test(title))
newVersion = (title as string).match(/release ([\d\.]+)/i)[1];
else {
console.log("Not target pull request, exiting");
process.exit(0);
}
}
console.log(`New Version: ${newVersion}`);

console.log("Bump Version");
execSync(`npm version ${newVersion}`);

const gitLogOutput = execSync(
`git log v${lastVersion}... --format=%s`
).toString("utf-8");

const commitsArray = gitLogOutput
.split("\n")
.filter((message) => message && message !== "");

const category = {
miscs: [] as string[],
features: [] as string[],
bugFixes: [] as string[],
};

commitsArray.forEach((message) => {
let cat: keyof typeof category;
if (message.includes("test")) {
cat = "miscs";
} else if (/(add)|(support)/i.test(message)) {
cat = "features";
} else if (/fix/i.test(message)) {
cat = "bugFixes";
} else {
cat = "miscs";
}
category[cat].push(`* ${message}`);
});

const now = new Date();
const MonthText = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
let newChangelog = `## ${newVersion} (${now.getDate()} ${
MonthText[now.getMonth()]
} ${now.getFullYear()})
`;

if (category.features.length > 0) {
newChangelog += `
### Feature
${category.features.join("\n")}
`;
}

if (category.bugFixes.length > 0) {
newChangelog += `
### Bug fix
${category.bugFixes.join("\n")}
`;
}

if (category.miscs.length > 0) {
newChangelog += `
### Misc
${category.miscs.join("\n")}
`;
}

const currentChangelog = readFileSync(changeLogPath, "utf-8");

writeFileSync(
changeLogPath,
`${newChangelog}
${currentChangelog}`
);

0 comments on commit a2560a5

Please sign in to comment.