如何在Bitbucket Pipelines多步骤中使用同一环境变量并解决报错
解决Bitbucket Pipelines重复部署环境报错
报错原因
Bitbucket Pipelines规定,同一个分支的流水线中,同一个部署环境只能被指定一次,不能在多个步骤里重复添加deployment: staging,否则会触发你遇到的重复环境报错。
解决方案
要让所有步骤复用staging环境的变量,有两种可行方式:
方式一:将整个分支流水线关联到staging环境
把deployment: staging配置在分支流水线的顶层,而非每个步骤中,这样所有步骤都会自动继承该环境的变量,同时符合配置规则:
image: python:3.8 options: docker: true pipelines: branches: master: deployment: staging steps: - step: name: Setup stage script: - echo ${db_name} - step: name: Setup cli prod script: - echo ${db_name} - step: name: Setup cli sandbox script: - echo ${db_name}
这种方式的优势是能完整使用Bitbucket部署环境的特性(比如部署记录、环境权限管控)。
方式二:使用变量组共享staging变量
如果不需要部署环境的额外特性,仅需共享变量,可以创建一个staging变量组,在每个步骤中引用:
- 先在仓库的
Settings > Repository variables > Variable groups中创建名为staging的变量组,添加db_name等所需变量。 - 修改配置文件:
image: python:3.8 options: docker: true pipelines: branches: master: - step: name: Setup stage variables: - staging script: - echo ${db_name} - step: name: Setup cli prod variables: - staging script: - echo ${db_name} - step: name: Setup cli sandbox variables: - staging script: - echo ${db_name}
内容的提问来源于stack exchange,提问作者Raghav
相关产品推荐
相关产品推荐

