why
为何会生出这个想法,因为别人pr了我的库,我每次都要去手动pull =>build=>publish。这个过程的痛点在于
- 我的本地需要有仓库代码
- npm publish 登录验证
how
基础知识
Microsoft 基于 Github Action 的 CI/CD 流程 5 分钟教你快速掌握 GitHub Actions 自动发布 Npm 包和网站
实战操作
在** 基础知识** 中,我们了解到了基本github ci/cd 的概念。我们现在需要做的是配置 github action,使其可以发布npm包
npm包持续集成
生成 npm accessToken

生成 github accessToken
https://github.com/settings/tokens

在仓库中配置token
把 npm 和 github 的token都配置进去,名称随意
使用方式
| TokenName | Key | Value |
|---|---|---|
| Personal Access Token | ACCESS_TOKEN | ${{ secrets.ACCESS_TOKEN }} (你刚刚配置的github accessToken的名称) |
| Npm Access Token | NODE_AUTH_TOKEN | ${{secrets.NPM_TOKEN}} (你刚刚配置的npmToken的名称) |
写workflow.yml
name: "publish to npm"
on: workflow_dispatch
jobs:
main:
runs-on: ubuntu-latest
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v2
with:
token: ${{ secrets.ACCESS_TOKEN }}
fetch-depth: 0
- name: ⎔ Setup node
# sets up the .npmrc file to publish to npm
uses: actions/setup-node@v2
with:
node-version: '16.13.0'
registry-url: "https://registry.npmjs.org"
- name: 📥 Download deps
uses: bahmutov/npm-install@v1
with:
useLockFile: false
- name: Configure git user
run: |
git config --global user.email ${{ github.actor }}@users.noreply.github.com
git config --global user.name ${{ github.actor }}
- name: ▶️ Run release
run: npm run release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
关于workflow的写法
github上很多优秀的项目都配置了workflow,可以去参考这些项目怎么写。有多action也被封装好了,可以在market中搜到
一些问题
- npm权限
npm notice
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`
npm ERR! A complete log of this run can be found in:
npm ERR! /home/runner/.npm/_logs/2022-05-16T02_33_55_906Z-debug.log
Error: Process completed with exit code 1.
解决方案:配置npm 2FA,然后配置 自动化 npm token