DEV Community

Daniel Bayerlein
Daniel Bayerlein

Posted on • Updated on

NPM Preview Releases with Bitbucket Pipelines

If you maintain node packages, it is useful to provide pre-releases for testing purposes. The maintainers of React, have an interesting solution for this:

Current Tags

16.13.0                         latest
0.0.0-d28bd2994                 next
0.0.0-experimental-d28bd2994    experimental
0.0.0-57333ca33                 canary
Enter fullscreen mode Exit fullscreen mode

The pre-release versions are created as version 0.0.0, followed by the commit hash. I like this approach, so I implemented it in Bitbucket Pipelines.

Bitbucket Pipelines

File: bitbucket-pipelines.yml

  branches:
    master:
      - step:
          name: Test and Build
          caches:
            - node
          script:
            - yarn install
            - yarn lint
            - yarn test
            - yarn build
          artifacts:
            - dist/**
      - step:
          name: Publish package to npm
          caches:
            - node
          script:
            - sed -i 's/\"version\":.*/\"version\":\ "'0.0.0-$BITBUCKET_COMMIT'",/g' package.json
            - pipe: atlassian/npm-publish:0.2.6
              variables:
                NPM_TOKEN: $NPM_TOKEN
            - echo "0.0.0-$BITBUCKET_COMMIT"
Enter fullscreen mode Exit fullscreen mode

In the first pipeline step the tests are performed and the package is build. The second step is to replace the version in the package.json and release the npm package. The version is replaced with 0.0.0, followed by $BITBUCKET_COMMIT. This variable is provided by Bitbucket and contains the commit hash of the commit that started the build.

Bitbucket already provides a Docker image (atlassian/npm-publish) for the publishing of npm packages.

It's necessary to create a secure environment variable called $NPM_TOKEN. If you are not familiar with it, you can find the documentation here.

🎉 Now every commit on the master branch will create an npm pre-release. The version can be found in the pipeline log:

+ echo "0.0.0-$BITBUCKET_COMMIT"
0.0.0-ae61b0bc5a03b0189a0bf2683b9e1f24f35d68c6
Enter fullscreen mode Exit fullscreen mode

Top comments (0)