This guide shows you how to use Bitbucket Pipelines for building and testing a Node.js software project in a Docker container.
Most of the configuration happens in the
bitbucket-pipelines.yml file that Pipelines uses to define the build.
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.
image: node:6.9.4 pipelines: default: - step: script: - node -v
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/.
You can check your bitbucket-pipelines.yml file with our online validator.
Manage 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:
image: node:6.9.4 pipelines: default: - step: script: - npm install
Manage dependencies with Yarn
The default Node.js image on Docker Hub comes with yarn installed by default. You can use it directly in your script to install specific packages locally or globally.
image: node:8 pipelines: default: - step: script: - yarn
Install 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:
npm install -g grunt-cli npm install grunt
yarn global add if you're using Yarn.
yarn global add grunt-cli yarn grunt
Download and publish 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 using repository variables. 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 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.
image: node:6.9.4 pipelines: default: - step: script: - mv .npmrc_config .npmrc - npm install
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.
image: node:6.9.4 pipelines: default: - step: script: - npm install - npm test
Use a service container to run a database in your pipeline.
Remember, you can check your bitbucket-pipelines.yml file with our online validator.