When deploying an application with Chef, I've got the code base set to be cloned from a private github repository with the following resource:
git '/mnt/application' do
repository 'git@github.com:organization/repository'
reference 'master'
action :sync
user node.application.user
group node.application.user
end
However, after scanning the documentation for the git
resource, I can't see how you supply the key file for authentication. I'm also confused as to how to store this key in a data bag, as the file contains a bunch of new lines. Any ideas?
I went through same problem, Only thing I was missing was this command then everything went well:
Reference and for my whole steps can be found at my blog: http://www.sadafnoor.com/blog/simplest-way-to-write-your-chef-cookbook-that-git-clone-private-repo-using-bitbucket-deploy-key/
if you are in a linux distribution store your ssh key in
<your home directory>/.ssh
and add github.com to<your home directory>/.ssh/known_hosts
You can add github.com to known_hosts using the following command
ssh-keyscan -H github.com >> <your home directory>/.ssh/known_hosts
After doing this you can clone your repo using
git
resource of chefYou should try this cookbook https://github.com/poise/application_git. It solves the problem that you mentioned.
With this cookbook, you can use
application_git
resource, specifiyng the private key:Based on the hint by sadaf2605, this was the easiest way for me – I just had to make sure to set the correct user/group as well as turn off StrictHostKeyChecking:
In case someone comes across this, the above didn't work for me, I kept getting the error:
What specifying ssh_wrapper does is it sets the GIT_SSH environment variable, and it turns out you can't provide parameters in the GIT_SSH environment variable (see Git clone with custom SSH using GIT_SSH error).
Instead, you would need to write your script to a file first, then set GIT_SSH to it.
So:
And change the git resource part to:
We use the similar setup for Mercurial, but it should be the same with Git, I hope.
We use ssh keys to authenticate. The key is stored in encrypted databag (with newlines replaced by "\n"). First of all this private key is created on the node from databag.
And then use it when connecting to git repository using ssh_wrapper: