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

NX monorepo fatal: No such ref: 'main~1'

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

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

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

Github 操作完全错误:

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.

工作流文件

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

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"]
        }
    }
}

好的...所以我终于弄清楚发生了什么事。在 github 操作中记录 git show-ref 后,我发现默认情况下 actions/checkout 操作只检查一次提交。

Only a single commit is fetched by default, for the ref/SHA that triggered the workflow.

在指定 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
{
  "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",
  },
}