You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab CI/CD流水线跨Stage无法共享dist工件求助

GitLab CI/CD流水线:Build阶段Artifact无法传递到Deploy阶段的问题解决

问题背景

我搭建了一个包含build、test、deploy阶段的GitLab CI/CD流水线,build阶段生成的dist目录已配置为artifact,但deploy阶段始终无法找到该目录。build阶段日志显示artifact上传成功,但deploy阶段执行目录检查时提示dist不存在。

流水线配置

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy

build:
  image: node
  script:
    - npm install
    - npm run build
    - pwd; ls -latrR dist
  artifacts:
    paths:
      - \"dist/\"
    expire_in: 1 week

deploy:
  dependencies:
    - \"build\"
  script:
    - pwd; ls -latrR
    - apt-get update -qq && apt-get install -y -qq lftp
    - if [ -d 'dist' ] ; then lftp -c \"set ftp:ssl-allow no; open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_HOST; mirror -v dist/ $FTP_DESTINATION --reverse --ignore-time --parallel=10 --exclude-glob .git* --exclude .git/\"; else echo \"No dist directory\"; fi;
  environment:
    name: production
  only:
    - main

Build阶段关键日志

Uploading artifacts...
dist/: found 45 matching artifact files and directories 
WARNING: Upload request redirected                  location=https://gitlab.com/api/v4/jobs/7052630492/artifacts?artifact_format=zip&artifact_type=archive&expire_in=1+week new-url=https://gitlab.com
WARNING: Retrying...                                context=artifacts-uploader error=request redirected
Uploading artifacts as \"archive\" to coordinator... 201 Created  id=7052630492 responseStatus=201 Created token=glcbt-65
Cleaning up project directory and file based variables
00:00
Job succeeded

Deploy阶段关键日志

$ apt-get update -qq && apt-get install -y -qq lftp
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package lftp.
(Reading database ... 23248 files and directories currently installed.)
Preparing to unpack .../lftp_4.9.2-2+b1_amd64.deb ...
Unpacking lftp (4.9.2-2+b1) ...
Setting up lftp (4.9.2-2+b1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
$ if [ -d 'dist' ] ; then lftp -c \"set ftp:ssl-allow no; open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_HOST; mirror -v dist/ $FTP_DESTINATION --reverse --ignore-time --parallel=10 --exclude-glob .git* --exclude .git/\"; else echo \"No dist directory\"; fi;
No dist directory
Cleaning up project directory and file based variables
00:00
Job succeeded

解决方案

1. 修正Artifact路径的转义问题

配置中paths字段的\"dist/\"包含多余的转义引号,这会导致GitLab无法正确识别路径。将其改为:

artifacts:
  paths:
    - dist/

2. 确认dist目录的生成位置

检查build阶段pwd命令的输出,确认npm run build生成的dist目录是否在项目根目录下。如果dist生成在子目录(如packages/app/dist),需同步修改artifact路径为对应子目录。

3. 排除Test阶段的干扰

如果test阶段未配置dependencies,默认会下载所有前置阶段的artifact,若test阶段执行了清理或修改dist目录的操作,可能会影响后续deploy阶段的artifact。可在test阶段添加配置避免传递artifact:

test:
  # 你的test阶段配置
  artifacts: {}  # 不生成或传递artifact

4. 简化Dependencies配置

deploy阶段的dependencies字段中,引号可省略,直接写job名称:

deploy:
  dependencies:
    - build

5. 验证Deploy阶段的目录结构

在deploy阶段的script开头添加更详细的目录检查命令,确认artifact是否被正确下载:

script:
  - pwd
  - ls -la  # 查看当前目录所有文件/文件夹
  - ls -la dist || echo "dist not found"  # 直接检查dist
  # 后续命令...

修改后的完整配置示例

stages:          
  - build
  - test
  - deploy

build:
  image: node
  script:
    - npm install
    - npm run build
    - pwd; ls -latrR dist
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

test:
  # 你的test阶段配置
  artifacts: {}  # 避免干扰artifact传递

deploy:
  dependencies:
    - build
  script:
    - pwd
    - ls -la
    - apt-get update -qq && apt-get install -y -qq lftp
    - if [ -d 'dist' ] ; then lftp -c "set ftp:ssl-allow no; open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_HOST; mirror -v dist/ $FTP_DESTINATION --reverse --ignore-time --parallel=10 --exclude-glob .git* --exclude .git/"; else echo "No dist directory"; fi;
  environment:
    name: production
  only:
    - main

内容的提问来源于stack exchange,提问作者Andrew Tyson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:44:58