Travis CI allows you to automate the test, build and publish of npm packages or any other type of project.
The great thing about Travis CI is that it’s FREE to use for open source projects.
On the end of this post you should be able to let Travis build and publish a npm package automatically after pushing
to the release
branch.
We are going to use that generated token and set it as environment variable inside the Travis dashboard. Go to the settings of your project inside Travis and search for the environment variables section.
'NPM_API_TOKEN'
to the generated token from npm'NPM_EMAIL'
to the email address which you registered on npm
By setting the NPM_API_TOKEN
and NPM_EMAIL
we can allow Travis to publish packages on your behalf.
In the root of your project you need a file called .travis.yml.
This file defines the build and deploy process.
Travis CI will automatically read that file on each commit.
We can define everything we need from bottom to top:
'deploy'
will run only on commits to the ‘release’ branch and will automatically replace 'NPM_API_TOKEN'
'NPM_EMAIL'
with the correct values from the environment variables which we defined earlier in the Travis CI dashboard.'before_deploy'
will only run on commits to the release
branch. In this case, the project will be build using npm run build
.'script'
will always run on every commit. In this case, unit tests and linting will run.Before pushing to the release
branch, be sure to update the version number of yourpackage.json
This can easily be done by using one of the following npm commands:
$ npm version patch
$ npm version minor
$ npm version major
patch: 1.0.0 -> 1.0.1
minor:
1.0.0 -> 1.1.0
major: 1.0.0 -> 2.0.0
By using the npm version command, npm will automatically create and commit a git tag for you.
When you push your code to the release
branch, Travis CI will automatically build, test and publish to npm for you!
As you can see it’s pretty easy to get started with Travis CI and creating a pipeline for building, testing and publishing npm packages.