Skip to content

Commit 24c3b22

Browse files
authored
feat: add GitHub release & NPM publish workflow (#12)
1 parent bd97d23 commit 24c3b22

File tree

6 files changed

+175
-9
lines changed

6 files changed

+175
-9
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
name: Release Creation on GitHub & NPM Publishing
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch: {}
8+
9+
jobs:
10+
check_conditions:
11+
name: Check conditions to create & release
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
outputs:
16+
run_suffix_present: ${{ steps.check_run_suffix_present.outputs.run_suffix_present }}
17+
run_suffix_force_version: ${{ steps.check_run_suffix_present.outputs.run_suffix_force_version }}
18+
steps:
19+
- name: Checkout the Codebase
20+
uses: actions/checkout@v4
21+
with:
22+
ref: main # branch to checkout
23+
fetch-depth: 0
24+
25+
- name: Get last commit message
26+
run: echo "message=$(git log -1 --pretty=%B)" >> $GITHUB_ENV
27+
28+
- name: Check commit message suffix for `[create-release]`
29+
id: check_run_suffix_present
30+
uses: actions/github-script@v7
31+
with:
32+
script: |
33+
let run_suffix_present = "false";
34+
let run_suffix_force_version = "";
35+
36+
const commitMessage = process.env.message;
37+
const regex = /\[create-release(?:-([a-z]+))?\]$/;
38+
const match = commitMessage.match(regex);
39+
40+
if (match) {
41+
run_suffix_present = "true";
42+
if(match[1]){
43+
run_suffix_force_version = `--${match[1]}`
44+
}
45+
}
46+
47+
console.dir(`Set output: run_suffix_present=${run_suffix_present}`);
48+
core.setOutput("run_suffix_present", run_suffix_present);
49+
50+
console.dir(`Set output: run_suffix_force_version=${run_suffix_force_version}`);
51+
core.setOutput("run_suffix_force_version", run_suffix_force_version);
52+
53+
dependence-lint:
54+
name: Depend on Lint
55+
uses: ./.github/workflows/lint.yml
56+
57+
create_release:
58+
needs:
59+
- check_conditions
60+
- dependence-lint
61+
if: needs.check_conditions.outputs.run_suffix_present == 'true'
62+
name: Create GitHub Release & NPM Publish
63+
runs-on: ubuntu-latest
64+
permissions:
65+
contents: write
66+
steps:
67+
- name: Checkout the Codebase
68+
uses: actions/checkout@v4
69+
with:
70+
ref: main # branch to checkout
71+
fetch-depth: 0
72+
73+
- name: Setup Node.js
74+
uses: actions/setup-node@v4
75+
with:
76+
node-version: 22
77+
78+
- name: Enable pnpm
79+
run: |
80+
corepack enable
81+
corepack prepare pnpm@v9.15.4 --activate
82+
83+
- name: Init git config for the bot by setting name & email
84+
run: |
85+
git config --global user.name "github-actions[bot]"
86+
git config --global user.email "no-reply@todde.tv"
87+
88+
# - name: Get old `package.json` version
89+
# id: get_package_version_old
90+
# run: echo version=$(node -p "require('./package.json').version") >> $GITHUB_OUTPUT
91+
92+
- name: Bump `package.json` version & generate changelog both from conventional commits
93+
# -from "v${{ steps.get_package_version_old.outputs.version }}"
94+
run: npx changelogen@0.5.7 --output CHANGELOG.md --bump --no-commit --no-tag ${{ needs.check_conditions.outputs.run_suffix_force_version }}
95+
env:
96+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
97+
98+
- name: Get new `package.json` version
99+
id: get_package_version_new
100+
run: echo version=$(node -p "require('./package.json').version") >> $GITHUB_OUTPUT
101+
102+
- name: Git add, commit & push changes made to `package.json` & `CHANGELOG.md` to `main`
103+
run: |
104+
git add .
105+
git commit -m "chore(release): v${{ steps.get_package_version_new.outputs.version }}"
106+
git push origin main
107+
108+
- name: Create a tag
109+
run: |
110+
git tag -a "v${{ steps.get_package_version_new.outputs.version }}" -m "Release v${{ steps.get_package_version_new.outputs.version }}"
111+
git push origin --tags
112+
113+
- name: Sync tags with GitHub releases & create the new release
114+
# run: npx changelogen@0.5.7 gh release "v${{ steps.get_package_version_new.outputs.version }}"
115+
# run: npx changelogen@0.5.7 gh release
116+
run: npx changelogen@0.5.7 gh release all
117+
env:
118+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
119+
120+
- name: Install dependencies
121+
# TODO fix this bc currently using the frozen lockfile will fail due to the patch inside `package.json`
122+
# run: pnpm install --frozen-lockfile
123+
run: pnpm install --no-frozen-lockfile
124+
125+
- name: Build the project
126+
run: pnpm run build
127+
128+
# - name: Publish package to NPM
129+
# if: github.ref == 'refs/heads/main'
130+
# run: pnpm publish --access public
131+
# env:
132+
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
133+
134+
- name: Publish package to NPM
135+
# if: github.ref == 'refs/heads/main'
136+
uses: JS-DevTools/npm-publish@v3
137+
with:
138+
token: ${{ secrets.NPM_TOKEN }}
139+
access: public
140+
# registry: "https://npm.pkg.github.com/"
141+
registry: https://registry.npmjs.org/

.github/workflows/lint.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
name: Linting
22

3-
permissions:
4-
contents: read
5-
63
on:
7-
push:
8-
branches:
9-
- main
10-
- dev
114
pull_request:
125
branches:
136
- main
14-
- dev
157
workflow_dispatch: {}
8+
workflow_call: {}
169

1710
jobs:
1811
lint:
1912
name: Lint
2013
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
2116
steps:
2217
- name: Checkout the Codebase
2318
uses: actions/checkout@v4

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
README.dev.md

CHANGELOG.md

Whitespace-only changes.

README.dev.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ type foo = { bar: 2 }
5151

5252
[Why I don't use Prettier for every file type](https://antfu.me/posts/why-not-prettier)
5353

54+
## GitHub Release & NPM Publishing
55+
56+
Using the suffix `[create-release]` or `[create-release-TYPE]` in a commit message on branch `main` will trigger
57+
the GitHub workflow (CI action) that:
58+
59+
- uses `npx changelogen` to analyze the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) from
60+
the last version to the current commit for:
61+
- bumping the version in `package.json` via:
62+
- automatically when no `TYPE` is given
63+
- manually with a given `TYPE` where the following is possible:
64+
- `major`: Bump as a semver-major version
65+
- `minor`: Bump as a semver-minor version
66+
- `patch`: Bump as a semver-patch version
67+
- `premajor`: Bump as a semver-premajor version, can set id with string.
68+
- `preminor`: Bump as a semver-preminor version, can set id with string.
69+
- `prepatch`: Bump as a semver-prepatch version, can set id with string.
70+
- `prerelease`: Bump as a semver-prerelease version, can set id with string.
71+
- creating a new changelog entry in `CHANGELOG.md` from the last version to the new version
72+
- Committing these changes as author `github-actions[bot] <no-reply@todde.tv>` directly on branch `main`
73+
- Creating a tag at the new version commit on with the version number
74+
- Creating a GitHub Release out of it
75+
- Publishing the package into the [NPM Registry](https://registry.npmjs.org/)
76+
77+
You can use `npx changelogen --dry` to generate the new changelog in a dry run to preview in development.
78+
5479
## Docs and helper websites
5580

5681
\[currently none\]

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@
9696
}
9797
},
9898
"files": [
99-
"dist"
99+
"CHANGELOG.md",
100+
"LICENSE.md",
101+
"README.md",
102+
"dist",
103+
"package.json"
100104
],
101105
"engines": {
102106
"node": "22",

0 commit comments

Comments
 (0)