Github Package npm 应用发布实践
创始人
2024-05-02 19:47:18
0

Github Package npm 应用发布实践


文章目录

  • 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

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 ` 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 Packageon:push:branches:- mainjobs:build:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v3- uses: actions/setup-node@v3with:node-version: 16- run: npm ci- run: npm testpublish-gpr:needs: buildruns-on: ubuntu-latestpermissions:packages: writecontents: readsteps:- uses: actions/checkout@v3- uses: actions/setup-node@v3with:node-version: 16registry-url: https://npm.pkg.github.com/- run: npm ci- run: npm publishenv: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 构建流程

发布的 npm 包

9. 管理 npm 包

将默认的 private 包转为 public

参考:

  • GitHub Packages 快速入门

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...