使用Github?Actions發(fā)布npm包完整過程詳解
本地發(fā)布npm包
發(fā)布本地npm包首先需要初始化npm,設(shè)置npm源,登錄npm,發(fā)布npm
初始包
npm init
生成 package.json包,如下是我的 package.json配置:
{
"name": "vuepress-plugin-md-tags", // 包名
"version": "1.0.7", // 版本
"description": "vuepress@1.x plugin", // 描述
"main": "index.js", // 入口文件
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": { // 倉庫
"type": "git",
"url": "https://github.com/HerryLo/vuepress-plugin-md-tags.git"
},
"keywords": ["vuepress@1.x", "vuepress-plugin", "tags"], // 關(guān)鍵字
"author": "herrylo3", // 作者
"license": "ISC", // license
"bugs": {
"url": "https://github.com/HerryLo/vuepress-plugin-md-tags/issues"
},
"homepage": "https://github.com/HerryLo/vuepress-plugin-md-tags"
}設(shè)置npm源
npm config set registry https://registry.npmjs.org/ 或者 nrm set npm
如果你發(fā)布包到 公有npm平臺,設(shè)置 https://registry.npmjs.org/即可,如果是私有平臺,記得設(shè)置私有npm源。
登錄npm
npm login
本地執(zhí)行命令 npm login 進(jìn)行登錄,需要輸入你的 npm用戶名、密碼、郵箱地址,還有郵箱驗(yàn)證碼,都搞完,才算是本地登錄成功。
發(fā)布包
在包目錄下,本地執(zhí)行命令執(zhí)行命令 npm publish
npm publish
每次發(fā)布包時(shí),記得修改 package.json 下的vesion版本號!
Github Action發(fā)布npm包
首先你肯定需要把代碼上傳到Github上,之后設(shè)置Github Action配置文件,在npm上生成 Granular Access Token,再在Github Action上設(shè)置 Actions secrets 字段。
::: tip
介紹以下內(nèi)容時(shí),默認(rèn)你的代碼以上傳到Github↓↓
:::
Github Action配置文件
生成你自己項(xiàng)目的Github Action配置文件,Github有提供模板,選擇圖片中框出的模板即可

下面是我自己的配置文件
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
name: Node.js Package
on:
release:
types: [created]
push:
#分支可以選擇多個(gè),如:master、main、release
#監(jiān)聽main分支的push操作,只要main分支發(fā)生push操作,即會(huì)運(yùn)行發(fā)布代碼
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm install
- run: npm publish
env:
# 通過NPM_TOKEN,Github Action才可以直接發(fā)布
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}添加npm token

生成token之后,馬上復(fù)制,刷新之后就看不見了。之后將token,設(shè)置到github項(xiàng)目setting中

記得點(diǎn)擊Add secret按鈕哦!
自動(dòng)發(fā)布包
on:
release:
types: [created]
push:
#分支可以選擇多個(gè),如:master、main、release
#監(jiān)聽main分支的push操作,只要main分支發(fā)生push操作,即會(huì)運(yùn)行發(fā)布代碼
branches:
- main設(shè)置的為每次 push 或 merge 會(huì)觸發(fā) github actions 自動(dòng)發(fā)布包。當(dāng)然,如何監(jiān)聽觸發(fā),你可以自己修改。

npm包
npm包:https://github.com/HerryLo/vuepress-plugin-md-tags
最近自己發(fā)布了 vuepress@1.x 插件npm包,如果你有使用vuepress@1.x,可以看看。
::: tip
npm包中的README.md文件,在上傳到npm倉庫之后,即是npm包首頁,請大家合理編寫README.md,便于別人理解使用!
:::
結(jié)束
文章蠻簡單,到這里就結(jié)束了,主要是關(guān)于npm包發(fā)布的流程步驟方法,希望以上內(nèi)容,可以幫助到你。
更多關(guān)于Github Actions發(fā)布npm包的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
es6學(xué)習(xí)之解構(gòu)時(shí)應(yīng)該注意的點(diǎn)
解構(gòu)賦值允許你使用類似數(shù)組或?qū)ο笞置媪康恼Z法將數(shù)組和對象的屬性賦給各種變量。這種賦值語法極度簡潔,同時(shí)還比傳統(tǒng)的屬性訪問方法更為清晰,下面這篇文章主要給大家介紹了關(guān)于在es6解構(gòu)時(shí)應(yīng)該注意的點(diǎn),需要的朋友可以參考下。2017-08-08
JS實(shí)現(xiàn)獲取GIF總幀數(shù)的方法詳解
如何通過js在上傳前就拿到它的總幀數(shù)來判斷呢?本文就跟大家分享一種解決方案,并將其封裝成插件發(fā)布至npm倉庫,快跟隨小編一起學(xué)習(xí)一下吧2022-05-05
JavaScript實(shí)現(xiàn)旋轉(zhuǎn)輪播圖
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)旋轉(zhuǎn)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
JS實(shí)現(xiàn)從頂部下拉顯示的帶動(dòng)畫QQ客服特效代碼
這篇文章主要介紹了JS實(shí)現(xiàn)從頂部下拉顯示的帶動(dòng)畫QQ客服特效代碼,可實(shí)現(xiàn)彈性緩沖效果的彈出QQ客服窗口的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

