简体   繁体   English

使用Bitbucket管道构建dist并使用git推送构建以进行部署

[英]Bitbucket pipeline to build dist and push build with git to deploy

I'm trying to build my React app in a Bitbucket pipeline, and then deploy it using a git push to my production server. 我试图在Bitbucket管道中构建我的React应用,然后使用git push将其部署到生产服务器。

This works fine when I put everything in one step, like so: 当我将所有内容都放在一个步骤中时,这种方法可以正常工作,如下所示:

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

However, I would like to split the build process and the deploy process into separate steps. 但是,我想将构建过程和部署过程分为单独的步骤。 Later on, I will also add tests to the build step. 稍后,我还将在构建步骤中添加测试。

This is what I tried: 这是我尝试的:

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

This passes, but doesn't produce a difference on my production server (as opposed to the first way I did it above). 这样可以通过,但不会对我的生产服务器产生任何影响(与上述第一种方法相反)。 I assumed this was because a new step would mean a new container, and that new container would thus not have access to the build I did in the previous step. 我认为这是因为新步骤将意味着一个新容器,因此该新容器将无法访问我在上一步中所做的构建。

I tried to get around that with artifacts, but to no avail. 我试图用人工制品来解决这个问题,但无济于事。 I can't seem to find a way to get my build from the first step into the second step. 我似乎找不到从第一步到第二步的构建方法。

Anyone have any advice? 有人有什么建议吗?

The executed commands and their resulting files are not persisted between steps, so you will need to use artifacts in order for the files produced by yarn in the first step to still be available in the second step. 执行的命令及其生成的文件不会步骤之间持久存在 ,因此您将需要使用工件 ,以使第一步中纱线产生的文件在第二步中仍然可用。 So if your yarn for example writes the files to the folder yarn-output in the root of the project your pipeline would become: 因此,例如,如果您的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM