我有一个看起来像这样的 .gitlab-ci.yml
:
image: mcr.microsoft.com/playwright:latest
before_script:
- apt-get remove nodejs -y
- apt-get update
- apt-get install -y curl
- curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
- apt-get install -y nodejs
- npm install
pages:
stage: build
script:
- npm run docsify ; npx next build ; npx next export
- rm -rf public
- mv out public
artifacts:
paths:
- public
cache:
key: default
paths:
- node_modules/
- ".next/cache/"
- public
untracked: true # unclear if needed
only:
- main
它运行了,看起来它做了所有正确的事情:
Running with gitlab-runner 14.9.1 (bd40e3da)
on runner-gitlab-runner-7ff6fc5d7b-dc9sh 2bfx5V6B
Preparing the "kubernetes" executor
Using Kubernetes namespace: gitlab-managed-apps
Using Kubernetes executor with image mcr.microsoft.com/playwright:latest ...
Using attach strategy to execute scripts...
Preparing environment
Waiting for pod gitlab-managed-apps/runner-2bfx5v6b-project-3385-concurrent-06khk6 to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner-2bfx5v6b-project-3385-concurrent-06khk6 to be running, status is Pending
ContainersNotInitialized: "containers with incomplete status: [init-permissions]"
ContainersNotReady: "containers with unready status: [build helper]"
ContainersNotReady: "containers with unready status: [build helper]"
Running on runner-2bfx5v6b-project-3385-concurrent-06khk6 via runner-gitlab-runner-7ff6fc5d7b-dc9sh...
Getting source from Git repository
Fetching changes with git depth set to 50...
Initialized empty Git repository in
Created fresh repository.
Checking out 456ffc27 as main...
Skipping Git submodules setup
Restoring cache
Checking cache for default-4...
No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted.
Successfully extracted cache
Executing "step_script" stage of the job script
...
Saving cache for successful job
Creating cache default-4...
node_modules/: found 22078 matching files and directories
.next/cache/: found 18 matching files and directories
public: found 925 matching files and directories
untracked: found 21814 files
No URL provided, cache will be not uploaded to shared cache server. Cache will be stored only locally.
Created cache
所以应该是caching:
node_modules/
.next/cache/
public
我的理解是,在下一个 pipeline 运行的下一个 git clone
之后,GitLab CI 应该恢复克隆存储库顶部的缓存。 (或者通过卷挂载使缓存可用,但我无法在 /cache
或 ./cache
中找到它。)
在下一次提交中打印出 public
的内容表明情况并非如此。
但从所有外观来看,缓存都已恢复:
Fetching changes with git depth set to 50...
Initialized empty Git repository in
Created fresh repository.
Checking out d7d84cd8 as main...
Skipping Git submodules setup
Restoring cache
Checking cache for default-4...
No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted.
Successfully extracted cache
缓存应该在哪里?
我应该如何检查它以查看它是否工作?
附录:
从 Next.js 的角度来看,此消息表明 Next.js 也没有从缓存中受益:
warn - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
我有https://nextjs.org/docs/messages/no-cache#gitlab-ci 中以利用caching 的行。
回答1
您尚未配置分布式 caching,因此您的缓存仅在本地存储在缓存上传时运行作业的特定运行器上。
跑步者之间不共享缓存。因此,如果您有多个跑步者,如果您的作业被与上次不同的跑步者接手,则缓存可能不可用。这是因为 GitLab 假设不同的运行器可能位于不同的平台和架构上,并且缓存文件可能取决于这些环境因素(例如二进制兼容性)。如果您有多个运行器或正在使用自动缩放功能,则需要配置 https://docs.gitlab.com/runner/configuration/autoscale.html#distributed-runners-caching。
如果您只有一个运行器但仍然存在此问题,请确保您的配置没有https://docs.gitlab.com/ee/ci/caching/index.html#troubleshooting。
您的作业中的日志消息表明缓存正在成功创建和恢复(从本地缓存)。
共享跑步者是否需要 S3?
如果您有多个运行器或使用如上所述的自动缩放配置,则需要分布式 caching。但是,您不必为此使用 AWS S3,只需使用与 S3 兼容的对象存储即可。例如,您可以自托管 https://github.com/minio/minio,它提供(除其他外)与 S3 兼容的对象存储服务器。
或者通过卷挂载使缓存可用,但我无法在 /cache 或 ./cache 中找到它。
这是从跑步者吊舱的角度来看的。 runner 会自动从 /cache
(挂载到 runner pod)中提取压缩缓存(如果存在)到构建目录。 Jobs 不会直接访问 /cache
挂载!但是,如果您想手动检查 runner pod 上的缓存,最好知道这一点。
您可以通过登录 runner pod 上的 shell(使用 kubectl exec -it <your-runner-pod-name> /bin/sh
)并检查 /cache
目录和其中的任何压缩档案来检查缓存。
回答2
确保您的跑步者配置为使用分布式 caching - https://docs.gitlab.com/runner/configuration/autoscale.html#distributed-runners-caching
回答3
如果要使用caching,则必须设置分布式caching。
如果要设置分布式 caching,则必须设置 S3 兼容的对象存储。
没有对象存储的 caching 没有开箱即用的支持。
即使您为每个构建使用相同的运行器,它也不会持续存在。