Let’s start with the idea of this article. We’re going to create a fully automated github repository and going to use github actions to automate the process of creating a new release and as well as publishing the new version of the project to npm.
Let’s start with the first step. We need to initialize your future repository.
npm init -y.
Your package.json will look like this:
The package.json file is the main file for your project. It’s the file that contains all the information about your project.
Let’s add some information to it. First of all, we need to add the name of your project. This is the name of your project which will be used in the npm package, so it’s important to choose a name that is unique, in my case I use dotenv-guards which is a library that I use to manage my environment variables and transform it into my own types.
The next step is to add the version of your project. I use the 1.0.0 version.
So we have the basic structure of the project. Now we need to write some code.
In my case I create a simple function that returns the transformed boolean value.
Don’t forget to add the unit tests. In my case I use Jest to test the code.
The common approach to test the code is to use AAA(arrange, act, assert) pattern.
Setup the CI
Before you start to push and define CI, you need to make sure that you have the following:
scripts in package.json, like build, test.
package.json exports your core modules, in my case package.json looks like this(see exports field):
Create a .github/workflows/main.yml file with following content:
Don’t forget to add the npm_token to the .github/workflows/main.yml file. It is a secret that you need to provide it to publish to npm.
Let’s run this workflow, you should get something like this:
You also can define separate workflow and run some check when PR is opened. In this case you need to create new workflow and update on strategy to pr, and don’t forget to remove push strategy, and releasing to npm.
Conclusion
This article is a simple guide to create a fully automated github repository and automate the process of creating a new release and publishing the new version of the project to npm. I hope that you will find this article useful. I hope you will like this article. If you have any questions, please don’t hesitate to contact me.
This is a real package and the name is dotenv-guards