GithubPackages
砖头
Sep 28 2019

# intro

Today we will see the new features of the github GitHub Package Registry (opens new window) !

videos from 7:00 (opens new window)

since I receive the email of the gihub and they offered me beta access to GitHub Package Registry. I decide to have a look what's new here.

this is offical configuration (opens new window)

# Authenticating to GitHub Package Registry

before we start this part, we need to know what's the github access token (opens new window)

well, we often ues ssh-key for a authenticating of github or gitlab, 【 if you do not know what's the ssh, please check here (opens new window) 】, but sometimes, we can not use ssh connect, so if we only have https connect, we will need to provide account & password or the token

we can enter the setting page of the github, and click the Developer settings (opens new window),we will see Personal access tokens on the left sidebar. then we need to click the Generate new token button to get a token (opens new window)

You must use a personal access token with the read:packages and write:packages scopes to publish and delete public packages in the GitHub Package Registry with npm. Your personal access token must also have the repo scope when the repository is private.

we can do like this, and click generate token.

Treat your tokens like passwords and keep them secret. When working with the API, use tokens as environment variables instead of hardcoding them into your programs.

now we get the token, so we can use it to authenticate.

# login to github npm

$ npm login --registry=https://npm.pkg.github.com --scope=@YOURNAME
> Username: USERNAME
> Password: TOKEN
> Email: PUBLIC EMAIL ADDRESS
1
2
3
4

USERNAME will be your github name, TOKEN will be the token we had generated just now

if success, you will see Logged in as USERNAME on https://npm.pkg.github.com/.

# Publish your package

  1. you will create a repo on github, name it hello, and then git clone your repo to local

  2. create .npmrc file echo registry=https://npm.pkg.github.com/USERNAME > .npmrc, we have to specify the registry so npm will know this is a github package

  3. verify the package.json filed name to be a scoped package, just like @USERNAME/hello

  4. npm publish, if success , you will see your package at https://github.com/USERNAME/hello/packages

  5. additional, if you want to publish a published package, you need to edit the package.json file, adding the flowing part

"publishConfig": {
  "registry":"https://npm.pkg.github.com/",
  "access": "public"
}
1
2
3
4

# install a github packge

  1. Also need .npmrc to specify which npm origin will be used echo registry=https://npm.pkg.github.com/USERNAME > .npmrc

  2. npm install @USERNAME/hello