Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to accept user input as part of a remote git post-receive hook?

Tags:

git

bash

ssh

tty

hook

I've got a post-receive hook that deploys our master branch whenever we push master.

I'd like to make the deployment optional; the hook asks for simple Y/N response to achieve this; bash pseudo code below:

echo "Should I deploy this code now? Enter Y/N"
read deploy_code

case ${deploy_code} in
  "Y") do_a_deploy ;;
  "N") exit ;;
    *) # ask the question again if not Y or N ;;
esac 

Because of the way the post-receive hook gets its arguments on stdin the read line doesn't pause for input from the user, and the script loops round and round trying to get a Y/N answer.

I thought that specifically requesting from /dev/tty would fix this;

read deploy_code < /dev/tty

but that still causes the script to loop endlessly, as the input isn't coming from the keyboard.

Is it actually possible to get keyboard input in this circumstance?

Edit: Ah. It looks like it's actually ssh to blame. Looking at the output now I've added the < /dev/tty I'm seeing /dev/tty: No such device or address

And I can mimic this if I run the script locally, but over ssh:

ssh 127.0.0.1 "echo 40913e300c8bf4ed7ea91b5ef61a522f3be2c05f e2aabfc865547d8b494b76c96947bab0c62acfec refs/heads/master | /path/to/post-receive"

Edit 2:

So I can either set the -t option to ssh to request a tty as per enabling tty in a ssh session or I could enable it in the authorised_keys file on the server on a per-key basis

Edit 3:

After creating ~/bin/ssh-t

#!/bin/sh
ssh -tt "$@"

(the double -t option forces a tty) and setting GIT_SSH to point to it, I'm now getting the dreaded fatal: protocol error: bad line length character: followed by 005 which I guess is something in .bash_profile or similar echoing before git has a chance to run

like image 456
jaygooby Avatar asked Oct 18 '11 15:10

jaygooby


1 Answers

You could parse the commit comment as well. Git allows empty commits. Just another option.

like image 140
Adam Dymitruk Avatar answered Sep 19 '22 22:09

Adam Dymitruk