bitbucket - 在 Bitbucket 管道上,如果集成测试失败,如何自动回滚部署

标签 bitbucket bitbucket-pipelines

我已经设置了管道以包括单元测试和集成测试。

在部署代码之前,我会对管道中的代码运行单元测试,如果失败,则不会部署代码。我的管道看起来有点像这样:

.build_&_test_template: &build_test_template |
  nvm install 18
  nvm use 18
  npm ci
  npm run coverage

pipelines:
  branches:
    uat:
      - step:
          name: Deploy to UAT
          deployment: test
          script:
            - *build_test_template
            - *shared_deploy_template

因此,如果 npm runco​​verage 未能满足我在 jasmine 测试配置上设置的条件,代码将无法到达 shared_deploy_template

此后,我添加了一个集成步骤,该步骤将在代码部署后运行 (shared_deploy_template),看起来有点像这样:

.shared_postman_test_template: &shared_postman_test_template |
  npm run postmanTests

pipelines:
  branches:
    uat:
      - step:
        ...
        script:
          - *build_test_template
          - *shared_deploy_template
          - *shared_postman_test_template

现在,如果 shared_postman_test_template 导致任何失败,我希望能够自动回滚部署。

有没有办法根据测试条件自动回滚部署?

最佳答案

我不知道实现这一目标的直接方法,但它可能是可能的。

首先,构建、部署和测试脚本应在单独的步骤中运行。否则,失败的测试将向 bitbucket-pipelines 发出部署尚未完成的信号,因此将“没有任何可回滚的内容”。此外,如果构建和部署指令发生在同一步骤中,则重新部署过去的工作版本将强制进行重建,这通常会更慢、不必要且可能不可重现 (YMMV)。

然后,在测试步骤中您可以使用 after-script部分来处理 $BITBUCKET_EXIT_CODE 变量,该变量告知 script 部分是否失败。在那里,您可以有条件地使用 Bitbucket API 重新触发之前成功的部署到该环境。

类似的东西

pipelines:
  branches:
    uat:
      - step:
          name: Build
          script:
            - *build_test_template
          artifacts:
            - dist/whatever
      - step:
          name: Deploy
          deployment: test
          script:
            - *shared_deploy_template
      - step:
          name: Integration Tests
          script:
            - *shared_postman_test_template
          after-script:
            - if [ x$BITBUCKET_EXIT_CODE != x0 ]; then ???; fi

我认为这很重要,我只能向您指出 pipelines API documentation祝您好运。


如果你负担得起,我认为不在 CI 系统中实现这一点更容易,而是使用一个处理健康检查和开箱即用的蓝绿部署的部署平台。

关于bitbucket - 在 Bitbucket 管道上,如果集成测试失败,如何自动回滚部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77684390/

相关文章:

php - 在 GIT 控制的 Intranet 上使用 GIT/BitBucket 进行持续集成

continuous-integration - 如何在 Bitbucket 和 Azure DevOps 之间创建 webhook?

docker - 使用 BitBucket Pipelines 通过 SSH 访问部署到 VPS

firebase - 如何通过 Bitbucket Pipeline 部署 Firebase?

.net-core - Bitbucket Pipeline/.Net Core - 项目文件不存在

bitbucket - bitbucket 是否将 git 归咎于类似于 github 的行函数?

git - 为什么 .gitignore 和 .metadata 没有被提交?

git - Phabricator 钩子(Hook)和 Bitbucket

java - 从 Bitbucket 管道中的 Java Maven 项目的 pom.xml 获取版本

amazon-web-services - 已安装 rsync 时未找到 rsync 命令