ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

GitHub——自动发布NPM包

2022-09-01 07:30:08  阅读:200  来源: 互联网

标签:NPM node GitHub run 脚本 npm actions 自动 build


前言

原理很简单,就是利用github的actions去触发上传到npm平台;

内容

?> 主要分为两个步骤:
1. 在NPM平台生成token
2. github配置secrets/actions

NPM生成token

登录npm平台, 生成一个token;

2022-08-31T22:39:47.png

GitHub配置secrets

登录Github平台, 配置secrets, 增加一个环境变量;
2022-08-31T22:47:29.png

GitHub配置actions

?> 请根据自身项目的实际情况做出配置 | 建议先创建nodejs脚本用于测试, 测试通过后再创建正式的发包脚本。

测试脚本

!> 1. 我的代码没有测试脚本所以直接把npm test干掉了;
2. 打包的时候使用的是自定义脚本(build.sh),所以这里将脚本替换成了自己项目下的打包脚本;
3. 如果项目是用自己定义的脚本打包整个项目, 一定要记得赋予脚本执行的权限;

git update-index --chmod=+x build.sh
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: cnblogs-theme-npm

on:
  push:
    branches: [ "v2" ]
  pull_request:
    branches: [ "v2" ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - run: ./build.sh

2022-08-31T22:53:44.png

发包脚本

?> 当建立release版本后,就会触发脚本进行发包;

# 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://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: NPM-PUBLISH

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
      - run: npm ci
      - run: ./build.sh

  publish-npm:
    needs: 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 ci
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_PUBLISH}}

标签:NPM,node,GitHub,run,脚本,npm,actions,自动,build
来源: https://www.cnblogs.com/wangyang0210/p/16645158.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有