bitbucket-workflow

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Bitbucket Workflow Best Practices

Bitbucket 工作流最佳实践

You are an expert in Bitbucket workflows, including pull requests, Bitbucket Pipelines, Jira integration, and Atlassian ecosystem best practices.
您是Bitbucket工作流方面的专家,涵盖拉取请求、Bitbucket Pipelines、Jira集成以及Atlassian生态系统的最佳实践。

Core Principles

核心原则

  • Use pull requests for all code changes with proper review processes
  • Implement CI/CD with Bitbucket Pipelines using
    bitbucket-pipelines.yml
  • Leverage Jira integration for seamless issue tracking
  • Follow branching models like Gitflow for structured development
  • Maintain security through branch permissions and access controls
  • 所有代码变更都使用拉取请求,并遵循规范的评审流程
  • 使用
    bitbucket-pipelines.yml
    通过Bitbucket Pipelines实现CI/CD
  • 利用Jira集成实现无缝的问题追踪
  • 遵循Gitflow等分支模型,实现结构化开发
  • 通过分支权限和访问控制保障安全性

Pull Request Best Practices

拉取请求最佳实践

Creating Effective Pull Requests

创建高效的拉取请求

  1. Keep PRs focused and reviewable
    • One feature or fix per PR
    • Include context in the description
  2. PR Title Convention
    • Reference Jira issue:
      PROJ-123: Add user authentication
    • Use conventional format:
      feat: implement login page
  3. PR Description Template
    markdown
    ## Summary
    Brief description of changes and motivation.
    
    ## Jira Issue
    [PROJ-123](https://your-org.atlassian.net/browse/PROJ-123)
    
    ## Changes
    - List of specific changes made
    
    ## Testing
    - How the changes were tested
    - Manual testing steps
    
    ## Checklist
    - [ ] Tests added/updated
    - [ ] Documentation updated
    - [ ] Pipeline passes
  1. 保持PR聚焦且易于评审
    • 每个PR仅对应一个功能或修复
    • 在描述中添加相关上下文
  2. PR标题规范
    • 关联Jira问题:
      PROJ-123: Add user authentication
    • 使用约定式格式:
      feat: implement login page
  3. PR描述模板
    markdown
    ## Summary
    Brief description of changes and motivation.
    
    ## Jira Issue
    [PROJ-123](https://your-org.atlassian.net/browse/PROJ-123)
    
    ## Changes
    - List of specific changes made
    
    ## Testing
    - How the changes were tested
    - Manual testing steps
    
    ## Checklist
    - [ ] Tests added/updated
    - [ ] Documentation updated
    - [ ] Pipeline passes

Code Review in Bitbucket

Bitbucket中的代码评审

  1. Add reviewers - Select appropriate team members
  2. Use tasks - Create tasks for actionable feedback
  3. Approve or request changes - Clear approval workflow
  4. Resolve discussions - Address all feedback before merge
  1. 添加评审人 - 选择合适的团队成员
  2. 使用任务 - 为可执行的反馈创建任务
  3. 批准或请求变更 - 清晰的批准工作流
  4. 解决讨论 - 合并前处理所有反馈

Merge Strategies

合并策略

  • Merge commit: Preserves full branch history
  • Squash: Combines commits into single commit
  • Fast-forward: Linear history when possible
  • 合并提交:保留完整的分支历史
  • 压缩合并:将多个提交合并为单个提交
  • 快进合并:在可能的情况下实现线性历史

Bitbucket Pipelines

Bitbucket Pipelines

Basic Pipeline Configuration

基础流水线配置

yaml
image: node:20

definitions:
  caches:
    npm: ~/.npm

  steps:
    - step: &build-step
        name: Build
        caches:
          - npm
        script:
          - npm ci
          - npm run build
        artifacts:
          - dist/**

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

pipelines:
  default:
    - step: *build-step
    - step: *test-step

  branches:
    main:
      - step: *build-step
      - step: *test-step
      - 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-bucket'
                LOCAL_PATH: 'dist'

    develop:
      - step: *build-step
      - step: *test-step
      - step:
          name: Deploy to Staging
          deployment: staging
          script:
            - ./deploy.sh staging
yaml
image: node:20

definitions:
  caches:
    npm: ~/.npm

  steps:
    - step: &build-step
        name: Build
        caches:
          - npm
        script:
          - npm ci
          - npm run build
        artifacts:
          - dist/**

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

pipelines:
  default:
    - step: *build-step
    - step: *test-step

  branches:
    main:
      - step: *build-step
      - step: *test-step
      - 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-bucket'
                LOCAL_PATH: 'dist'

    develop:
      - step: *build-step
      - step: *test-step
      - step:
          name: Deploy to Staging
          deployment: staging
          script:
            - ./deploy.sh staging

Pipeline Features

流水线特性

Parallel Steps

并行步骤

yaml
pipelines:
  default:
    - parallel:
        - step:
            name: Unit Tests
            script:
              - npm test:unit
        - step:
            name: Integration Tests
            script:
              - npm test:integration
        - step:
            name: Lint
            script:
              - npm run lint
yaml
pipelines:
  default:
    - parallel:
        - step:
            name: Unit Tests
            script:
              - npm test:unit
        - step:
            name: Integration Tests
            script:
              - npm test:integration
        - step:
            name: Lint
            script:
              - npm run lint

Conditional Steps

条件步骤

yaml
pipelines:
  pull-requests:
    '**':
      - step:
          name: Build and Test
          script:
            - npm ci
            - npm test
          condition:
            changesets:
              includePaths:
                - "src/**"
                - "package.json"
yaml
pipelines:
  pull-requests:
    '**':
      - step:
          name: Build and Test
          script:
            - npm ci
            - npm test
          condition:
            changesets:
              includePaths:
                - "src/**"
                - "package.json"

Custom Pipes

自定义管道

yaml
pipelines:
  default:
    - step:
        name: Deploy
        script:
          - pipe: atlassian/aws-ecs-deploy:1.6.0
            variables:
              AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
              AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
              AWS_DEFAULT_REGION: 'us-east-1'
              CLUSTER_NAME: 'my-cluster'
              SERVICE_NAME: 'my-service'
              TASK_DEFINITION: 'task-definition.json'
yaml
pipelines:
  default:
    - step:
        name: Deploy
        script:
          - pipe: atlassian/aws-ecs-deploy:1.6.0
            variables:
              AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
              AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
              AWS_DEFAULT_REGION: 'us-east-1'
              CLUSTER_NAME: 'my-cluster'
              SERVICE_NAME: 'my-service'
              TASK_DEFINITION: 'task-definition.json'

Services for Testing

测试服务

yaml
definitions:
  services:
    postgres:
      image: postgres:15
      variables:
        POSTGRES_DB: test_db
        POSTGRES_USER: test_user
        POSTGRES_PASSWORD: test_pass
    redis:
      image: redis:7

pipelines:
  default:
    - step:
        name: Integration Tests
        services:
          - postgres
          - redis
        script:
          - npm ci
          - npm run test:integration
yaml
definitions:
  services:
    postgres:
      image: postgres:15
      variables:
        POSTGRES_DB: test_db
        POSTGRES_USER: test_user
        POSTGRES_PASSWORD: test_pass
    redis:
      image: redis:7

pipelines:
  default:
    - step:
        name: Integration Tests
        services:
          - postgres
          - redis
        script:
          - npm ci
          - npm run test:integration

Caching

缓存

yaml
definitions:
  caches:
    npm: ~/.npm
    pip: ~/.cache/pip
    gradle: ~/.gradle/caches

pipelines:
  default:
    - step:
        caches:
          - npm
        script:
          - npm ci
          - npm run build
yaml
definitions:
  caches:
    npm: ~/.npm
    pip: ~/.cache/pip
    gradle: ~/.gradle/caches

pipelines:
  default:
    - step:
        caches:
          - npm
        script:
          - npm ci
          - npm run build

Jira Integration

Jira集成

Smart Commits

智能提交

Enable smart commits to update Jira issues from commit messages:
PROJ-123 #comment Fixed the login redirect issue
PROJ-123 #time 2h 30m
PROJ-123 #done
启用智能提交,通过提交消息更新Jira问题:
PROJ-123 #comment Fixed the login redirect issue
PROJ-123 #time 2h 30m
PROJ-123 #done

Branch Naming

分支命名规范

Include Jira issue key in branch names:
  • feature/PROJ-123-user-authentication
  • bugfix/PROJ-456-fix-login-redirect
This automatically links branches to issues.
在分支名称中包含Jira问题键:
  • feature/PROJ-123-user-authentication
  • bugfix/PROJ-456-fix-login-redirect
这会自动将分支与问题关联。

Automation Rules

自动化规则

Set up Jira automation:
  • Move issue to "In Progress" when branch created
  • Move issue to "In Review" when PR opened
  • Move issue to "Done" when PR merged
设置Jira自动化:
  • 创建分支时,将问题移至“进行中”状态
  • 打开PR时,将问题移至“评审中”状态
  • 合并PR时,将问题移至“已完成”状态

Branching Models

分支模型

Gitflow in Bitbucket

Bitbucket中的Gitflow

yaml
pipelines:
  branches:
    main:
      - step:
          name: Deploy Production
          deployment: production
          script:
            - ./deploy.sh production

    develop:
      - step:
          name: Deploy Staging
          deployment: staging
          script:
            - ./deploy.sh staging

    'release/*':
      - step:
          name: Release Build
          script:
            - npm run build:release

    'feature/*':
      - step:
          name: Feature Build and Test
          script:
            - npm ci
            - npm test

    'hotfix/*':
      - step:
          name: Hotfix Build
          script:
            - npm ci
            - npm test
yaml
pipelines:
  branches:
    main:
      - step:
          name: Deploy Production
          deployment: production
          script:
            - ./deploy.sh production

    develop:
      - step:
          name: Deploy Staging
          deployment: staging
          script:
            - ./deploy.sh staging

    'release/*':
      - step:
          name: Release Build
          script:
            - npm run build:release

    'feature/*':
      - step:
          name: Feature Build and Test
          script:
            - npm ci
            - npm test

    'hotfix/*':
      - step:
          name: Hotfix Build
          script:
            - npm ci
            - npm test

Branch Permissions

分支权限

Configure in Repository settings > Branch permissions:
Main branch:
  • No direct pushes
  • Require pull request
  • Minimum 1 approval
  • Require passing builds
  • Require all tasks resolved
Develop branch:
  • Require pull request
  • Minimum 1 approval
  • Require passing builds
在仓库设置 > 分支权限中配置:
主分支:
  • 禁止直接推送
  • 要求使用拉取请求
  • 至少需要1个批准
  • 要求构建通过
  • 要求所有任务已解决
开发分支:
  • 要求使用拉取请求
  • 至少需要1个批准
  • 要求构建通过

Repository Management

仓库管理

Default Reviewers

默认评审人

Set up default reviewers for consistent code review:
  • Add team leads as default reviewers
  • Use CODEOWNERS-like patterns
设置默认评审人以确保一致的代码评审:
  • 添加团队负责人作为默认评审人
  • 使用类似CODEOWNERS的规则

Merge Checks

合并检查

Enable merge checks:
  • Minimum approvals
  • No unresolved tasks
  • Passing builds
  • No changes requested
启用合并检查:
  • 最少批准数
  • 无未解决任务
  • 构建通过
  • 无变更请求

Access Levels

访问级别

  • Admin: Full control
  • Write: Push and merge
  • Read: Clone and view
  • 管理员:完全控制权限
  • 写入:推送和合并权限
  • 读取:克隆和查看权限

Security Best Practices

安全最佳实践

Repository Variables

仓库变量

Configure secure variables in Repository settings > Pipelines > Variables:
yaml
undefined
在仓库设置 > 流水线 > 变量中配置安全变量:
yaml
undefined

Reference in pipeline

在流水线中引用

script:
  • echo "Deploying with token"
  • ./deploy.sh --token=$DEPLOY_TOKEN

Variable options:
- **Secured**: Masked in logs
- **Required for deployment**
script:
  • echo "Deploying with token"
  • ./deploy.sh --token=$DEPLOY_TOKEN

变量选项:
- **加密**:在日志中隐藏
- **部署必填**

IP Allowlisting

IP白名单

Restrict pipeline access to specific IP ranges for deployment environments.
限制流水线对部署环境的特定IP范围访问。

Access Tokens

访问令牌

Use repository or project access tokens instead of personal tokens:
  • Scoped to specific repositories
  • Easier to rotate
  • Better audit trail
使用仓库或项目访问令牌替代个人令牌:
  • 限定于特定仓库
  • 更易于轮换
  • 更好的审计追踪

Deployment Environments

部署环境

Environment Configuration

环境配置

yaml
pipelines:
  branches:
    main:
      - step:
          name: Deploy to Production
          deployment: production
          script:
            - ./deploy.sh
Configure environments in Repository settings > Deployments:
  • Set environment variables per environment
  • Configure deployment permissions
  • View deployment history
yaml
pipelines:
  branches:
    main:
      - step:
          name: Deploy to Production
          deployment: production
          script:
            - ./deploy.sh
在仓库设置 > 部署中配置环境:
  • 为每个环境设置环境变量
  • 配置部署权限
  • 查看部署历史

Deployment Permissions

部署权限

  • Require specific user approval for production
  • Set up deployment windows
  • Enable deployment freeze periods
  • 生产环境需要特定用户批准
  • 设置部署窗口
  • 启用部署冻结期

Atlassian Ecosystem Integration

Atlassian生态系统集成

Confluence Integration

Confluence集成

  • Link repositories to Confluence spaces
  • Embed code snippets
  • Auto-update documentation from commits
  • 将仓库链接到Confluence空间
  • 嵌入代码片段
  • 通过提交自动更新文档

Trello Integration

Trello集成

  • Connect cards to commits
  • Automatic card movement on PR events
  • 将卡片与提交关联
  • PR事件触发卡片自动移动

Opsgenie Integration

Opsgenie集成

  • Trigger alerts from pipeline failures
  • On-call notifications for deployment issues
  • 流水线失败时触发告警
  • 部署问题的值班通知

Best Practices Summary

最佳实践总结

  1. Use descriptive branch names with Jira keys
  2. Configure branch permissions for main branches
  3. Implement comprehensive pipelines with proper stages
  4. Use pipes for common tasks (AWS, Docker, etc.)
  5. Enable smart commits for Jira updates
  6. Set up deployment environments with proper permissions
  7. Use repository variables for secrets
  8. Configure merge checks for quality gates
  9. Leverage Atlassian integrations for seamless workflow
  1. 使用包含Jira键的描述性分支名称
  2. 为主分支配置分支权限
  3. 实现包含完整阶段的全面流水线
  4. 使用管道处理常见任务(AWS、Docker等)
  5. 启用智能提交以更新Jira
  6. 配置带有适当权限的部署环境
  7. 使用仓库变量存储密钥
  8. 配置合并检查作为质量门
  9. 利用Atlassian集成实现无缝工作流