NX monorepo fatal:没有这样的参考:'main~1'

Cle*_*ent 4 javascript git monorepo github-actions

我正在尝试在启用 nx 云的 github actions 中构建我的 nx 应用程序。我总是收到fatal: No such ref: 'main~1'错误。

该命令nx affected --target=build --base=main~1 --head=HEAD --parallel --with-deps在我提交之前有效,但在我提交之后,该命令在本地或 ci 中不再有效。

根据文档,将基础设置为main~1应该仅将其与主分支的先前提交进行比较。

Github Actions 中的完整错误:

yarn run v1.22.10
$ nx affected --target=build --base=main~1 --head=HEAD --parallel --with-deps
fatal: Not a valid object name main~1
fatal: No such ref: 'main~1'
nx affected

Run task for affected projects

Run command using --base=[SHA1] (affected by the committed, uncommitted and
untracked changes):
  --base  Base of the current branch (usually master)                   [string]

or using --base=[SHA1] --head=[SHA2] (affected by the committed changes):
  --base  Base of the current branch (usually master)                   [string]
  --head  Latest commit of the current branch (usually HEAD)            [string]

or using:
  --files        Change the way Nx is calculating the affected command by
                 providing directly changed files, list of files delimited by
                 commas                                                  [array]
  --uncommitted  Uncommitted changes                                   [boolean]
  --untracked    Untracked changes                                     [boolean]

Options:
  --help           Show help                                           [boolean]
  --version        Show version number                                 [boolean]
  --target         Task to run for affected projects         [string] [required]
  --parallel       Parallelize the command (default: false)            [boolean]
  --maxParallel    Max number of parallel processes. This flag is ignored if the
                   parallel option is set to `false`. (default: 3)      [number]
  --all            All projects                                        [boolean]
  --exclude        Exclude certain projects from being processed
                                                           [array] [default: []]
  --runner         This is the name of the tasks runner configured in nx.json
                                                                        [string]
  --skip-nx-cache  Rerun the tasks even when the results are available in the
                   cache                              [boolean] [default: false]
  --configuration  This is the configuration to use when performing tasks on
                   projects                                             [string]
  --only-failed    Isolate projects which previously failed
                                                      [boolean] [default: false]
  --verbose        Print additional error stack trace on failure

Error: Command failed: git merge-base --fork-point "main~1" "HEAD"
fatal: No such ref: 'main~1'

    at checkExecSyncError (child_process.js:616:11)
    at Object.execSync (child_process.js:652:15)
    at getFilesUsingBaseAndHead (/home/runner/work/clemento/app/node_modules/@nrwl/workspace/src/command-line/shared.js:55:37)
    at Object.parseFiles (/home/runner/work/clemento/app/node_modules/@nrwl/workspace/src/command-line/shared.js:25:20)
    at Object.<anonymous> (/home/runner/work/clemento/app/node_modules/@nrwl/workspace/src/command-line/affected.js:26:112)
    at Generator.next (<anonymous>)
    at fulfilled (/home/runner/work/clemento/app/node_modules/tslib/tslib.js:114:62) ***
  status: 128,
  signal: null,
  output: [
    null,
    <Buffer >,
    <Buffer 66 61 74 61 6c 3a 20 4e 6f 20 73 75 63 68 20 72 65 66 3a 20 27 6f 72 69 67 69 6e 2f 6d 61 69 6e 7e 31 27 0a>
  ],
  pid: 1713,
  stdout: <Buffer >,
  stderr: <Buffer 66 61 74 61 6c 3a 20 4e 6f 20 73 75 63 68 20 72 65 66 3a 20 27 6f 72 69 67 69 6e 2f 6d 61 69 6e 7e 31 27 0a>
***
error Command failed with exit code 1.
Run Code Online (Sandbox Code Playgroud)

工作流程文件

name: Deploy Updated Apps
on:
  push:
    branches:
      - main
      - feature/*
      - bugfix/*
      - hotfix/*
      - refactor/*
      - test/*
    paths:
      - "apps/**"
      - "libs/**"
      - .github/**
      - scripts/**
      - package.json
      - workspace.json
      - nx.json
jobs:
  deploy-updated-apps:
    name: Deploy Updated Apps
    runs-on: ubuntu-latest
    env:
      NX_BRANCH: ${{ github.event.number }}
      NX_RUN_GROUP: ${{ github.run_id }}
    steps:
      - uses: actions/checkout@v2
      - name: Get Service Account Key
        run: ./scripts/get-service-account.sh
      - name: Login to Google Cloud
        run: ./scripts/login-to-google-cloud.sh
      - name: Install dependencies
        run: yarn install
      - name: Build affected apps
        run: yarn affected:build
Run Code Online (Sandbox Code Playgroud)

nx.json

{
    "implicitDependencies": {
        "package.json": {
            "dependencies": "*",
            "devDependencies": "*"
        },
        ".eslintrc.json": "*"
    },
    "affected": {
        "defaultBase": "main"
    },
    "npmScope": "someorg",
    "tasksRunnerOptions": {
        "default": {
            "runner": "@nrwl/nx-cloud",
            "options": {
                "cacheableOperations": ["build", "lint", "test", "e2e"],
                "accessToken": "<ACCESS_TOKEN>",
                "canTrackAnalytics": false,
                "showUsageWarnings": true
            }
        }
    },
    "projects": {
        "frontend": {
            "tags": []
        },
        "frontend-e2e": {
            "tags": [],
            "implicitDependencies": ["frontend"]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Cle*_*ent 26

好吧……我终于明白是怎么回事了。登录github actions后git show-ref发现,actions/checkoutaction默认只检查一次commit。

对于触发工作流的 ref/SHA,默认情况下仅获取单个提交。

在指定fetch-depth: 0来获取所有分支的所有引用后,它起作用了。Nx 能够正确比较不同分支和同一分支中先前提交之间的差异。

jobs:
  deploy-updated-apps:
    name: Deploy Updated Apps
    runs-on: ubuntu-latest
    env:
      NX_BRANCH: ${{ github.event.number }}
      NX_RUN_GROUP: ${{ github.run_id }}
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Install dependencies
        run: yarn install
      - name: Build affected apps
        run: yarn affected:build:branch
Run Code Online (Sandbox Code Playgroud)
{
  "scripts": {
    "affected:build:branch": "nx affected:build --base=origin/main --head=HEAD --with-deps",
    "affected:build:main": "nx affected:build --base=origin/main~1 --head=HEAD --with-deps",
  },
}
Run Code Online (Sandbox Code Playgroud)

  • 它也是[文档](https://nx.dev/ci/monorepo-ci-github-actions)中示例的一部分,但它们没有解释为什么需要“fetch-深度:0”。感谢您的澄清!他们还添加了`-uses: nrwl/nx-set-shas@v2`,根据[README](https://github.com/nrwl/nx-set-shas),它是`一个设置基础的Github Action CI 中“nx 受影响”命令所需的头 SHA。 (2认同)