---
id: cicd-bitbucket
title: "Bitbucket Pipelines"
shortTitle: "Bitbucket Pipelines"
track: cicd
trackTitle: "CI/CD"
order: 9
level: normal
status: published
estimatedMinutes: 25
prerequisites:
  - cicd-teamcity
next: iac-terraform
reviewedAgainst: 2026-10-12
sources:
  - title: "Get started with Bitbucket Pipelines"
    url: https://support.atlassian.com/bitbucket-cloud/docs/get-started-with-bitbucket-pipelines/
  - title: "Bitbucket Pipelines configuration reference"
    url: https://support.atlassian.com/bitbucket-cloud/docs/bitbucket-pipelines-configuration-reference/
---

# Bitbucket Pipelines

Bitbucket Pipelines đọc `bitbucket-pipelines.yml` ở gốc repo. Mỗi step là một container. Bài này để đọc file khi source đang nằm trên Bitbucket.

## Để làm gì

Bạn viết một step chạy Node, và biết pipe là bước đóng gói, không phải shell của bạn.

## Gồm những gì

- `pipelines.default` và `branches`.
- `image` đặt ở đầu file hoặc trong từng step.
- Pipe: nhận mặt bước đóng gói, chưa cần dùng.

Đừng bật Pipelines trên workspace trả phí cho bài này.

## Dùng khi nào

Remote là Bitbucket và đã bật Pipelines. Không bật thêm nếu GitHub Actions đã build cùng repo mirror. Hai nơi cùng deploy là hai lần ghi lên production.

## Kiến thức cốt lõi

Khóa gốc `pipelines` chứa `default`, chạy cho mọi nhánh không có mục riêng, và `branches`, ghi đè theo tên nhánh. `pull-requests` chạy khi có pull request.

Mỗi step có `script`, list lệnh. `image` chọn container. Step sau không giữ file của step trước trừ khi bạn khai `artifacts`.

Pipe là bước đóng gói, lấy từ repo của Atlassian hoặc của người khác, dạng `pipe: atlassian/aws-s3-deploy:1.1.0`. Pin version. Pipe nhận biến. Đừng đưa key vào YAML. Dùng secured variable của repository.

## Lệnh và code mẫu

`bitbucket-pipelines.yml`:

```yaml
image: node:24
pipelines:
  default:
    - step:
        name: test
        script:
          - node -e "console.log('ok')"
  branches:
    main:
      - step:
          name: test
          script:
            - node -e "console.log('ok')"
```

## Thực hành

1. Chỉ step nào chạy khi push nhánh `feature`.
2. Chỉ step nào chạy khi push `main`.
3. Không bật Pipelines và không tạo secured variable trên workspace thật.

## Kết quả mong đợi

Nhánh `feature` dùng `default`. Nhánh `main` dùng mục trong `branches.main`, không chạy thêm `default` cho cùng một push.

## Checklist

- [ ] Secured variable cho token, không ghi trong `script`.
- [ ] Pipe có số version.
- [ ] Artifact được khai nếu step sau cần file build.

## Lỗi thường gặp

**File tên `bitbucket-pipeline.yml`, thiếu chữ s.** Bitbucket bỏ qua. Đúng là `bitbucket-pipelines.yml`.

**Tưởng step sau còn `node_modules`.** Step mới, container mới. Khai artifact hoặc cài lại trong step đó.

## Nguồn

1. [Get started](https://support.atlassian.com/bitbucket-cloud/docs/get-started-with-bitbucket-pipelines/) — bật và file YAML.
2. [Configuration reference](https://support.atlassian.com/bitbucket-cloud/docs/bitbucket-pipelines-configuration-reference/) — `default`, `branches`, `step`.

## Tiếp theo

[Terraform trên Azure](./02-terraform.md)
