Martin Bean
Martin Bean

Reputation: 39429

What's wrong with my deploy script?

I'm trying to automate my deployment process using a shell script I can run from my local machine. The process is simple:

  1. SSH to the remote server
  2. Run a git pull command depending on the argument supplied

Currently I have this:

#!/bin/sh

SSH_HOST="REMOVED"
SSH_PORT="REMOVED"
SSH_USER="REMOVE"

if [ "$1" = "live" ]
    then
    DIR="REMOVED"
    GIT_BRANCH="REMOVED"
    echo "Deploying to live site..."
elif [ "$1" = "test" ]
    then
    DIR="REMOVED"
    GIT_BRANCH="REMOVED"
    echo "Deploying to test site..."
else
    echo "ERROR: Please specify a target (either live or test)"
    exit 1;
fi

# create SSH connection
echo "ssh $SSH_USER@$SSH_HOST -p $SSH_PORT"
ssh $SSH_USER@$SSH_HOST -p $SSH_PORT << END_SSH
sudo -s
cd $DIR
git pull origin $GIT_BRANCH
END_SSH

echo "Site successfully deployed."
exit 0;

But I get the following messages when I run the script:

Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: sorry, you must have a tty to run sudo
error: cannot open .git/FETCH_HEAD: Permission denied

Where have I gone wrong? The last line would just be because I didn't successfully authenticate as sudo on the remote server, so it was unable to run the Git command due to a permissions error, but what's causing the first two lines? Have I misconfigured something, or do I have a syntax error somewhere or something?

Upvotes: 0

Views: 579

Answers (2)

CB Bailey
CB Bailey

Reputation: 793109

Pseudo-terminal will not be allocated because stdin is not a terminal.

This is normal behaviour because you've redirected stdin with <<.

sudo: sorry, you must have a tty to run sudo

And this is a direct result of the previous behaviour because sudo wants a real user sitting at an interactive terminal.

I don't think you really need an interactive session, just ssh as the user that you actually want to run the commands as and call ssh with a shell command as parameter, or better, create a script on the server and just run that from a non-interactive ssh call.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272417

ssh is asking for a password here:

ssh $SSH_USER@$SSH_HOST -p $SSH_PORT << END_SSH

but stdin isn't coming from a terminal (tty), but rather a heredoc. So it can't use its standard tty-compatible password-request mechanism.

If your remote machine is configured with your public key then you will likely avoid this. Another option is to investigate the -t option (which forces a pseudo-tty allocation)

Upvotes: 1

Related Questions