赞
踩
Access Tokens
选项。Generate New Token
按钮,选择 Classic Token
选项。Automation
选项。Generate Token
按钮生成 token,复制保存生成的 token,防止丢失,不要泄漏出去。Settings
选项。Actions
功能的,如果没有开启,需要手动在 Settings
里的 Actions > General
选项开启。Secrets and variables
下的 Actions
选项,点击 New repository secret
按钮。NPM_TOKEN
,value:输入刚才生成的 token,点击 Add secret
按钮保存。.github/workflows
目录。.github/workflows
目录下创建 publish.yml
文件。publish.yml
文件内容如下:# .github/workflows/publish.yml name: Publish Package to npmjs on: push: tags: - 'v*' jobs: build: runs-on: ubuntu-latest permissions: contents: read id-token: write steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '20.x' registry-url: 'https://registry.npmjs.org' - run: npm install -g npm - run: npm ci - run: npm publish env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN
就是上面配置的 Secrets 的 name。package.json
文件。npm init -y
{
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"provenance": true
}
}
{ "name": "mine-npm-demo", "version": "1.0.0", "description": "This is a sample demo about npm", "main": "index.js", "private": false, "scripts": {}, "keywords": [ "npm", "demo" ], "engines": { "node": ">=20" }, "type": "module", "author": { "name": "biaov", "email": "biaov@qq.com" }, "license": "ISC", "repository": { "type": "git", "url": "git+https://github.com/biaov/mine-npm-demo.git" }, "homepage": "https://github.com/biaov/mine-npm-demo", "bugs": { "url": "https://github.com/biaov/mine-npm-demo/issues" }, "publishConfig": { "registry": "https://registry.npmjs.org/", "provenance": true }, "contributors": [ { "name": "biaov", "email": "biaov@qq.com" } ] }
package-lock.json
文件,以便于后面执行 npm ci
命令。npm i
git add .
和 git commit -m 'feat: add publish.yml'
命令。git push
命令提交代码到远程仓库。git tag v1.0.0
命令创建 tag。git push origin v1.0.0
命令推送 tag 到远程仓库。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。