Publishing a npm private package to GitHub packages using GitHub actions
Published at 2022/03/19
In this tutorial I want to explain how to publish a private npm package to GitHub Packages using GitHub Actions. Also, the package will be linked to a private repository belonging to an organization. For this tutorial, I'm going to use as the name of the package and as the name of the organization.
Publishing the package
-
Create the file. Example:
.code
Important:
- The name of the package must have this structure: @organization/package-name |
- You have to set the URL of your repository |
- This attribute serves to tell GitHub that you will publish this package to its registry |
- Even if the package will be private, the value of this attribute must be false |
-
Create the workflow file in folder
.code
Important:
- This workflow executes when the branch is pushed. It is responsible to install and build the package using npm through the and steps
- Before execute the two previous steps, it is important to set to indicate to GitHub you will use the npm registry to build the package
- Before publish the package, it is necessary to set to indicate to GitHub you will publish the package to its packages registry
- To upload the package is required to set , it is a secret created by GitHub automatically in the project's repository. Also, you can use another authentication method like personal access tokens. In this link you can read more about this
Installing the published package in a project
-
Create a file on the root folder of the project that will use the published package
.code
In this link you can see how to create a personal access token
-
Add the package to the file dependencies
.code
-
Install the dependencies executing
Final words
Sometimes, these kind of tasks are hard, specially for no expert people in deployments, servers and infrastructure like me. I hope that this tutorial has been useful for you.
Sources
- Authentication in a workflow
docs.github.com
- Creating a personal access token
docs.github.com
- Configuring npm for use with GitHub Packages
docs.github.com
- Publishing Node.js packages
docs.github.com
- setup-node [#Publish to npmjs and GPR with npm]
github.com
- Publishing and Installing Private GitHub Packages using Yarn and Lerna
viewsource.io