首页 > 其他分享 >Github Package npm 应用发布实践

Github Package npm 应用发布实践

时间:2022-12-31 10:02:45浏览次数:64  
标签:npm Github package GitHub Package json github packages


Github Package npm 应用发布实践

Github Package npm 应用发布实践_github

文章目录

  • ​​Github Package npm 应用发布实践​​
  • ​​1. 简介​​
  • ​​2. 创建新库​​
  • ​​3. 编写 index.js​​
  • ​​4. npm init 初始化​​
  • ​​5. npm install 安装包​​
  • ​​6. 编写 release-package.yml​​
  • ​​7. 发布​​
  • ​​8. 查看已发布包​​
  • ​​9. 管理 npm 包​​

1. 简介

​GitHub Packages​​ 是一个用于托管和管理包的平台,包括容器和其他依赖项。 GitHub Packages 将源代码和包组合在一起,以提供集成的权限管理和计费,使你能够在 GitHub 上专注于软件开发。

您可以将 GitHub Packages 与 ​​GitHub API​​​、​​GitHub Actions​​ 以及 web 挂钩集成在一起,以创建端到端的 DevOps 工作流程,其中包括您的代码、CI 和部署解决方案。

GitHub Packages 为常用的包管理器提供不同的包仓库,例如 npm、RubyGems、Apache Maven、Gradle、Docker 和 Nuget。 GitHub 的 Container registry 针对容器进行了优化,支持 Docker 和 OCI 映像。

今天,我尝试实现 Github Package npm 应用发布实践。

2. 创建新库

名字:​​github-packages-npm-demo​

Github Package npm 应用发布实践_npm_02

3. 编写 index.js

创建 ​​index.js​​ 文件,并添加指示“Hello world!”的基本警报

console.log("Hello, World!");

4. npm init 初始化

使用 ​​npm init​​​ 初始化 npm 包。 在包初始化向导中,输入名称为 ​​@YOUR-USERNAME/YOUR-REPOSITORY​​​ 的包,并将测试脚本设置为 ​​exit 0​​​。 这将生成一个 ​​package.json​​ 文件,其中包含有关包的信息。

如何安装 npm,可以​​参考这篇文章​​。

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (github-packages-npm-demo) @ghostwritten/github-packages-npm-demo
version: (1.0.0)
description: github packages npm demo
entry point: (index.js)
test command: exit 0
git repository: (https://github.com/Ghostwritten/github-packages-npm-demo.git)
keywords:
author: ghostwritten
license: (ISC)
About to write to /root/github/github-packages-npm-demo/package.json:

{
"name": "@ghostwritten/github-packages-npm-demo",
"version": "1.0.0",
"description": "github packages npm demo",
"main": "index.js",
"scripts": {
"test": "exit 0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Ghostwritten/github-packages-npm-demo.git"
},
"author": "ghostwritten",
"license": "ISC",
"bugs": {
"url": "https://github.com/Ghostwritten/github-packages-npm-demo/issues"
},
"homepage": "https://github.com/Ghostwritten/github-packages-npm-demo#readme"
}


Is this OK? (yes) yes

$ ls
index.js package.json

5. npm install 安装包

运行 ​​npm install​​​ 以生成 ​​package-lock.json​​ 文件,然后提交更改并将其推送到 GitHub。

$ npm install
npm notice created a lockfile as package-lock.json. You should commit this file.
up to date in 3.702s
found 0 vulnerabilities



╭───────────────────────────────────────────────────────────────╮
│ │
│ New major version of npm available! 6.14.12 → 9.2.0 │
│ Changelog: https://github.com/npm/cli/releases/tag/v9.2.0 │
│ Run npm install -g npm to update! │
│ │
╰───────────────────────────────────────────────────────────────╯


$ ls
index.js package.json package-lock.json

$ git add index.js package.json package-lock.json
$ git commit -m "initialize npm package"
$ git push

6. 编写 release-package.yml

创建 ​​.github/workflows​​​ 目录。 在此目录中,创建名为 ​​release-package.yml​​ 的文件。

name: Node.js Package

on:
push:
branches:
- main

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

publish-gpr:
needs: build
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://npm.pkg.github.com/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

如何生成 ​​TOKEN​​,请​​参考这篇文章​​。

7. 发布

告诉 NPM 使用以下方法之一发布包的范围和仓库:

  • 第一种方法
    在根目录中创建包含以下内容的 ​​​.npmrc​​ 文件,为存储库添加 NPM 配置文件
@YOUR-USERNAME:registry=https://npm.pkg.github.com
  • 第二种方法:
    编辑 ​​​package.json​​​ 文件并指定 ​​publishConfig​​ 密钥:
"publishConfig": {
"@YOUR-USERNAME:registry": "https://npm.pkg.github.com"
}

提交并推送更改到 GitHub。

$ git add .github/workflows/release-package.yml
# Also add the file you created or edited in the previous step.
$ git add .npmrc or package.json
$ git commit -m "workflow to publish package"
$ git push

只要您的仓库中创建新版本,您创建的工作流程就会运行。 如果测试通过,则包将发布到 GitHub Packages。

8. 查看已发布包

workflow 构建流程

Github Package npm 应用发布实践_git_03

发布的 npm 包

Github Package npm 应用发布实践_github_04

9. 管理 npm 包

将默认的 ​​private​​​ 包转为 ​​public​​ 包

Github Package npm 应用发布实践_json_05

Github Package npm 应用发布实践_github_06

参考:


标签:npm,Github,package,GitHub,Package,json,github,packages
From: https://blog.51cto.com/ghostwritten/5981896

相关文章

  • github-keydb 知识
    https://github.com/Snapchat/KeyDB KeyDBisnowapartofSnapInc!Checkouttheannouncement hereReleasev6.3.0 isherewithmajorimprovementsaswe......
  • npm---缓存
    一、缓存位置window下的缓存位置(npm全局位置):user\xxx\AppData\Roaming\npm-cache当中的_cacache文件夹二、清理缓存//强制删除缓存npmcacheclean-f//无论在项目中......
  • Day 04 npm与包
    包一、什么是包第三方模块又称为包基于内置模块封装出来的,提供了更高级、更方便的API搜索包:https://www.npmjs.com/下载包:官方提供了NodePackageManager(简称:NPM包......
  • github报错(完美解决):获取token。remote: Support for password authentication was rem
    文章目录​​前言​​​​一、需求背景环境​​​​一、问题描述​​​​1、截图​​​​2、报错信息解读​​​​二、寻找破解之法​​​​1、进入网址​​​​2、创建个......
  • 解决fatal: unable to connect to github.com: github.com[0: 20.205.243.166]: err
    输入下面命令行pipinstall"git+git://github.com/erikwijmans/Pointnet2_PyTorch.git#egg=pointnet2_ops&subdirectory=pointnet2_ops_lib"出现错误  解决方法......
  • npm切换淘宝镜像源
    在我们使用npm时,有时经常会卡住,有时又很慢,这时候我们可以更换为淘宝镜像源安装依赖包:配置npm使用淘宝镜像源npmconfiggetregistry配置后可通过下面方式来验证是否......
  • GitHub关于时间单位的单词速记表
    音标GitHub[ɡɪthʌb]GitLab[ɡɪtlæb]时间一月Jan/January二月Feb/February三月Mar/March四月Apr/April五月May六月Jun/June七月Jul/July八月Aug......
  • 安装 PHP No package 'libxml-2.0' found
    centos7编译安装php7遇到的问题:./configure配置遇到的Nopackage‘libxml-2.0‘found缺失libxml2.0库,解决方法:yum-yinstalllibxml2yum-yinstalllibxml2-devel......
  • Java SE:static package this关键字与访问权限
    JAVA课程-类和对象static修饰符1.static修饰变量在类中,被static修饰的变量叫做类变量,否则叫做实例变量。类变量:例如staticinta,为整个类中所有对象所共享,只能存在一......
  • git 本地电脑重新装git后 更新github项目报错 fatal: detected dubious ownership in
    解决方法参考:fatal:detecteddubiousownershipinrepositoryat‘D:/‘之解决方法 1、今天在学习git的时候出现这个错误:2、执行下面代码即可:gitconfig--glob......