Publishing npm Packages From TeamCity

Written by Ken Dale

Releasing new versions from continuous integration, rather than someone’s machine in a team environment is a great way to release new versions of npm packages in a predicable and controlled manner.

Here’s a quick runthrough for publishing an npm package from TeamCity:

First, we need a TeamCity build

We’ll need the build itself setup on TeamCity. For us, we’re currently following a build.cmd philosophy for our projects. An example build.cmd looks like this:

@echo Off
pushd %~dp0

call npm install
if %ERRORLEVEL% neq 0 goto BuildFail

call npm run build
if %ERRORLEVEL% neq 0 goto BuildFail

call npm run validate
if %ERRORLEVEL% neq 0 goto BuildFail

call npm pack
if %ERRORLEVEL% neq 0 goto BuildFail

goto BuildSuccess

echo *** BUILD FAILED ***
goto End

echo *** BUILD SUCCEEDED ***
goto End


Note the usage of npm pack. This generates the necessary .tgz artifact for the package that we can publish to npm.

Simply setup TeamCity to run your build.cmd, or however you want to setup your build.

The important point is ensuring the build artifacts include the .tgz file.

Build script: Artifact paths{: .img-fluid}

Publishing the npm pack artifact

Now that there’s a working build generating the necessary artifact, we can create a build step to publish the package. This relies on the build script for the .tgz artifact.

Here’s a runthrough of the various options to configure:

  • Create a new TeamCity build configuration, name it “Publish to npm” or similar.
  • For the build step: Use the following script (change ritter-jekyll to the name of your npm package):
@echo Off

call npm config set "//" "%npm_auth_token%"

echo Before publish...
for /f %%%%i in ('dir ritter-jekyll-*.tgz /b') do set PACKAGE=%%%%i
call echo Publishing %%PACKAGE%% ...
call npm publish %%PACKAGE%%

call npm config delete "//"
  • Triggers: Create a “Finish Build Trigger”, triggering on successful build only, with the branch filter:
  • Inspect “Failure Conditions”, you may want to set “an error message is logged by build runner” if it is not already.
  • Dependencies: Setup both a snapshot and artifact dependency:

Snapshot dependency{: .img-fluid}

Artifact dependency{: .img-fluid}

Now, deploy from master!

Now, you’re ready to deploy the latest version of your package from the master branch. And, subsequent version updates will be automatically published by TeamCity as they reach master.

Published August 26, 2016 by

undefined avatar
Ken Dale Senior Application Developer (Former)

Suggested Reading