ERROR: Error cloning remote repo 'origin'

Jenkins setup:
Version: 2.401.1
I have a multibranch pipeline with few jenkins stages. Jenkinsfile is kept in a scm repo.
Jenkins agent is created using ec2 plugin
Issue:
Facing an intermittent issue where checkout scm fails with below issue:
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
The recommended git tool is: NONE
using credential xxx
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository https:///abc.git

git init /var/lib/jenkins/workspace/abc_controller # timeout=10
Fetching upstream changes from https:///abc.git
git --version # timeout=10
git --version # ‘git version 2.39.3’
using GIT_ASKPASS to set credentials Credentials to xxx repository
git fetch --no-tags --force --progress – https:///abc.git +refs/heads/controller:refs/remotes/origin/controller # timeout=10
ERROR: Error cloning remote repo ‘origin’
hudson.plugins.git.GitException: Command “git fetch --no-tags --force --progress – https:///abc.git +refs/heads/controller:refs/remotes/origin/controller” returned status code 1:
stdout:
stderr: remote: Enumerating objects: 13251
remote: Enumerating objects: 18395, done.
remote: Counting objects: 0% (1/5144)
remote: Counting objects: 1% (52/5144)
remote: Counting objects: 2% (103/5144)
remote: Counting objects: 3% (155/5144)
remote: Counting objects: 4% (206/5144)


Resolving deltas: 99% (12013/12133)
Resolving deltas: 100% (12133/12133)
Resolving deltas: 100% (12133/12133), done.
error: cannot lock ref ‘refs/remotes/origin/controller’: Unable to create ‘/var/lib/jenkins/workspace/abc_controller/.git/refs/remotes/origin/controller.lock’: No such file or directory
From https:///abc
! [new branch] controller → origin/controller (unable to update local ref)

at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2842)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2185)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:635)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$2.execute(CliGitAPIImpl.java:871)
at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$GitCommandMasterToSlaveCallable.call(RemoteGitImpl.java:170)
at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$GitCommandMasterToSlaveCallable.call(RemoteGitImpl.java:161)
at hudson.remoting.UserRequest.perform(UserRequest.java:211)
at hudson.remoting.UserRequest.perform(UserRequest.java:54)
at hudson.remoting.Request$2.run(Request.java:377)
at hudson.remoting.InterceptingExecutorService.lambda$wrap$0(InterceptingExecutorService.java:78)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Suppressed: hudson.remoting.Channel$CallSiteStackTrace: Remote call to EC2 (jenkins-slave) - jenkins-slave (xxx-xxx)
	at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1784)
	at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:356)
	at hudson.remoting.Channel.call(Channel.java:1000)
	at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.execute(RemoteGitImpl.java:153)

Tried a few solutions:

  1. Increased timeout from 10 mins to 20 mins
    Logs-> Same issue
  2. Implement ‘Clean Before Checkout’ with git plugin
    Logs->
    org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: xxx org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: node

org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: xxx Caused: java.io.IOException: Remote call on EC2 (jenkins-agent) - jenkins-agent (xxx) failed

Plugin list is here

Office-365-Connector: 4.18.0
ace-editor: 1.1
analysis-model-api: 11.2.0
ant: 487.vd79d090d4ea_e
antisamy-markup-formatter: 159.v25b_c67cd35fb_
apache-httpcomponents-client-4-api: 4.5.14-150.v7a_b_9d17134a_5
authentication-tokens: 1.53.v1c90fd9191a_b_
authorize-project: 1.6.0
aws-credentials: 191.vcb_f183ce58b_9
aws-java-sdk-cloudformation: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-codebuild: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-ec2: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-ecr: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-ecs: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-efs: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-elasticbeanstalk: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-iam: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-kinesis: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-logs: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-minimal: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-sns: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-sqs: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk-ssm: 1.12.481-392.v8b_291cfcda_09
aws-java-sdk: 1.12.481-392.v8b_291cfcda_09
aws-parameter-store: 1.2.2
badge: 1.9.1
blueocean-autofavorite: 1.2.5
blueocean-bitbucket-pipeline: 1.27.4
blueocean-commons: 1.27.4
blueocean-config: 1.27.4
blueocean-core-js: 1.27.4
blueocean-dashboard: 1.27.4
blueocean-display-url: 2.4.2
blueocean-events: 1.27.4
blueocean-git-pipeline: 1.27.4
blueocean-github-pipeline: 1.27.4
blueocean-i18n: 1.27.4
blueocean-jira: 1.27.4
blueocean-jwt: 1.27.4
blueocean-personalization: 1.27.4
blueocean-pipeline-api-impl: 1.27.4
blueocean-pipeline-editor: 1.27.4
blueocean-pipeline-scm-api: 1.27.4
blueocean-rest-impl: 1.27.4
blueocean-rest: 1.27.4
blueocean-web: 1.27.4
blueocean: 1.27.4
bootstrap4-api: 4.6.0-6
bootstrap5-api: 5.2.3-1
bouncycastle-api: 2.28
branch-api: 2.1105.v472604208c55
build-user-vars-plugin: 1.9
caffeine-api: 3.1.6-115.vb_8b_b_328e59d8
checks-api: 2.0.0
cloudbees-bitbucket-branch-source: 805.v7f97d29dc0f5
cloudbees-disk-usage-simple: 182.v62ca_0c992a_f3
cloudbees-folder: 6.815.v0dd5a_cb_40e0e
command-launcher: 100.v2f6722292ee8
commons-text-api: 1.10.0-36.vc008c8fcda_7b_
commons-httpclient3-api: 3.1-3
commons-lang3-api: 3.12.0-36.vd97de6465d5b_
configuration-as-code-secret-ssm: 1.0.1
configuration-as-code: 1647.ve39ca_b_829b_42
credentials-binding: 604.vb_64480b_c56ca_
credentials: 1254.vb_96f366e7b_a_d
cucumber-reports: 5.7.5
cucumber-trends-report: 1.3
data-tables-api: 1.13.3-4
display-url-api: 2.3.7
docker-commons: 419.v8e3cd84ef49c
docker-workflow: 563.vd5d2e5c4007f
dtkit-api: 3.0.2
durable-task: 507.v050055d0cb_dd
ec2: 2.0.7
echarts-api: 5.4.0-4
email-ext: 2.97
extended-read-permission: 3.2
external-monitor-job: 203.v683c09d993b_9
favorite: 2.4.2
font-awesome-api: 6.3.0-2
forensics-api: 2.2.0
git-client: 4.3.0
git-server: 99.va_0826a_b_cdfa_d
git: 5.0.2
github-api: 1.314-431.v78d72a_3fe4c3
github-branch-source: 1725.vd391eef681a_e
github: 1.37.1
greenballs: 1.15.1
groovy-postbuild: 2.5
handlebars: 3.0.8
jacoco: 3.3.3
handy-uri-templates-2-api: 2.1.8-22.v77d5b_75e6953
htmlpublisher: 1.31
instance-identity: 142.v04572ca_5b_265
ionicons-api: 56.v1b_1c8c49374e
jackson2-api: 2.15.2-350.v0c2f3f8fc595
jakarta-activation-api: 2.0.1-3
jakarta-mail-api: 2.0.1-3
javadoc: 233.vdc1a_ec702cff
javax-activation-api: 1.2.0-6
javax-mail-api: 1.6.2-9
jaxb: 2.3.8-1
jdk-tool: 66.vd8fa_64ee91b_d
jenkins-design-language: 1.27.4
jersey2-api: 2.39.1-2
jira: 3.10
jjwt-api: 0.11.5-77.v646c772fddb_0
job-dsl: 1.84
jquery-detached: 1.2.1
jquery3-api: 3.7.0-1
jsch: 0.2.8-65.v052c39de79b_2
junit: 1207.va_09d5100410f
ldap: 682.v7b_544c9d1512
lockable-resources: 1156.v5e9f897ece02
mailer: 457.v3f72cb_e015e5
mask-passwords: 150.vf80d33113e80
matrix-auth: 3.1.8
matrix-project: 789.v57a_725b_63c79
mercurial: 1260.vdfb_723cdcc81
metrics: 4.2.18-438.v0ede325a_4c68
mina-sshd-api-common: 2.10.0-69.v28e3e36d18eb_
mina-sshd-api-core: 2.10.0-69.v28e3e36d18eb_
momentjs: 1.1.1
node-iterator-api: 49.v58a_8b_35f8363
okhttp-api: 4.11.0-145.vcb_8de402ef81
pam-auth: 1.10
parameterized-scheduler: 1.2
pipeline-build-step: 491.v1fec530da_858
pipeline-graph-analysis: 202.va_d268e64deb_3
pipeline-groovy-lib: 656.va_a_ceeb_6ffb_f7
pipeline-input-step: 468.va_5db_051498a_4
pipeline-milestone-step: 111.v449306f708b_7
pipeline-model-api: 2.2133.ve46a_6113dfc3
pipeline-model-definition: 2.2133.ve46a_6113dfc3
pipeline-model-extensions: 2.2133.ve46a_6113dfc3
pipeline-rest-api: 2.32
pipeline-stage-step: 305.ve96d0205c1c6
pipeline-stage-tags-metadata: 2.2133.ve46a_6113dfc3
pipeline-stage-view: 2.32
pipeline-utility-steps: 2.15.4
plain-credentials: 143.v1b_df8b_d3b_e48
plugin-util-api: 3.2.1
popper-api: 1.16.1-3
popper2-api: 2.11.6-2
prism-api: 1.29.0-7
prometheus: 2.2.3
pubsub-light: 1.17
rake: 1.8.0
rebuild: 320.v5a_0933a_e7d61
role-strategy: 633.v836e5b_3e80a_5
saml: 4.418.vdfa_7489a_b_a_2d
scm-api: 672.v64378a_b_20c60
script-security: 1251.vfe552ed55f8d
snakeyaml-api: 1.33-95.va_b_a_e3e47b_fa_4
snyk-security-scanner: 3.2.7
splunk-devops: 1.10.1
sse-gateway: 1.26
ssh-credentials: 305.v8f4381501156
ssh-slaves: 2.877.v365f5eb_a_b_eec
sshd: 3.303.vefc7119b_ec23
structs: 324.va_f5d6774f3a_d
timestamper: 1.25
token-macro: 359.vb_cde11682e0c
trilead-api: 2.84.v72119de229b_7
validating-string-parameter: 2.8
variant: 59.vf075fe829ccb
warnings-ng: 10.2.0
windows-slaves: 1.8.1
workflow-aggregator: 596.v8c21c963d92d
workflow-api: 1213.v646def1087f9
workflow-basic-steps: 1017.vb_45b_302f0cea_
workflow-cps-global-lib: 609.vd95673f149b_b
workflow-cps: 3673.v5b_dd74276262
workflow-durable-task-step: 1247.v7f9dfea_b_4fd0
workflow-job: 1308.v58d48a_763b_31
workflow-multibranch: 746.v05814d19c001
workflow-scm-step: 408.v7d5b_135a_b_d49
workflow-step-api: 639.v6eca_cd8c04a_a_
workflow-support: 839.v35e2736cfd5c
xunit: 3.1.2

Those messages indicate that you are cloning a very large git repository. If you create an existing clone on the agent in a location that is separate from the Jenkins agent directory, you can use the git plugin’s support of a reference repository to reduce the git data transfer, the time required to perform the data transfer, and use no more disc space than you are already using for the large repository.

A 10-15 minute video segment is available that describes the different options that are available to better manage very large git repositories. The video segment describes reference repositories, narrow refspecs, shallow clone, large file support, and more.

When I’ve seen that message in the past, there was often a network issue between the controller and the agent. I’ve not found a general purpose solution to that message, though reducing the amount of time that the agent spends cloning the repository may also reduce the occurrence of that message.