Archive · DevOps

Bitbucket Pipelines: Build and Push an Image

A bitbucket-pipelines.yml that tests every pull request and builds a Docker image on main.

1 min read

  • Bitbucket Pipelines
  • Docker 23.0
  • Node.js 18

Pipelines is Bitbucket’s built-in CI. Add bitbucket-pipelines.yml to the repository root:

image: node:18

pipelines:
  pull-requests:
    '**':
      - step:
          name: Test
          caches: [node]
          script:
            - npm ci
            - npm test
  branches:
    main:
      - step:
          name: Test
          caches: [node]
          script: [npm ci, npm test]
      - step:
          name: Build and push
          services: [docker]
          script:
            - docker build -t $REGISTRY/shop-api:$BITBUCKET_COMMIT .
            - echo $REGISTRY_PASSWORD | docker login -u $REGISTRY_USER --password-stdin $REGISTRY
            - docker push $REGISTRY/shop-api:$BITBUCKET_COMMIT

Set REGISTRY, REGISTRY_USER and REGISTRY_PASSWORD as secured repository variables, not in the file. Tagging with $BITBUCKET_COMMIT makes each image traceable to a commit.

The Free plan includes a limited number of build minutes each month, so use caches, fail fast, and add condition: changesets to skip steps when unrelated files change. Add a manual trigger: manual step for production deploys.