• 自动化发布npm包小记


    1.注册npm账号

    打开npm官网,并注册自己的npm账号

    2.申请AccessToken

    1.登录npm官网,登录成功后,点开右上角头像,并点击Access Tokens选项
    access token
    2.点开Generate New Token下拉框,点击Classic Token(和Granular Access Token有什么区别,请自行查验?)
    classic token
    3.填写token信息,选择token类型为Automation(可以避免2FA校验),保存申请的token信息(等下会用到)

    3.github新建代码仓库

    在github上面创建新的空仓库
    github仓库

    4.修改仓库设置

    1.进入新建的代码仓库,点击仓库tab选项卡的Settings
    在这里插入图片描述
    2.点开Secrets and variables选项卡,点击Actions选项,点击对应页面的New repository secret按钮
    在这里插入图片描述
    3.新建名称为NPM_TOKEN的secret, 并将刚刚申请到的npm token(前面要你保存的token)填入secret字段
    secret

    5.新增ci文件

    在代码根目录新建.github/workflow/ci.yml文件,文件内容如下

    name: CI
    on:
      push:
        branches:
    	  # 触发ci/cd的代码分支
          - master
    jobs:
      build:
        # 指定操作系统
        runs-on: ubuntu-latest
        steps:
          # 将代码拉到虚拟机
          - name: Checkout repository
            uses: actions/checkout@v2
          # 指定node版本
          - name: Use Node.js
            uses: actions/setup-node@v3
            with:
              node-version: '16.x'
              registry-url: 'https://registry.npmjs.org'
          # 依赖缓存策略
          - name: Cache
            id: cache-dependencies
            uses: actions/cache@v3
            with:
              path: |
                **/node_modules
              key: ${{runner.OS}}-${{hashFiles('**/pnpm-lock.yaml')}}
          # 安装pnpm
          - name: Install pnpm
            run: npm install -g pnpm@7.5.0
          # 依赖下载
          - name: Installing Dependencies
            if: steps.cache-dependencies.outputs.cache-hit != 'true'
            run: pnpm install
          # 打包
          - name: Running Build
            run: pnpm run build
          # 测试
          - name: Running Test
            run: pnpm run test-unit
          # 发布
          - name: Running Publish
            run: npm publish
            env:
              # NPM_TOKEN is access token
             NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    npm代码如下

    name: CI
    on:
      push:
        branches:
          # 触发ci/cd的代码分支
          - master
    jobs:
      build:
        # 指定操作系统
        runs-on: ubuntu-latest
        steps:
          # 将代码拉到虚拟机
          - name: Checkout repository
            uses: actions/checkout@v2
          # 指定node版本
          - name: Use Node.js
            uses: actions/setup-node@v3
            with:
              node-version: '16.x'
              registry-url: 'https://registry.npmjs.org'
          # 依赖缓存策略
          - name: Cache
            id: cache-dependencies
            uses: actions/cache@v3
            with:
              path: |
                **/node_modules
              key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}
          # 依赖下载
          - name: Installing Dependencies
            if: steps.cache-dependencies.outputs.cache-hit != 'true'
            run: npm install
          # 打包
          - name: Running Build
            run: npm run build
          # 测试
          - name: Running Test
            run: npm run test-unit
          # 发布
          - name: Running Publish
            run: npm publish
            env:
              # NPM_TOKEN is access token
             NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    6.推送代码至github

    在代码目录初始化git,并将代码推送到刚刚github上面新建的代码仓库master分支,会自动触发ci/cd进行自动化发包

  • 相关阅读:
    降维算法实战项目(1)—使用PCA对二维数据降维(Python代码+数据集)
    Tensorflow模型整体构建流程
    美团二面:为什么 Redis 会有哨兵?
    双目立体视觉
    Docker概述与基本使用
    ChatGPT-4 Turbo 今天开放啦!附如何查询GPT-4 是否为 Turbo
    STM32H7的LCD控制学习和应用
    组织机器学习代码
    IDEA 2022.3 发布,终于支持 redis 了
    算法 | 详解斐波那契数列问题
  • 原文地址:https://blog.csdn.net/luo1055120207/article/details/133134169