Mastering CI/CD: Structuring Build, Test, and Deploy Steps in Bitbucket Pipelines
Learn how to structure a robust bitbucket-pipelines.yml file to automate your build process, run comprehensive tests, and manage deployments across environments.
4 min read
Since I started building web applications in 2021, I have spent a significant amount of time refining my CI/CD workflows. While there are many tools available, I often find myself returning to Bitbucket Pipelines for its seamless integration with the repository and its straightforward, YAML-based configuration. It allows me to define my entire delivery process—from the first linting check to the final production deployment—directly within my codebase. To create a reliable pipeline, I focus on three distinct phases: build, test, and deploy.
The Build Phase: Preparing the Environment
The build phase is where I define the environment and prepare the application for execution. In my experience, using a specific Docker image is the best way to ensure consistency across different stages. By default, Bitbucket uses a standard image, but I prefer to specify one that matches my local development environment, such as a specific Node.js or Python version. This prevents the "it works on my machine" syndrome.
During this stage, I focus on installing dependencies and generating artifacts. One of the most critical aspects of a fast build step is caching. Bitbucket provides built-in support for caching popular package managers like npm, yarn, or pip. By caching the node_modules directory, for instance, I can shave minutes off every subsequent run. Here is how I usually structure the initial build step in my bitbucket-pipelines.yml:
image: node:18
pipelines:
default:
- step:
name: Build and Package
caches:
- node
script:
- npm install
- npm run build
artifacts:
- dist/**The artifacts definition is crucial. It instructs Bitbucket to save the files generated during the build step—like a compiled React bundle or a Python wheel—so they can be passed to the next steps in the pipeline. Without artifacts, each step starts with a fresh clone of the repository, losing any work done in previous stages.
The Test Phase: Ensuring Quality and Stability
Once the build is successful, the next logical step is testing. I treat this phase as the primary gatekeeper for my main branch. If a single test fails, the pipeline stops, preventing broken code from ever reaching a server. I typically break this phase into several logical components: linting, unit testing, and sometimes security scanning.
I prefer running linting first because it is usually the fastest check. If my TypeScript or Python code doesn't adhere to formatting standards or has obvious syntax errors, there is no point in waiting for a full suite of unit tests to complete. I often use tools like ESLint or Prettier for my JavaScript projects and Flake8 or Black for my Django applications. After linting, I move on to the unit tests, ensuring that my logic remains sound after new changes.
To keep the pipeline efficient, I sometimes run these tests in parallel. Bitbucket allows for parallel steps, which can significantly reduce the total execution time if you have a large test suite. However, for most of my projects, a sequential approach within a single step is sufficient for maintaining clarity and simplicity. I always ensure that I output test reports in a format Bitbucket understands, such as JUnit XML, so I can view the results directly in the Bitbucket UI.
The Deploy Phase: From Code to Production
The final and most rewarding phase is the deployment. This is where the artifacts created in the build phase are pushed to their final destination, whether that is an AWS S3 bucket, a Docker registry, or an EC3 instance. I utilize environment-specific deployments—typically staging and production—to ensure I can test changes in a live-like environment before going public.
For AWS deployments, I rely heavily on Bitbucket Pipes. Pipes are pre-configured scripts provided by Bitbucket or third parties that simplify common tasks. For example, using the aws-s3-deploy pipe allows me to sync my build artifacts to an S3 bucket with just a few lines of configuration. This removes the need to manually install the AWS CLI and manage complex authentication scripts within my YAML file.
I also implement manual triggers for production deployments. While staging can be automated to deploy every time a change is merged into the develop branch, I prefer to have a human verify the staging site before clicking the "deploy" button for the production environment. This provides an extra layer of safety. Here is an example of how I define a deployment step using a pipe and environment variables:
- step:
name: Deploy to Production
deployment: production
trigger: manual
script:
- pipe: atlassian/aws-s3-deploy:1.1.0
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: "us-east-1"
S3_BUCKET: "my-production-app-bucket"
LOCAL_PATH: "dist"Using the deployment keyword is essential because it allows me to track which version of my code is currently running in each environment through the Bitbucket dashboard. It also allows me to restrict deployment permissions to specific team members if necessary. By combining clear build, test, and deploy steps, I create a predictable path for my code, allowing me to focus more on developing features and less on the mechanics of shipping them.
Have a project in mind?
I build and run web apps end to end.