LoginSignup
13
13

More than 5 years have passed since last update.

ItamaeでGitのプライベートリポジトリをcloneする

Posted at

Itamaeには、Git resourceがあるんだけど、プライベートリポジトリの認証はオプションになかった。
今回、プライベートリポジトリをcloneしたかったので、実現した方法を2つ。
実現できたんだけど、いまいちベストプラクティスが分からない…。

なお、どちらの方法もVagrantで起動したVMに対して実行する前提。

cloneするURLに認証情報を埋め込む方法

さすがに、レシピ自体に認証情報を埋めたくなかったので、node.jsonに書くようにした。
ユーザ名でメールアドレスを使う場合は、エスケープしなきゃかも。

recipe.rb
homeDir = "/home/vagrant"
fugaDir = "#{homeDir}/fugaScope"
githubUser = node["git-env"]["user"]
githubPassword = node["git-env"]["password"]

git fugaDir  do
    repository "https://#{githubUser}:#{githubPassword}@github.com/hoge/fuga.git"
    user "vagrant"
    not_if "test -d #{fugaDir}"
end
node.json
{
    "git-env": {
        "user": "hoge",
        "password": "hogehoge"
    },
}

実行は以下の通り。

$ itamae ssh --vagrant -h vagrant --node-json node.json recipe.rb

git-credentialsを使う方法

.netrcじゃなくて、git-credentialsを使うんだ!というのをどこかで見かけたのでやってみた。
node.jsonと実行方法は一緒。

recipe.rb
homeDir = "/home/vagrant"
fugaDir = "#{homeDir}/fugaScope"
githubUser = node["git-env"]["user"]
githubPassword = node["git-env"]["password"]
credentialUrl = "https://#{githubUser}:#{githubPassword}@github.com"

execute "GitHub auth setting" do
    command "git config --global credential.helper store;
        echo '#{credentialUrl}' >> .git-credentials
    "
    user "vagrant"
    not_if "grep '#{credentialUrl}' #{homeDir}/.git-credentials"
end

git fugaDir  do
    repository "https://github.com/hoge/fuga.git"
    user "vagrant"
    not_if "test -d #{fugaDir}"
end
13
13
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
13
13