DEV Community

Cover image for Release on NPM using Github Actions

Posted on

Release on NPM using Github Actions

Good morning everyone and happy MonDEV! ☕
How was your week? Have you tried creating your own npx command? If you have, today's newsletter will help streamline the release process; if you haven't, it might still be useful in the future or offer inspiration for other projects that could benefit from a GitHub passage to implement CI/CD.

Today we talk about GitHub Actions and how to automate the release of new versions of your package! Did you know that, whether for the less experienced, those wanting to learn, or simply to save time, GitHub has a rich list of preconfigured actions to perform releases on the most well-known package managers? NPM is, of course, a leader in this list!

Let's see how to release a package in a few simple steps! First of all, we'll need a token issued by npm: to get it, log in to your account, click on the profile picture in the top right, then on Access Tokens, and then on Generate New Token > Classic Token; after confirming using the password, name your token and select Publish as the type; copy your token and set it aside.

Now, going into your repo, on the Action panel, it will suggest workflows to start from; searching for npm, you will find "publish npm package". A new file will be created with a working workflow that by default will release your package every time you create a new release. If you don't use tests (oh no!), remove the line with run: npm test... Then hurry to write tests and reintegrate it ;) In the last line, you'll notice a value in curly braces; that represents your npm token. Save the file and in the repo's settings, under secrets and variables > Actions, add a new secret with the key npm_token.

You're done! Now, every time you create a release, your repo will be directly distributed on npm! Of course, it's also possible to modify the trigger and activate it on every push or pull request to a specific branch, it's all up to your preferences!

I hope you have fun with GitHub Actions and that, regardless of the project you want to release, you find some interesting insights!
Have a great start of the week and happy coding 0_1

Top comments (0)