GIT Push and Puppet Master
Goal: Be able to remote push to our puppetmaster using git
Note: I'm assuming you have passwordless ssh set up to make this all really easy
Setting up the bare repo:
- Clone your puppet repo down to your local machine (normal git clone)
- Create a local bare clone of that repo
git clone --bare my_local_clone bare_clone.git
copy that bare clone to the remote server
scp -r bare_clone.git user@git.example.com:/opt/git
Set up the git remote server
Set up the new remote on your local machine (inside your local repo full clone):
git remote add NAMEOFREMOTE username@remoteserver:/opt/git
- On the remote, add this following file as
./hooks/post-receive
andchmod 755
it:
#!/usr/bin/env ruby
# post-receive
#From: http://krisjordan.com/essays/setting-up-push-to-deploy-with-git
# 1. Read STDIN (Format: "from_commit to_commit branch_name")
from, to, branch = ARGF.read.split " "
# 2. Only deploy if master branch was pushed
# Uncomment to ensure this only happens on master branch
# if (branch =~ /master$/) == nil
# puts "Received branch #{branch}, not deploying."
# exit
# end
# 3. Copy files to deploy directory
#you can change the deploy_to_dir to whatever is needed
#deploy_to_dir = File.expand_path('../deploy')
deploy_to_dir = '/etc/puppet/'
`GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f #{branch}`
puts "DEPLOY: #{branch} (#{to}) copied to '#{deploy_to_dir}'"
# 4.TODO: Deployment Tasks
# i.e.: Run Puppet Apply, Restart Daemons, etc
Push to remote server
- Push the master branch to the remote server
git push NAMEOFREMOTE master
How to test without needing to commit to local clone
- get the last two commit ids on the remote bare clone
git log -2 --format=oneline --reverse
- now run the script (grad the two git hashes from the output of the previous command) (on the remote server inside the bare clone again)
echo "$FROM_ID $TO_ID master" | ./hooks/post-receive
Future to do:
- Have it so that when puppet master repo gits a merge CI kicks in (not sure what those tests look like or how to run)
- When tests pass, have it auto deploy to puppetmaster