You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在BitBucket Pipelines中避免master与development分支的部署代码重复?

Reusing BitBucket Pipeline Steps for Multiple Branches with YAML Anchors

Absolutely! You can use YAML anchors and aliases to avoid duplicating your deployment script logic across branches. The key is to define a reusable base configuration anchor that contains all the shared logic, then reference it in each branch while overriding only the differences (like the deployment environment, step name, and branch-specific paths).

Here's a clean, working refactor of your script:

# Define a reusable anchor for core deployment logic (shared across branches)
&base_deployment
  trigger: manual
  script:
    - pipe: atlassian/scp-deploy:1.0.1
      variables:
        USER: $DOMAIN
        SERVER: $SERVER
        LOCAL_PATH: 'dist/*'

branches:
  master:
    - step:
        name: Deployment to production
        deployment: production
        <<: *base_deployment
        script:
          # Merge base variables + add production-specific path
          - pipe: atlassian/scp-deploy:1.0.1
            variables:
              <<: *base_deployment.script[0].variables
              REMOTE_PATH: '~/public/prod'
  development:
    - step:
        name: Deployment to development
        deployment: development
        <<: *base_deployment
        script:
          # Merge base variables + add development-specific path
          - pipe: atlassian/scp-deploy:1.0.1
            variables:
              <<: *base_deployment.script[0].variables
              REMOTE_PATH: '~/public/dev'

Breakdown of how this works:

  1. Anchor Setup: The &base_deployment anchor stores all shared configuration (manual trigger, SCP pipe with common variables like USER and SERVER). This way, you only maintain this logic once.
  2. Alias Reference: The <<: *base_deployment syntax merges the base configuration into each branch's step, so you don't have to rewrite the entire script block.
  3. Override Differences: We only specify what changes per branch: the step name, deployment environment (to pull the correct BitBucket variables), and REMOTE_PATH for the target server directory.

If you ever need to update shared logic (like changing the SCP pipe version or adjusting a common variable), you only modify the anchor definition—all branches will inherit the change automatically.

内容的提问来源于stack exchange,提问作者Bas van Dijk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 14:57:29