如何解决Gitlab CI执行git clean -ffdx时出现段错误的问题
根因分析
你遇到的git clean -ffdx偶现段错误,核心触发原因有两个:
- 跨发行版镜像共享缓存导致权限混乱:你的
.npm_dependencies模板全局共享node_modules缓存,但不同job使用的基础镜像所属发行版不一致:mhart/alpine-node:12为Alpine系,cypress/browsers:node12.18.0-chrome83-ff77为Debian系,两个镜像内默认运行用户的UID/GID不匹配,缓存拉取后文件权限错乱,git clean遍历无权限文件时偶发段错误。 - 旧版本Gitlab Runner的已知bug:13.x及以下版本的Gitlab Runner内置git组件,在处理包含大量小文件的目录(如node_modules)执行clean操作时,存在内存访问越界问题,偶发段错误。
解决方案
- 拆分不同镜像job的缓存,避免跨发行版共享node_modules缓存
修改缓存配置,按镜像类型区分缓存key:
image: mhart/alpine-node:12 stages: - build - test variables: NODE_MODULES_DIR: '$CI_PROJECT_DIR/node_modules' .npm_dependencies_alpine: cache: key: npm-deps-alpine paths: - ${NODE_MODULES_DIR} before_script: - npm ci .npm_dependencies_cypress: cache: key: npm-deps-cypress paths: - ${NODE_MODULES_DIR} before_script: - npm ci jest-tests: extends: .npm_dependencies_alpine stage: test script: - npm run jest:test cypress-tests: stage: test extends: .npm_dependencies_cypress image: cypress/browsers:node12.18.0-chrome83-ff77 script: - npm install -g wait-on - npm run start & wait-on http://localhost:3000 - npm run cypress:run build: extends: .npm_dependencies_alpine stage: build artifacts: paths: - build/ script: - npm run build
- 禁用自动git clean,手动指定清理范围
在全局或对应job的变量中添加GIT_CLEAN_FLAGS: none,跳过runner默认的git clean逻辑,手动清理不需要的文件,避免遍历node_modules目录:
variables: NODE_MODULES_DIR: '$CI_PROJECT_DIR/node_modules' GIT_CLEAN_FLAGS: none .npm_dependencies: before_script: # 手动清理除node_modules外的变更文件 - git ls-files --others --exclude-standard | grep -v node_modules | xargs rm -rf || true - git checkout . -- ':!node_modules' || true - npm ci
- 升级Gitlab Runner到14.x及以上稳定版本,修复内置git的段错误bug。如果使用的是公共共享runner无法自主升级,可选择前两种方案规避问题。
内容的提问来源于stack exchange,提问作者Litvinov Anatoliy
相关产品推荐
相关产品推荐

