git pull failure in lifecycle config

Viewed 457

I'm trying to have a lifecycle configuration that upon startup, will pull the most recent code from a github repo.

I am encountering this error (logged in CloudWatch) on the git pull.

fatal: could not read Username for 'https://github.com/toasttab/midgard': No such device or address

However, as soon as I open a terminal on the server, I am able to git pull without issue. There is no prompt for my credentials when I git pull within terminal.

Here is my lifecycle config:


#!/bin/bash
set -e
sudo -u ec2-user -i <<'EOF'

# enable conda activate & activate python3 env
source ~/anaconda3/etc/profile.d/conda.sh
conda activate python3

# configure git
git config --global user.name "Foo"
git config --global user.email "Foo@Bar.com"

# install all git repo libraries into current env
cd ~/SageMaker/my-repo
git checkout master
git pull
pip install -e .

EOF

Any ideas?

2 Answers

This message means that Git is trying to prompt on /dev/tty for a username and password, but cannot do so, since you have no terminal.

In your case, you need to be sure that credentials are passed somehow. If, when you open a terminal, you're prompted for a username and password, then you need to provide those credentials in a noninteractive way. Usually this is done by reading a token from the environment using a credential helper, as outlined in an entry in the Git FAQ. Note that sudo clears all environment variables unless you use -E (which may or may not work depending on the configuration of your system).

You could also try using SSH URLs and forward an SSH agent, but again, you'd need to not clear the environment in sudo to do that, since your agent socket is in an environment variable.

If you want to disable the attempt to prompt on the terminal and just have Git fail (say, so you can get better debugging information), you can do so by setting GIT_TERMINAL_PROMPT to 0.

Add in your script the lines:

id -a
env

And compare the user and environment variables with the one you would see when opening a terminal a terminal on the server: there should be a difference which will explain why the script fails while the interactive session works.

Related