GitHub Actions中Lerna Publish执行失败求助
我们使用Lerna将Monorepo发布至私有包管理器Nexus,但在GitHub Actions中执行lerna publish命令时失败,仅返回模糊提示:
info cli using local version of lerna lerna notice cli v5.3.0 lerna info ci enabled Error: Process completed with exit code 1.
已尝试全局/本地安装Lerna的多种方式,以及调整lerna bootstrap和lerna publish的各类参数,但问题仍未解决。
环境信息
- Node版本:16.15.1
- Lerna版本:5.3.0
工作流文件(release.yml)
name: 'MyPackage Production Release' on: workflow_dispatch: inputs: version: required: true type: choice description: Which version should be published? options: - patch - minor - major tag: required: true type: choice description: Which npm tag should this be published to? options: - latest - next - test jobs: build-my-package: runs-on: [self-hosted, Linux, X64, enterprise] steps: - uses: actions/checkout@v3 with: fetch-depth: 0 #indicates all history for all branches and tags. - uses: actions/setup-node@v3 with: node-version: 16.15.1 registry-url: 'https://registry.npmjs.org' - name: Configure Identity run: | git config user.name github-actions git config user.email github-actions@github.com shell: bash - name: strict SSL run: | npm config set cafile my-file.pem npm config set strict-ssl false npm config set https-proxy http://proxy.our.proxysite.net:80/ npm config set proxy http://proxy.our.proxysite.net:80/ npm config set NODE_TLS_REJECT_UNAUTHORIZED=0 npm config set registry https://registry.npmjs.org/ - name: Install Dependencies run: | npm ci --no-package-lock --legacy-peer-deps lerna bootstrap --ignore-scripts -- --legacy-peer-deps shell: bash - name: Prepare Nexus Token run: | npm config set registry https://nexus.our.site.net/repository/my-package-repo/ npm config set email myemail@myemail.net npm config set strictSSL false npm config set alwaysAuth true echo //nexus.our.site.net/repository/my-package-repo/:_auth=${{ secrets.NEXUS_BASE64_AUTH }} > .npmrc npm config set _auth ${{ secrets.NEXUS_BASE64_AUTH }} shell: bash - name: Release run: | HUSKY_SKIP_HOOKS=1 lerna publish $(echo "${{ github.event.inputs.version }}") --yes --force-publish='*' --dist-tag $(echo "${{ github.event.inputs.tag }}") --conventional-commits --create-release github env: GH_TOKEN: ${{ secrets.GH_TOKEN }} GHE_API_URL: ${{ secrets.GHE_API_URL }} GHE_VERSION: ${{ secrets.GHE_VERSION }} shell: bash
1. 先拿到详细错误日志
当前的错误提示太笼统,直接在lerna publish命令后加--loglevel verbose参数,强制输出完整执行日志,就能定位到底是认证、Git操作还是发包环节出了问题:
HUSKY_SKIP_HOOKS=1 lerna publish $(echo "${{ github.event.inputs.version }}") --yes --force-publish='*' --dist-tag $(echo "${{ github.event.inputs.tag }}") --conventional-commits --create-release github --loglevel verbose
2. 修复Nexus认证的配置冲突
在Prepare Nexus Token步骤里,你同时用了npm config set _auth和写入.npmrc两种方式,容易导致认证信息冲突。Lerna会优先读取项目根目录的.npmrc,建议只保留文件写入的方式,而且要用追加模式避免清空原有内容:
修改该步骤为:
npm config set registry https://nexus.our.site.net/repository/my-package-repo/ npm config set email myemail@myemail.net npm config set strictSSL false npm config set alwaysAuth true # 用>>追加,避免覆盖原有.npmrc内容 echo "registry=https://nexus.our.site.net/repository/my-package-repo/" >> .npmrc echo "//nexus.our.site.net/repository/my-package-repo/:_auth=${{ secrets.NEXUS_BASE64_AUTH }}" >> .npmrc echo "//nexus.our.site.net/repository/my-package-repo/:always-auth=true" >> .npmrc
3. 确保Git操作权限正常
虽然配置了Git用户信息,但自托管Runner可能存在仓库同步问题,在Release步骤前加个拉取操作,保证仓库状态是最新的:
- name: Sync latest code run: git pull origin ${{ github.ref_name }}
另外要确认GH_TOKEN拥有仓库的写入权限(需要包含repo权限),因为--create-release github需要创建GitHub Release的权限。
4. 统一代理与SSL配置
在strict SSL步骤里设置的代理,可能不适用于私有Nexus仓库。如果Nexus不需要代理,就在Prepare Nexus Token步骤里清空代理配置:
npm config delete https-proxy npm config delete proxy
如果需要代理,先确认代理地址能正常访问你的Nexus私有仓库。
5. 验证包的发布配置
检查所有子包的package.json里是否设置了publishConfig.registry,避免部分包仍尝试发布到npm官方仓库:
"publishConfig": { "registry": "https://nexus.our.site.net/repository/my-package-repo/" }
同时确认自托管Runner的Node环境对项目目录有读写权限,避免因权限不足导致发包失败。
内容的提问来源于stack exchange,提问作者Travis Brigman

