在 gitlab CI 构建期间缓存构建文件夹

Cache build folder during gitlab CI build

我有一个远程服务器,我通过 Nginx 为我的项目提供服务。我正在使用 Gitlab CI 来自动化我的部署过程,但我 运行 遇到了问题。 当我将我的提交推送给 master b运行ch gitlab-运行ner 运行 很好,但问题是它删除了我的 React 构建文件夹(没关系,因为我已经将它放入 .gitignore),但是因为它总是删除我的构建文件夹,所以我的 Nginx 在构建完成之前无法提供任何文件,并且创建了一个新的构建文件夹。 这个问题有什么解决办法吗?如果我可以在构建过程完成之前缓存我的构建文件,那就太好了。 我附上了我的 gitlab.ci.yml。提前致谢!

image: docker:latest
services:
  - docker:dind
stages:
  - build
  - deploy
variables:
    GIT_SSL_NO_VERIFY: "1"
build-step:
  stage: build
  tags:
    - shell
  script:
    - docker image prune -f
    - docker-compose -f docker-compose.yml -f docker-compose.prod.yml build
deploy-step:
  stage: deploy
  tags:
    - shell
  script:
    - docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d


部署作业开始时,应该可以使用 git 提取并禁用 git 清理。这里有 link 变量来执行此操作:

https://docs.gitlab.com/ee/ci/yaml/#git-clean-flags

https://docs.gitlab.com/ee/ci/yaml/#git-strategy

看起来像这样:

deploy-step:
  variables:
    GIT_STRATEGY: fetch
    GIT_CLEAN_FLAGS: none
  stage: deploy
  tags:
    - shell
  script:
    - docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

这应该使 GitLab 使用 git fetch 而不是 git clone,而不是 运行 任何 git clean ... 命令。您之前 运行 的构建工件不应被删除。

虽然这有一些问题。如果构建出现问题,您可能最终会遇到这样一种情况,您将不得不手动登录到 运行ner 修复它的服务器。 GitLab 使用 git clean 的原因是为了防止这些类型的问题。

一个更合适的解决方案是使用nginx 有一种双重缓冲区。您可以有两个不同的构建文件夹,更改 nginx 中的配置,然后向 nginx 发送信号以重新加载配置。然后 nginx 将确保优雅地切换到您的应用程序的新版本,而不会出现任何中断。这是一个 link 给已经这样做的人:

https://syshero.org/2016-06-09-zero-downtime-deployments-using-nginx/