事象
GitLab Runner上でgitlab-ci.yamlの処理の中で、2度目の実行から、git remote add した際にリモートリポジトリの宛先が既に登録されているとエラーが表示される。
$ git remote add codecommit https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test
fatal: remote codecommit already exists.
原因
GitLab RunnerがGitLabリポジトリを持っている方法が、Git fetchになっていたため、git remoteがそのまま残ってしまったことが原因です。
対処方法
対処方法は2通りあります。
- 対処方法1: IF文で分岐処理させる
- 対処方法2: リポジトリの取得方法をgit clone方式に変更
対処方法1: IF文で分岐処理させる
単純にremoteに登録されていたら、set-urlで上書きをするif分で分岐処理する方法です。
修正前のコード
text<br />
script:<br />
- git config --global credential.helper '!aws codecommit --profile codecommit credential-helper $@'<br />
- git config --global credential.UseHttpPath true<br />
- git remote add codecommit https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test<br />
- git push codecommit master<br />
修正したコード
text<br />
script:<br />
- git config --global credential.helper '!aws codecommit --profile codecommit credential-helper $@'<br />
- git config --global credential.UseHttpPath true<br />
- >-<br />
if [ "codecommit" = $(git remote | grep codecommit) ]; then<br />
git remote set-url codecommit https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test<br />
else<br />
git remote add codecommit https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test<br />
fi<br />
- git push hogehoge master<br />
対処方法2: リポジトリの取得方法をgit clone方式に変更
Runnerによって実行されるたびに、リポジトリからgit cloneするため、remoteの宛先リポジトリが保持され続けられる減少は回避できます。しかし、差分更新のfetchと比較して全取得のcloneの方が時間を要するので、Runnerの実行時間との兼ね合いかなと思います。
以上