Jenkinsのgit fetchでCannot lock refエラーが出た時の対応

eiryu
4 min readOct 17, 2018

--

ジョブを実行したら以下のようなエラーが出た。

error: Cannot lock ref 'refs/remotes/origin/refactor/foo/bar': 'refs/remotes/origin/refactor/foo' exists; cannot create 'refs/remotes/origin/refactor/foo/bar'
! [new branch] refactor/foo/bar -> origin/refactor/foo/bar (unable to update local ref)
error: some local refs could not be updated; try running
'git remote prune git@github.com:orgname/reponame.git' to remove any old, conflicting branches
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1710)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1454)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:63)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:314)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:764)
... 11 more
ERROR: null
Finished: FAILURE

Gitの仕様として、refactor/fooが既にあるためrefactor/foo/barが作れないとのこと。
Jenkinsサーバ上に残骸が残っているため、git remote pruneすれば良いとのことで、実際やってみるとfetch出来るようになった。

$ sudo -u jenkins git remote prune origin
Pruning origin
URL: git@github.com:orgname/reponame.git
* [pruned] origin/refactor/foo
.
.
$ sudo -u jenkins git fetch
.
.

発生頻度はそんなに高くないが、かといって発生した時にJenkinsサーバに入ってこの作業はしたくない。
ビルドの前処理にシェルスクリプトでgit remote prune実行するしかないかと思っていたら、ジョブのソースコード管理のGitの欄のAdditional Behavioursで指定出来ることが発覚。

追加を押した時のリスト
追加後

この設定を全ジョブに行ったので今後は発生しないはず。
しかし、ジョブが増えた時に気にしなくて良いように、Gitの共通設定が欲しいなあ。。

ちなみに設定すると以下のようなログがジョブのコンソールに出るようになり、効いていることが分かる。

> git -c core.askpass=true fetch --tags --progress git@github.com:orgname/reponame.git +refs/heads/*:refs/remotes/origin/* --prune

参考

--

--