How to automatically publish new version of your package on NPM after it passes automated unit tests.
Update
Use semantic-release - this tool works beautifully.
1 | 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.
Finally, for simple publishing only if there is a version change (determined either manually or using next-ver) I have ci-publish
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
travis version
Switch to the package folder
Make sure there is
.travis.yml
with 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.Run
travis setup npm
and 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 npm
step. These are the settings that I am trying to make it work:- set
node
to one of the multiple possible versions - set
all_branches
to true - build only on a single node version and deploy from that version
1
2
3
4
5
6
7
8
9
10
11
12
13
14language: node_js
node_js:
- '0.12'
deploy:
provider: npm
email: <email>
api_key:
secure: <key>
clean_up: false
on:
tags: true
all_branches: true
node: '0.12'
repo: <github username>/<repo name>- set
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
Update
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.yml
file with two deployment commands
1 | deployment: |
- I am using the trick
echo -e "line 1\nline 2\nline 3"
that pipes three separate lines intonpm login
command to provide my credentials and login to the NPM. - After I logged in, I set up command
npm run 2npm
to run the NPM script with single tool
1 | "scripts": { |
I am using the publish module to actually publish the quickly
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.