Cơ Bản Pipeline
Cách Hoạt Động
bitbucket-pipelines.ymlFile cấu hình ở thư mục gốc repo
Docker containersMỗi step chạy trong container riêng
TriggersPush, PR, tag, lịch trình, hoặc thủ công
Build minutesHạn ngạch phụ thuộc vào gói dịch vụ
Bật Pipelines
# Repository Settings → Pipelines → Enable # Thêm bitbucket-pipelines.yml vào thư mục gốc # Push đầu tiên sẽ kích hoạt pipeline
bitbucket-pipelines.yml
Cấu Hình Tối Giản
image: node:20 pipelines: default: - step: script: - npm install - npm test
Pipeline Theo Branch
pipelines: branches: main: - step: script: - npm run build - npm run deploy
Pipeline Theo Tag & Pull Request
pipelines: tags: 'v*': - step: script: - npm run release pull-requests: '**': - step: script: - npm test
Steps
Tùy Chọn Step
nameTên hiển thị của step
imageGhi đè Docker image toàn cục
scriptDanh sách lệnh shell cần chạy
size1x (4GB) hoặc 2x (8GB) bộ nhớ
max-timeTimeout tính bằng phút (mặc định 120)
triggermanual cho step chỉ chạy thủ công
Step Song Song
- parallel: - step: name: "Lint" script: - npm run lint - step: name: "Test" script: - npm test
Step Thủ Công
- step: name: "Deploy to Production" trigger: manual script: - ./deploy.sh prod
Biến
Các Loại Biến
Repository variablesSettings → Pipelines → Variables
Deployment variablesGiới hạn trong môi trường deployment
Secured variablesMã hóa, ẩn trong log
Pipeline variablesĐịnh nghĩa trực tiếp trong YAML
Sử Dụng Biến
pipelines: default: - step: script: - echo $MY_VAR - docker login -u $DOCKER_USER -p $DOCKER_PASS
Biến Built-in
$BITBUCKET_COMMITSHA commit đầy đủ
$BITBUCKET_BRANCHTên branch
$BITBUCKET_TAGTên tag (pipeline theo tag)
$BITBUCKET_BUILD_NUMBERSố build tăng dần
$BITBUCKET_REPO_SLUGSlug của repository
Caching
Cache Có Sẵn
- step: caches: - node # ~/.npm - pip # ~/.cache/pip - docker # Docker layer cache script: - npm install - npm test
Cache Tùy Chỉnh
definitions: caches: gradle: ~/.gradle/caches mylibs: vendor/libs pipelines: default: - step: caches: - gradle script: - ./gradlew build
Hành Vi Cache
Thời hạnCache hết hạn sau 7 ngày
Phạm viChia sẻ trên tất cả pipeline trong repo
XóaPipelines → Caches → Delete
Artifacts
Truyền File Giữa Các Step
- step: name: "Build" script: - npm run build artifacts: - dist/** - step: name: "Deploy" script: - ls dist/ # artifacts có sẵn - ./deploy.sh
Tùy Chọn Artifact
artifactsGlob pattern cho file cần truyền
DownloadTự động có sẵn ở các step tiếp theo
Max size1 GB mỗi step
RetentionCó sẵn trong 14 ngày sau khi build
Deployments
Môi Trường Deployment
- step: name: "Deploy Staging" deployment: staging script: - ./deploy.sh staging - step: name: "Deploy Production" deployment: production trigger: manual script: - ./deploy.sh prod
Loại Môi Trường
testMôi trường kiểm thử
stagingMôi trường tiền production
productionMôi trường live, theo dõi trên dashboard
Các Pattern Thường Gặp
Docker Build & Push
- step: services: - docker script: - docker build -t myapp:$BITBUCKET_COMMIT . - docker login -u $DOCKER_USER -p $DOCKER_PASS - docker push myapp:$BITBUCKET_COMMIT
Service Containers
definitions: services: postgres: image: postgres:16 variables: POSTGRES_DB: testdb POSTGRES_PASSWORD: secret pipelines: default: - step: services: - postgres script: - npm test
Step Điều Kiện với Pipe
- step: name: "Deploy to S3" script: - pipe: atlassian/aws-s3-deploy:1.1.0 variables: AWS_ACCESS_KEY_ID: $AWS_KEY AWS_SECRET_ACCESS_KEY: $AWS_SECRET S3_BUCKET: my-bucket LOCAL_PATH: dist/