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

GitHub Actions中Angular构建产物移至.NET wwwroot后assets文件夹丢失

Angular构建产物assets文件夹在GitHub Actions中移动后丢失的问题

我正在开发一个采用.NET Core后端与Angular前端的项目,通过GitHub Actions工作流部署至Azure。工作流拉取前后端代码仓库,构建Angular项目后将产物移动到.NET后端的wwwroot文件夹中。

核心工作流步骤如下:

# Step 6: Install Angular dependencies and build the Angular app
- name: Install dependencies and build Angular app
  run: |
    cd frontend
    npm ci --legacy-peer-deps
    npx ng build --configuration=production --output-path="./angular-output" --output-hashing=all

# Step 7: List the Angular build output to verify contents
- name: List Angular build output
  run: ls -R ./frontend/angular-output

# Step 8: Move Angular build output to .NET wwwroot
- name: Move Angular build output to .NET wwwroot
  run: mv ./frontend/angular-output/* ./Main/Applications/Lili.Shop.API/wwwroot/

# Step 9: List the wwwroot contents after moving files to verify placement
- name: List wwwroot contents after moving Angular files
  run: ls -R ./Main/Applications/Lili.Shop.API/wwwroot

执行后发现:angular-output文件夹中的assets文件夹存在且包含预期文件,但执行mv命令后,仅index.html等文件出现在wwwroot中,assets文件夹及其内容看似丢失。

已尝试:

  • 单独移动assets文件夹但未解决问题;
  • 记录移动前后目录内容,确认assets在源目录存在但未出现在目标目录的顶层。

疑问:为何移动后assets文件夹丢失?是否是权限或GitHub Actions处理mv命令导致?如何确保所有Angular构建产物正确移至wwwroot?


问题分析与解决方案

核心原因

GitHub Actions默认使用Ubuntu Runner(Linux环境),mv ./source/* ./target/的行为在目标目录存在同名文件夹时会导致嵌套,而非替换:
如果你的.NET代码仓库中,wwwroot目录原本就存在assets文件夹,执行mv命令时,会把angular-output/assets文件夹移动到wwwroot/assets文件夹内部,形成wwwroot/assets/assets/的嵌套结构。此时ls -R wwwroot会显示顶层的assets目录,但你要的内容都在子目录里,看起来像是assets丢失了。

权限问题可能性极低,因为GitHub Actions Runner对仓库目录有完整操作权限,且你能成功移动其他文件,所以排除权限影响。

解决方案

方案1:清空wwwroot后再移动(最直接)

如果wwwroot目录的内容可以完全替换,先清空目录再移动产物,彻底避免嵌套问题:

- name: Move Angular build output to .NET wwwroot
  run: |
    rm -rf ./Main/Applications/Lili.Shop.API/wwwroot/*
    mv ./frontend/angular-output/* ./Main/Applications/Lili.Shop.API/wwwroot/

方案2:用rsync同步目录(更可靠)

使用rsync替代mv,它专门用于目录同步,能确保源目录内容完整覆盖目标目录,还能自动删除目标中多余的文件:

- name: Sync Angular build output to .NET wwwroot
  run: rsync -av --delete ./frontend/angular-output/ ./Main/Applications/Lili.Shop.API/wwwroot/
  • -a:归档模式,递归同步并保留文件属性
  • -v:显示同步过程,方便调试
  • --delete:删除目标目录中源目录不存在的文件,确保内容完全一致

方案3:单独处理assets文件夹

如果不想清空整个wwwroot,先删除目标目录的assets再移动:

- name: Move Angular build output to .NET wwwroot
  run: |
    rm -rf ./Main/Applications/Lili.Shop.API/wwwroot/assets
    mv ./frontend/angular-output/* ./Main/Applications/Lili.Shop.API/wwwroot/

验证建议

移动后执行以下命令,查看assets的实际路径,确认是否存在嵌套:

find ./Main/Applications/Lili.Shop.API/wwwroot -name "assets"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 19:34:54