CLASSFUNC BLOG
We Share Our Knowledge
Public new npm private package to github package
Dam Van Duong
2 Th11 2022 11:24

Public Github Package Private

Bước 1: Tạo file Github Workflows

mkdir .github cd .github mkdir workflows cd workflows touch github-packages.yml

Example file github-packages.yml

# 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://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages name: GlobalNavi Publish Packages 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 install - run: npm ci publish-gpr: needs: build runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: node-version: 16 registry-url: https://npm.pkg.github.com/ - run: npm install - run: npm ci - run: npm publish env: NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

Ở file này chú ý Github actions này sẽ chạy khi tạo mới 1 bản release trên Github Repository, mình có thể chuyển actions thành "Push to new branch"

on: push: # Sequence of patterns matched against refs/heads branches: - main

Bước 2: Cập nhật file package.json và thêm .npmrc lưu trữ thông tin Authentication to GitHub Packages

Package.json thêm các thuộc tính:

"repository": "https://github.com/Andom-Geniam/git-globalnavi", "publishConfig": { "@Andom-Geniam:registry": "https://npm.pkg.github.com" }

.npmrc thêm

//npm.pkg.github.com/:_authToken=Token @Andom-Geniam:registry=https://npm.pkg.github.com

_authToken tạo ở https://github.com/settings/tokens (dung classic token) https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry

Ảnh màn hình 2022-11-02 lúc 17.30.42.png

Lưu ý: Cấp các quyền đầy đủ cho Token (Chủ yếu là access to actions, packages, and workflows)

Tham khảo: https://docs.github.com/en/packages/quickstart