In the console output of the failing job you will see an error similar to - stderr: fatal: Unable to create '/var/jenkins_home/jobs/job-name-example/workspace@script/.git/index.lock': File exists.
Take a note of the path from step 1 - /var/jenkins_home/jobs/job-name-example/workspace@script/.git/index.lock
Navigate to the Script Console in Jenkins UI - https://jenkins.example.com/script
Paste the following code block into the console for the values you found in step 2 without /index.lock.
dh = new File('./var/jenkins_home/jobs/job-name-example/workspace@script/.git')
dh.eachFile {
println(it)
}
Click Run
You will see an output of all the files in the .git directory like below...
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/index.lock
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/config
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/lfs
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/branches
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/objects
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/refs
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/FETCH_HEAD
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/index
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/logs
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/HEAD
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/hooks
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/info
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/description
Ensure the file exists - ./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/index.lock
Paste the following code block into the console for the values you found in step 2.
String filename = "./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/index.lock"
// this deletes the file
boolean fileSuccessfullyDeleted = new File(filename).delete()
println fileSuccessfullyDeleted
Click Run and you should see the output - true
Run step 4, 5 & 6 again and ensure the index.lock file has been deleted