3
2

More than 5 years have passed since last update.

Permission denied (publickey) on Travis.CI

Posted at

はい。タイトルで察した方もおられると思います。やっていきましょう。

Travis.CIは最初にgit clone --recursiveでレポジトリをクローンするため、submoduleがすべて自動的にクローンされていきます。ただ、ここでgitのデータ転送プロトコルとしてsshを使用していると、秘密鍵はレポジトリに置かれていないのでログインに失敗し、結果クローンが失敗します。そしてバッジが赤くなります。

これを何とかする方法として、そもそも常にhttpsを使えばいいんじゃないのという話なんですが、既にgit@...でsubmoduleを登録してしまっているし、CIのことはCIでしたいので、travis.yml上で解決しましょう。

早速ですが解決方法です。以下のtravis.ymlを見て下さい。

この方は何をしているかというと、まずTravisがgit clone --recursiveしないようにします。

# disable the default submodule logic
git:
    submodules: false

続いて、力技で.gitmodulesに書かれている全てのgit@github.com:https://github.com/に書き換え、その後手動でgit submodule update --init --recursiveするというわけです。

before_install:
  - sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules
  - git submodule update --init --recursive

さて、これで解決、といきたいところですが、OS Xだとこれは動きません。OS Xでだけ、sed: 1: ".gitmodules": invalid command code .というエラーが出ます。

LinuxはLinuxですが、OS XはUnix系列なのでこの辺のコマンドの使い方がほんの少しだけ違うことがあり、発狂することがあります。

何がなんだかわからないですが、とりあえずsedのマニュアルを見ましょう。OS Xで。手元にない方のために付け加えておくと、BSDもUnix系列であり、OS XはBSD版のsedを提供しているので、例えばこれ(https://www.freebsd.org/cgi/man.cgi?query=sed&sektion=&n=1 )とほぼ同じであることが期待されます。

     -i extension
             Edit files in-place, saving backups with the specified extension.  If a zero-length extension is given, no backup will be saved.  It is not recommended to
             give a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.

-iオプションはファイルをその場(in-place)で変更します。なので元のファイルは消えてしまいます。これは怖い操作なので、sedはバックアップファイルを作ってあげたいと思ってくれています。さらにありがたいことに、バックアップファイルの拡張子を選ばせてくれます。-i ".bak"のようにすれば、file.bakにバックアップが作られるというわけです。ありがたいことです。

ところで、先のコマンドに戻りましょう。

sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules

まず、sed-iオプションを見て、続く文字列('s/git@github.com:/https:\/\/github.com\//')がバックアップファイルの拡張子だと解釈します。ということは、sedコマンドは.gitmodulesのはずです。おや、.gitmodulesは有効なsedコマンドではないですね。というわけでエラーを伝えましょう。

sed: 1: ".gitmodules": invalid command code .

はい。

解決策は、適当な拡張子をsed -i ".bak"のようにして渡すか、もう空文字列""にしてしまうことでしょう。空文字列を設定した場合、sedはバックアップファイルを作りません。

なので、"${TRAVIS_OS_NAME}" == "linux"ならsed -i 's/...' .gitmodulesを、"osx"ならsed -i "" 's/...' .gitmodulesを実行すれば解決です。お疲れ様でした。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2