I'm trying to build my nx application in github actions with nx cloud enabled. I always get the fatal: No such ref: 'main~1'
error.
The command nx affected --target=build --base=main~1 --head=HEAD --parallel --with-deps
works before I commit, but after I commit, the command no longer works locally or in ci.
Based on the docs, setting the base to main~1
should just compare it to the previous commit of the main branch.
Full error in 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.
Workflow File
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"]
}
}
}
Okay... so I finally figured out what's going on. After logging git show-ref
in github actions, I found that the actions/checkout
action by default only checks out one commit.
Only a single commit is fetched by default, for the ref/SHA that triggered the workflow.
After specifying the fetch-depth: 0
to fetch all the refs for all the branches, it worked. Nx is able to properly compare the difference between the different branches and previous commits in the same branch.
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",
},
}