This guide will help you understand how to configure your bitbucket-pipelines.yml file to build and test a Node.js project.
Specify your Node.js version with Docker
Bitbucket Pipelines runs all your builds in Docker containers using an image that you provide at the beginning of your configuration file. You can easily use Node.js with Bitbucket Pipelines by using one of the official Node.js Docker images on Docker Hub.
For instance, you can use Node.js 6.9.4 by specifying it at the beginning of your bitbucket-pipelines.yml file.
If you wanted to use a different version of Node.js you simply need to change the tag of the Node.js Docker image. The example below would start a container with Node 4.7.2.
To find a list of all supported Node.js versions and corresponding image tags, please refer to https://hub.docker.com/_/node/.
Managing dependencies with npm
The default Node.js image on Docker Hub comes with npm installed by default. You can use it directly in your script to install specific packages locally or globally.
If you are using a package.json file, you can simply run npm install at the beginning of your script to install all the required dependencies.
Managing dependencies with Yarn
You will have to install Yarn as part of your build script to be able to use it. You can simply copy and paste the instructions in the example below to
Installing dependencies globally (Gulp, Grunt, Angular CLI...)
Node packages can also be installed globally. For instance, if your pipeline depends on a task runner like Gulp or Grunt, you will need to install them globally. You can use the npm install -g command if you're using npm as your package manager.
or yarn global add if you're using Yarn.
Download and publishing modules to a private registry
If you are using private modules from either the public npm registry or your own private registry you can simply check in a .npmrc file at the root of your repository with the using environment variables. In order to avoid conflict with the local ~/.npmrc file during development you can simply name it .npmrc_config and rename it as part of the pipeline script.
If you are using a private npm registry you just need to replace
registry.npmjs.org with the address of your registry.
Then configure the following environment variable in Bitbucket Pipelines:
- NPM_TOKEN: This is the authentication token to your registry. You can find it in your local ~/.npmrc, after you login to your registry.
Your bitbucket-pipelines.yml script will then look like the following to install your private dependencies.
It is not yet possible to use multiple containers in a running pipeline, but you can use or create a Docker image that contains both Node.js and the required database. You will find below some examples of images that you can re-use or adapt to meet your requirements.
You simply need to add to your bitbucket-pipelines.yml file the same commands that you would run locally to test your application. For instance, if you are running npm test locally to run your test suite the following example should work for you.
Was this helpful?
Thanks for your feedback!