drone.io でsubmoduleをcloneするときにプロトコルを強制的に変更する

この記事は drone.io Advent Calendar 2017 - Adventar の21日目の記事です。

drone.io は Goで作られたオープンソースのCD (Continuous Delivery)環境です。

github.com

submoduleを含むリポジトリ

submoduleを含むリポジトリでテストした場合以下のようなエラーが表示されることがあります。

+ git init
Initialized empty Git repository in /drone/src/drone.example.com/hoge/drone-test/.git/
+ git remote add origin https://drone.example.com/hoge/drone-test.git
+ git fetch --no-tags origin +refs/heads/master:
From https://drone.example.com/hoge/drone-test
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
+ git reset --hard -q 91b0c8f8ea9a479cab3708ca40273df8
+ git submodule update --init --recursive
Submodule 'subproject' (git@drone.example.com:ci/subproject.git) registered for path 'subproject'
Cloning into '/drone/src/drone.example.com/hoge/drone-test/subproject'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'git@drone.example.com:ci/subproject.git' into submodule path '/drone/src/drone.example.com/hoge/drone-test/subproject' failed
Failed to clone 'subproject'. Retry scheduled
Cloning into '/drone/src/drone.example.com/hoge/drone-test/subproject'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'git@drone.example.com:ci/subproject.git' into submodule path '/drone/src/drone.example.com/hoge/drone-test/subproject' failed
Failed to clone 'subproject' a second time, aborting
exit status 1

これはsubmoduleをgitプロトコルで落としてこようとしてsshするための鍵が設定されていないため取得に失敗しエラーになっています。

通常submodule addする場合はhttpsを指定すれば十分だと思いますが、drone側で強制的にhttps指定に書き換えることができます。

.drone.yml に新しくcloneの項目を作ってください。 submodule_override を指定することで自由に書き換えることができます。

clone:
  git:
    image: plugins/git
    recursive: true
    submodule_override:
      test-kitchen: https://drone.example.com/ci/subproject.git

上記を設定することで以下のように

+ git config --global submodule.test-kitchen.url https://drone.example.com/ci/subproject.git

submoduleが上書きされ、cloneすることができます。