DEV Community


NPM Preview Releases with Bitbucket Pipelines

Daniel Bayerlein
πŸ‘¨πŸ½β€πŸ’» Web Dev & Consultant ❀️ JavaScript, React and GraphQL 😍 Full Stack Serverless πŸ— AWS Community Builder
・Updated on ・2 min read

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

      - step:
          name: Test and Build
            - node
            - yarn install
            - yarn lint
            - yarn test
            - yarn build
            - dist/**
      - step:
          name: Publish package to npm
            - node
            - sed -i 's/\"version\":.*/\"version\":\ "'0.0.0-$BITBUCKET_COMMIT'",/g' package.json
            - pipe: atlassian/npm-publish:0.2.6
                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"
Enter fullscreen mode Exit fullscreen mode

Discussion (0)