为什么 GitLab Ci 找不到我的缓存文件夹?
Why does GitLab Ci not find my cached folder?
我的 GitLab 中有 CI 个作业 运行 的列表,但缓存没有按预期工作:
我的文档生成工作就这样结束了:
[09:19:33] Documentation generated in ./documentation/ in 4.397 seconds using gitbook theme
Creating cache angular...
00:02
WARNING: frontend/node_modules: no matching files
frontend/documentation: found 136 matching files
No URL provided, cache will be not uploaded to shared cache server. Cache will be stored only locally.
Created cache
Job succeeded
然后我开始部署作业(到 GitLab Pages)但它失败了,因为它没有找到文档文件夹:
$ cp -r frontend/documentation .public/frontend
cp: cannot stat 'frontend/documentation': No such file or directory
这是生成的缓存配置:
generate_docu_frontend:
image: node:12.19.0
stage: build
cache:
key: angular
paths:
- frontend/node_modules
- frontend/documentation
needs: ["download_angular"]
这是用于部署的:
deploy_documentation:
stage: deploy
cache:
- key: angular
paths:
- frontend/node_modules
- frontend/documentation
policy: pull
- key: laravel
paths:
- backend/vendor
- backend/public/docs
policy: pull
有谁知道为什么我的文档文件夹不见了?
您的作业输出中的消息 No URL provided, cache will be not uploaded to shared cache server. Cache will be stored only locally.
只是意味着您的 运行 用户没有使用 Amazon S3 to store your cache, or something similar like Minio。
没有 S3/Minio,缓存仅存在于第一个 运行 作业和缓存资源的 运行 节点上。因此,下一次作业 运行s 如果它被另一个 运行ner 拾取,它将没有缓存并且你会 运行 进入这样的错误。
有几种解决方法:
- 将您的 运行 用户配置为使用 S3/Minio(如果您有兴趣自己托管,Minio 有一个开源、免费使用的许可证)。
- 只使用一个 运行ner(不是一个很好的解决方案,因为通常更多的 运行ner 意味着更快的管道,这会大大减慢速度,尽管它会解决缓存问题)。
- 使用
tags
。标签用于确保特定 运行 人的工作 运行s。比方说,您的 10 个 运行 用户中有 1 个可以访问您的生产服务器,但所有人都可以访问您的下层环境服务器。您的低环境作业可以 运行 在任何 运行 用户上,但您的生产部署作业必须 运行 在具有产品访问权限的 运行 用户上。您可以通过在名为 运行 的 prod-access
上放置一个标签并将相同的标签放在产品部署作业上来实现。这将确保作业将 运行 在具有产品访问权限的 运行 用户上。这里可以使用相同的东西来确保缓存可用。
- 使用
artifacts
代替缓存。我将在下面解释这个选项,因为它确实是你应该用于这个用例的。
让我们简单解释一下 difference between Cache and Artifacts:
缓存通常最适合用于依赖安装,如 npm
或 composer
(对于 PHP 项目)。当你有一份 运行s npm ci
或 composer install
的工作时,你不希望它在你的管道 运行s 之后每次都 运行 当你不' 有必要更改依赖项,因为这会浪费时间。使用 cache
关键字缓存依赖项,以便后续 管道 不必再次安装依赖项。
当您需要在同一管道中的作业之间共享文件或目录时,最好使用工件。例如,安装 npm 依赖项后,您可能需要在管道中的另一个作业中使用 node_modules
目录。 运行ner 在作业结束时也会将工件上传到 GitLab 服务器,而不是在 运行 作业的 运行ner 本地存储。除非使用 dependencies
or needs
.
进行控制,否则将为所有后续作业下载所有先前的工件
工件是您用例的更好选择。
让我们更新您的 .gitlab-ci.yml
文件以使用 artifacts
而不是 cache
:
stages:
- build
- deploy
generate_docu_frontend:
image: node:12.19.0
stage: build
script:
- ./generate_docs.sh # this is just a representation of whatever steps you run to generate the docs
artifacts:
paths:
- frontend/node_modules
- frontend/documentation
expire_in: 6 hours # your GitLab instance will have a default, you can override it like this
when: on_success # don't attempt to upload the docs if generating them failed
deploy_documentation:
stage: deploy
script:
- ls # just an example showing that frontend/node_modules and frontend/documentation are present
- deploy.sh # whatever else you need to run this job
我的 GitLab 中有 CI 个作业 运行 的列表,但缓存没有按预期工作:
我的文档生成工作就这样结束了:
[09:19:33] Documentation generated in ./documentation/ in 4.397 seconds using gitbook theme
Creating cache angular...
00:02
WARNING: frontend/node_modules: no matching files
frontend/documentation: found 136 matching files
No URL provided, cache will be not uploaded to shared cache server. Cache will be stored only locally.
Created cache
Job succeeded
然后我开始部署作业(到 GitLab Pages)但它失败了,因为它没有找到文档文件夹:
$ cp -r frontend/documentation .public/frontend
cp: cannot stat 'frontend/documentation': No such file or directory
这是生成的缓存配置:
generate_docu_frontend:
image: node:12.19.0
stage: build
cache:
key: angular
paths:
- frontend/node_modules
- frontend/documentation
needs: ["download_angular"]
这是用于部署的:
deploy_documentation:
stage: deploy
cache:
- key: angular
paths:
- frontend/node_modules
- frontend/documentation
policy: pull
- key: laravel
paths:
- backend/vendor
- backend/public/docs
policy: pull
有谁知道为什么我的文档文件夹不见了?
您的作业输出中的消息 No URL provided, cache will be not uploaded to shared cache server. Cache will be stored only locally.
只是意味着您的 运行 用户没有使用 Amazon S3 to store your cache, or something similar like Minio。
没有 S3/Minio,缓存仅存在于第一个 运行 作业和缓存资源的 运行 节点上。因此,下一次作业 运行s 如果它被另一个 运行ner 拾取,它将没有缓存并且你会 运行 进入这样的错误。
有几种解决方法:
- 将您的 运行 用户配置为使用 S3/Minio(如果您有兴趣自己托管,Minio 有一个开源、免费使用的许可证)。
- 只使用一个 运行ner(不是一个很好的解决方案,因为通常更多的 运行ner 意味着更快的管道,这会大大减慢速度,尽管它会解决缓存问题)。
- 使用
tags
。标签用于确保特定 运行 人的工作 运行s。比方说,您的 10 个 运行 用户中有 1 个可以访问您的生产服务器,但所有人都可以访问您的下层环境服务器。您的低环境作业可以 运行 在任何 运行 用户上,但您的生产部署作业必须 运行 在具有产品访问权限的 运行 用户上。您可以通过在名为 运行 的prod-access
上放置一个标签并将相同的标签放在产品部署作业上来实现。这将确保作业将 运行 在具有产品访问权限的 运行 用户上。这里可以使用相同的东西来确保缓存可用。 - 使用
artifacts
代替缓存。我将在下面解释这个选项,因为它确实是你应该用于这个用例的。
让我们简单解释一下 difference between Cache and Artifacts:
缓存通常最适合用于依赖安装,如
npm
或composer
(对于 PHP 项目)。当你有一份 运行snpm ci
或composer install
的工作时,你不希望它在你的管道 运行s 之后每次都 运行 当你不' 有必要更改依赖项,因为这会浪费时间。使用cache
关键字缓存依赖项,以便后续 管道 不必再次安装依赖项。当您需要在同一管道中的作业之间共享文件或目录时,最好使用工件。例如,安装 npm 依赖项后,您可能需要在管道中的另一个作业中使用
进行控制,否则将为所有后续作业下载所有先前的工件node_modules
目录。 运行ner 在作业结束时也会将工件上传到 GitLab 服务器,而不是在 运行 作业的 运行ner 本地存储。除非使用dependencies
orneeds
.
工件是您用例的更好选择。
让我们更新您的 .gitlab-ci.yml
文件以使用 artifacts
而不是 cache
:
stages:
- build
- deploy
generate_docu_frontend:
image: node:12.19.0
stage: build
script:
- ./generate_docs.sh # this is just a representation of whatever steps you run to generate the docs
artifacts:
paths:
- frontend/node_modules
- frontend/documentation
expire_in: 6 hours # your GitLab instance will have a default, you can override it like this
when: on_success # don't attempt to upload the docs if generating them failed
deploy_documentation:
stage: deploy
script:
- ls # just an example showing that frontend/node_modules and frontend/documentation are present
- deploy.sh # whatever else you need to run this job