➺  Mervin Villaceran

Bitbucket pipeline setup for yarn build

March 25, 2021 2 min read
Bitbucket Pipeline Yarn yml

This is a bitbucket pipeline setup to build your Javascript files for production or any environment using a specific branch.

  • Install dependencies
  • Run build
  • Push to branch

After that, you can now pull all the changes in your server.

#  Template NodeJS build

# This template allows you to validate your NodeJS code.
# The workflow allows running tests and code linting on the default branch.
image: node:14.15.0

pipelines:
branches:
'master':
- step:
name: Install, Build and Push to current branch
caches:
- nodecustom
- yarncustom
script:
- yarn install --frozen-lockfile
- npm run prod
- git add -f --all
- git commit -m "npm Build"
- git push
definitions:
caches:
nodecustom: ./node_modules
yarncustom: /usr/local/share/.cache/yarn

Build in a specific branch

You can specify any branch you want to build the project. If you want to build it in multiple branches, you can enclose a comma separated string with brackets.

'{master, dev, staging}'

Caching

Cache node_modules and yarn to reduce build time for future builds.

Install dependencies

Install dependencies using yarn.

--frozen-lockfile will not generate and update the yarn.lock file. It is used to make sure that you are using the same dependency version on every build.

Push build files to current branch

First is you need to add all the new/updated files created by yarn build (ex. dist/).
-f --all will help you to add files even if it is included in the .gitignore file.

Sometimes you don't want to include build files in your repository because it will always result in merge hell (lots of conflicts). That's why the better way is to add it in your gitignore file and force git to add all the updated files including the files that has been ignored.

After a successful build of the pipeline, you can now deploy your project manually or via git. You can also create a new pipeline step to automatically deploy your repo to the server.

Keep coding!