Github Actions作业间依赖未共享:Lint作业缺失npm安装依赖
问题根源
GitHub Actions里每个作业都是独立的运行环境——哪怕你指定了相同的runs-on,每个作业都会启动全新的虚拟机实例。Setup作业里安装的依赖、生成的文件,默认不会自动传递给后续的Lint和Test作业,这就是Lint作业里提示找不到nx包、需要重新安装的原因。
解决办法
要共享依赖和项目文件,有两种常用方式:用缓存保存依赖,或者用工件上传下载完整项目状态。
方式1:用缓存复用依赖
利用actions/cache把node_modules和NX的缓存目录存起来,后续作业直接读取缓存,不用重复安装。修改后的配置如下:
jobs: Setup: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - uses: nrwl/nx-set-shas@v3 - name: Setup --base dev branch for NX run: git fetch origin dev:dev # 缓存node_modules和NX缓存 - name: Cache dependencies and NX cache uses: actions/cache@v3 with: path: | node_modules .nx/cache key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }} restore-keys: | ${{ runner.os }}-npm- - name: Install Monorepo dependencies run: npm ci - name: Install all library dependencies run: npm run install:libs --parallel=3 - name: Affected Apps - Setup run: npx nx affected --base=$GITHUB_BASE_REF --target=setup --parallel=3 Lint: runs-on: ubuntu-latest needs: Setup steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - uses: nrwl/nx-set-shas@v3 - name: Setup --base dev branch for NX run: git fetch origin dev:dev # 恢复缓存 - name: Restore dependencies and NX cache uses: actions/cache@v3 with: path: | node_modules .nx/cache key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }} restore-keys: | ${{ runner.os }}-npm- - name: Affected Apps - Lint run: npx nx affected --base=$GITHUB_BASE_REF --target=lint --parallel=3 - name: Workspace - Lint run: | npx nx workspace-lint npx nx format:check --base=$GITHUB_BASE_REF Test: runs-on: ubuntu-latest needs: Setup steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - uses: nrwl/nx-set-shas@v3 - name: Setup --base dev branch for NX run: git fetch origin dev:dev # 恢复缓存 - name: Restore dependencies and NX cache uses: actions/cache@v3 with: path: | node_modules .nx/cache key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }} restore-keys: | ${{ runner.os }}-npm- - name: Affected Apps - Test run: npx nx affected --base=$GITHUB_BASE_REF --target=test --parallel=3
方式2:用工件传递完整项目状态
如果需要共享的不只是依赖,还有安装后的库文件或其他生成内容,可以在Setup作业把整个项目(除了.git目录)上传为工件,后续作业下载使用。配置示例:
jobs: Setup: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - uses: nrwl/nx-set-shas@v3 - name: Setup --base dev branch for NX run: git fetch origin dev:dev - name: Install Monorepo dependencies run: npm ci - name: Install all library dependencies run: npm run install:libs --parallel=3 - name: Affected Apps - Setup run: npx nx affected --base=$GITHUB_BASE_REF --target=setup --parallel=3 # 上传项目工件 - name: Upload project files uses: actions/upload-artifact@v3 with: name: project-artifacts path: | ./* !./.git/ Lint: runs-on: ubuntu-latest needs: Setup steps: # 下载项目工件 - name: Download project files uses: actions/download-artifact@v3 with: name: project-artifacts path: ./ - name: Affected Apps - Lint run: npx nx affected --base=$GITHUB_BASE_REF --target=lint --parallel=3 - name: Workspace - Lint run: | npx nx workspace-lint npx nx format:check --base=$GITHUB_BASE_REF Test: runs-on: ubuntu-latest needs: Setup steps: # 下载项目工件 - name: Download project files uses: actions/download-artifact@v3 with: name: project-artifacts path: ./ - name: Affected Apps - Test run: npx nx affected --base=$GITHUB_BASE_REF --target=test --parallel=3
注意事项
- 缓存方案更高效,适合依赖变动不频繁的场景,缓存会根据
package-lock.json的哈希值自动更新。 - 工件方案适合需要完整复用项目状态的场景,但上传下载会有一定时间成本。
- 用缓存方案时,每个作业仍需重新checkout代码;用工件方案则无需再checkout,因为工件已包含所有项目文件。
内容的提问来源于stack exchange,提问作者Chrissi Grilus
相关产品推荐
相关产品推荐

