How to automatically publish new version of your package on NPM after it passes automated unit tests.
Use semantic-release - this tool works beautifully.
npm install -g semantic-release-cli
Answer simple questions and it will setup Travis CI build that will publish to NPM a new version of your package if there are semantic changes it all tests pass. It will also create and push Git tag to GitHub, creating perfect release notes. Here is an example snap-shot/releases.
I also make sure small releases do not break projects that already depend on published version using dont-crack plugin.
For projects hosted on GitLab there is semantic-release-gitlab.
Using TravisCI - does NOT work
- Install travis command line tool
sudo gem install travis -v 1.7.7 --no-rdoc --no-ri(for details see installation).
- Check the installation
- Switch to the package folder
- Make sure there is
.travis.ymlwith build configured.
- Look up your NPM email and password if you do not remember
- Encode NPM email and password
echo -n "username:password" | base64. Copy the produced string - this is your api key.
travis setup npmand answer a couple of questions. I advise to only release tagged commits.
Due to a travis bug there are problems detecting the correct tagged commit and triggering the
deploy to npmstep. These are the settings that I am trying to make it work:
nodeto one of the multiple possible versions
build only on a single node version and deploy from that version
repo: <github username>/<repo name>
Next time you commit and tag, when pushing to the remote, the travis should build, run unit tests, and if they pass, and there is a new tag, it should publish to the NPM registry.
More details / instructions at docs.travis-ci.com
After spending way too much time on this, and being able to successfully publish to NPM only to have version clashes, trying to securely encode variables using Travis, but having publish not being able to read them, here is my verdict: the Travis to NPM pipeline is broken really badly. What is wrong with these two organizations? Just Google "travis npm publish bug" and see the travis repo discussions - they have been going on for ages! For now I will manually publish my modules.
Using CircleCI - works
After I gave up on TravisCI, I tried one of my other standby continuous build services - CircleCI. They allowed me to securely store my NPM personal credentials as environment variables without storing them with the source.
- I set my NPM username (bahmutov), email and password in 3 separate variables: NPM_USERNAME, NPM_EMAIL and NPM_PASSWORD
- I wrote the following
circle.ymlfile with two deployment commands
- I am using the trick
echo -e "line 1\nline 2\nline 3"that pipes three separate lines into
npm logincommand to provide my credentials and login to the NPM.
- After I logged in, I set up command
npm run 2npmto run the NPM script with single tool
I am using the publish module to actually publish the
module to the registry:
npm install --save-dev publish.
This module first checks if the current package version has not been published yet.
If not, then it publishes. The publish thus works after each successful build on CircleCi, but will only
publish new version if I change the module's version string.
You can see the build history for bahmutov/quickly.