使用 git 构建 dist 和推送构建的 Bitbucket 管道进行部署

Bitbucket pipeline to build dist and push build with git to deploy

我正在尝试在 Bitbucket 管道中构建我的 React 应用程序,然后使用 git 推送将其部署到我的生产服务器。

当我将所有内容都放在一个步骤中时,效果很好,如下所示:

image: node

clone:
  depth: full
pipelines:
  default:
    - step:
        name: Build app
        caches:
          - node
        script:
          - yarn
          - yarn build
          - git config user.email "<email>"
          - git config user.name "<name>"
          - git config remote.origin.url <remote ssh>
          - git add .
          - git commit -m "Add build"
          - git push

但是,我想将构建过程和部署过程拆分为不同的步骤。稍后,我还将在构建步骤中添加测试。

这是我试过的:

image: node

clone:
  depth: full
pipelines:
  default:
    - step:
        name: Build app
        caches:
          - node
        script:
          - yarn
          - yarn build
    - step:
        name: Deploy to production
        deployment: production
        script:
          - git config user.email "<email>"
          - git config user.name "<name>"
          - git config remote.origin.url <remote ssh>
          - git add .
          - git commit -m "Add build"
          - git push

这通过了,但不会在我的生产服务器上产生差异(与我上面的第一种方法相反)。我认为这是因为新步骤意味着新容器,因此新容器将无法访问我在上一步中所做的构建。

我试图用工件来解决这个问题,但无济于事。我似乎无法找到一种方法让我的构建从第一步进入第二步。

有人有什么建议吗?

执行的命令及其生成的文件不会在 个步骤之间持久化,因此您需要使用 artifacts 以便第一个由 yarn 生成的文件步骤在第二步中仍然可用。因此,如果您的 yarn 例如将文件写入项目根目录中的文件夹 yarn-output,您的管道将变为:

image: node

clone:
  depth: full
pipelines:
  default:
    - step:
        name: Build app
        caches:
          - node
        script:
          - yarn
          - yarn build
        artifacts:
          - yarn-output/**
    - step:
        name: Deploy to production
        deployment: production
        script:
          - git config user.email "<email>"
          - git config user.name "<name>"
          - git config remote.origin.url <remote ssh>
          - git add .
          - git commit -m "Add build"
          - git push