Angular工作区NPM包GitHub Actions发布失败:dist文件夹不存在
问题根源与解决方案
你的Publish Job报错dist文件夹不存在的核心原因是:GitHub Actions中每个Job都运行在独立的虚拟机环境中,Build Job生成的dist文件不会自动共享到其他Job。另外你的配置还有几个小问题需要修正,以下是分步解决方法:
1. 用Artifact实现Job间产物传递
在Build Job完成后,将生成的dist文件夹上传为GitHub Artifact,然后在Publish和Debug Job中下载该Artifact,这样就能获取到构建产物。
2. 修正Publish Job的其他错误
env字段位置错误,应该放在step层级之外,而不是run脚本内部- 无需重复执行
actions/checkout(因为我们只需要构建产物,不需要重新拉代码) - 统一Node.js版本和Angular CLI的安装逻辑,避免版本不一致
修改后的完整配置
name: Angular Package on: release: types: [created] workflow_dispatch: inputs: release_type: required: false type: string project_name: description: 'Enter the project name' required: true type: string jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: node-version: 16 - name: Install Angular CLI run: npm install -g @angular/cli - name: Install dependencies run: npm ci - name: Build Angular Project run: ng build ${{ github.event.inputs.project_name }} # 新增:上传dist文件夹作为Artifact - name: Upload build artifact uses: actions/upload-artifact@v3 with: name: angular-build-artifact path: dist/ debug: needs: build runs-on: ubuntu-latest steps: # 新增:下载Build Job的Artifact - name: Download build artifact uses: actions/download-artifact@v3 with: name: angular-build-artifact path: dist/ - name: List Files in dist run: ls -R dist - name: Print Current Directory run: pwd publish: needs: build runs-on: ubuntu-latest steps: - uses: actions/setup-node@v3 with: node-version: 16 registry-url: 'https://npm.pkg.github.com' # 新增:下载Build Job的Artifact - name: Download build artifact uses: actions/download-artifact@v3 with: name: angular-build-artifact path: dist/ - name: Publish to GitHub Packages run: npm publish dist/${{ github.event.inputs.project_name }} --access public env: NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
关键修改说明
- Artifact传递:通过
actions/upload-artifact和actions/download-artifact实现dist文件夹在Job间的共享,这是GitHub Actions中跨Job传递文件的标准方式 - Publish Job优化:
- 直接使用
registry-url配置npm仓库,无需手动执行npm config set - 将
env字段移到step层级,确保NODE_AUTH_TOKEN正确注入 - 去掉不必要的
actions/checkout,因为我们只需要构建后的dist文件
- 直接使用
- 版本统一:将所有Job中的
actions/setup-node统一为v3,保持Node.js版本一致
内容的提问来源于stack exchange,提问作者Hamza Abbas
相关产品推荐
相关产品推荐

