git 使用 gitlab-ci 部署 React 应用程序的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53788639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:15:30  来源:igfitidea点击:

Best way to deploy react app with gitlab-ci?

reactjsgitdockergitlabgitlab-ci

提问by cclloyd

I'm just getting started figuring out gitlab CI/CD. I have my own gitlab instance, and a couple runners, 1 shared, 1 project specific, both using docker engine.

我刚刚开始研究 gitlab CI/CD。我有自己的 gitlab 实例和几个运行程序,1 个共享,1 个特定于项目,都使用 docker 引擎。

Currently my staging server is its own VM that it hosts with docker-compose. I usually deploy to this server with a bare git repo, and just keep the build files in git.

目前我的登台服务器是它自己的虚拟机,它用 docker-compose 托管。我通常使用裸 git 存储库部署到此服务器,并将构建文件保存在 git 中。

But I wanted to switch to a CI/CD model, so I tried this as my .gitlab-ci.yml:

但我想切换到 CI/CD 模型,所以我尝试将其作为我的.gitlab-ci.yml

image: node

stages:
- build
- stage

build_frontend:
  stage: build
  script:
  - cd ./src/frontend
  - npm install && npm audit fix
  # CI=false set to ignore warnings as errors
  - CI=false npm run build
  artifacts:
    paths:
    - ./src/frontend/build
  tags:
  - projectname

But I'm sort of lost on how to actually deploy the build. What would the best way be to get the files onto the staging server, which is just a VM.

但是我对如何实际部署构建有点迷茫。将文件放到临时服务器上的最佳方法是什么,它只是一个虚拟机。

回答by VonC

You can take some clues from how GitLab itself uses its own CI, as described in "How to use GitLab CI for Vue.js":

您可以从 GitLab 本身如何使用自己的 CI 中获取一些线索,如“如何为 Vue.js 使用 GitLab CI”中所述:

They have a dedicated deploy step:

他们有一个专门的部署步骤:

build site:
  image: node:6
  stage: build
  script:
    - npm install --progress=false
    - npm run build
  artifacts:
    expire_in: 1 week
    paths:
      - dist

unit test:
  image: node:6
  stage: test
  script:
    - npm install --progress=false
    - npm run unit

deploy:
  image: alpine
  stage: deploy
  script:
    - apk add --no-cache rsync openssh
    - mkdir -p ~/.ssh
    - echo "$SSH_PRIVATE_KEY" >> ~/.ssh/id_dsa
    - chmod 600 ~/.ssh/id_dsa
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - rsync -rav --delete dist/ [email protected]:/your/project/path/

So if you can package and scp your app, you can deploy it to your VM.

因此,如果您可以打包和 scp 应用程序,则可以将其部署到您的 VM。