Ubuntu – Using sudo in a Git hook to set permissions after deploy

deploymentgitpermissionsUbuntu

I run a few sites on Ubuntu and I'm setting up a new deploy worflow as follows (the example uses the staging environment):

On my dev machine, I do git push staging which pushes to a bare repository and triggers the post-receive hook, which do these things:

  • Checkout the project on a separate folder
  • Backup the current deployed directory
  • Deploy the files with rsync
  • Set the appropriate permissions using a post_deploy.sh script

The above is working fine, except the permissions setting part.

Since I need to use sudo to do a chgrp and chown, I tried adding the following to /etc/sudoers:

myusername ALL=(ALL:ALL) NOPASSWD: /path/to/post_deploy.sh

But when performing the git push I get this error:

remote: sudo: no tty present and no askpass program specified

If I run post_deploy.sh directly from the deployed server I have no problems.

How can I run the post_deploy.sh script from the git hook? I don't mind having to type an extra password, but from what I read the askpass thing is not for the command line (am I right?).

Best Answer

This is an issue with /etc/sudoers file where it has the following entry:

Defaults requiretty

It is detailed in THIS POST

There are two options, comment out the Defaults requiretty setting from /etc/sudoers or use the pseudo-tty allocation (-t) argument for ssh.

Try the following in your post-receive script:

ssh -t 127.0.0.1 "sudo chown user /path/to/pushfile"

Although you will have to have ssh pre-shared keys configured to yourself and run it once manually to add an entry to known hosts (or find the argument to ignore this).

Related Topic