image: node:16

definitions:
  steps:
    - step: &build
        name: Build
        script:
          - npm install
          - npm run build
          - git add dist/ -f
          # only commit if there are changes
          - git diff --staged --quiet || git commit -m "chore:\ pipeline build ${BITBUCKET_BUILD_NUMBER} [skip ci]"
          - git push
        artifacts:
          - '**'  # copy the entire build directory to subsequent steps

    - step: &test
        name: Test
        script:
          - npm test

    - step: &publish
        name: Publish
        deployment: npm
        script:
          - npx semantic-release

pipelines:
  default:
    - step: *build
    - step: *test
  branches:
    beta:
      - step: *build
      - step: *test
      - step: *publish

    master:
      - step: *build
      - step: *test
      - step: *publish

    next:
      - step: *build
      - step: *test
      - step: *publish
