Get started with Bitbucket Cloud
New to Bitbucket Cloud? Check out our get started guides for new users.
This guide shows you how to use Bitbucket Pipelines for building and testing a Node.js software project in a Docker container.
If you'd prefer to quickly import a demo repository with a working pipeline to experiment with, have a look at our demo node repo.
If you'd like to set it up by hand, most of the configuration happens in the bitbucket-pipelines.yml file that Pipelines uses to define the build.
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 10.15.0 by specifying it at the beginning of your bitbucket-pipelines.yml file.
1
2
3
4
5
6
image: node:10.15.0
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:
1
image: 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.
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:
1
2
3
4
5
6
image: node:10.15.0
pipelines:
default:
- step:
script:
- npm install
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.
1
2
3
4
5
6
image: node:10.15.0
pipelines:
default:
- step:
script:
- yarn
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:
1
2
3
npm install -g grunt-cli
npm install
grunt
or use yarn global add if you're using Yarn.
1
2
3
yarn global add grunt-cli
yarn
grunt
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.
.npmrc_config
1
2
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
registry=https://registry.npmjs.org
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.
bitbucket-pipelines.yml
1
2
3
4
5
6
7
image: node:10.15.0
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.
1
2
3
4
5
6
7
image: node:10.15.0
pipelines:
default:
- step:
script:
- npm install
- npm test
Use a service container to run a database in your pipeline.
Now you've built and tested your project you might want to deploy it somewhere. Have a look at our deployment guides for your next steps.
Was this helpful?