We can easily resolve virtually every problem in Jenkins. However, that is not true, especially when we do not have administrator access to the host Jenkins runs in. One problem is “Jenkins config.lock .git/config File exists”. This means Jenkins is unable to lock the config.lock file – “Could not lock config file .git/config: File Exists”.
Sample Scenario: Jenkins config.lock – Could not lock config file .git/config: File Exists
Imagine we have Jenkins running in the cloud and don’t have admin access to the host machine. We will likely have admin access to a Jenkins instance but not the host (e.g., EC2 instance). Also, giving admin access in Jenkins is safer and more accessible than direct access to the host.
Fix The Error using Groovy codes
To fix the Jenkins config.lock “Could not lock config file .git/config: File Exists” error, go to the Script console in Jenkins. Then, we list all the files in the host server, specifically under the Jenkins cache directory using the following Groovy script.
1 2 3 4 5 6 7 8 9 10 11 | import groovy.io.FileType def list = [] def dir = new File("/var/jenkins_home/caches") dir.eachFileRecurse (FileType.FILES) { file -> list << file } list.each { println it.path } |
Then, we identify the paths to config.lock files that are under the .git directory. Next, we use another Groovy code to delete the config.lock files. Consider the following codes.
1 2 3 4 | // TODO: Change the value of filename accordingly String filename = "/var/jenkins_home/caches/git-90324795569f0fe83b93a5c4fae97c85/.git/config.lock" boolean fileSuccessfullyDeleted = new File(filename).delete() println fileSuccessfullyDeleted |
In the codes, we specify a path to a config.lock file. We may have many files of this type in Jenkins. If so, delete them one by one carefully. Once we delete all the config.lock files, we can trigger the affected Jenkins jobs again.
Summary
To fix the Jenkins config.lock .git/config File exists or the “Could not lock config file .git/config: File Exists” error, we need a Jenkins user with the right to execute codes in the Script console. Then, we run Groovy codes to list all the files under the Jenkins cache directory in the host server. From the list of files, we can pick out those config.lock files. Finally, delete those config.lock files (one-by-one, for caution) using another Groovy code.