environment variable problem when executing shell script from a service

TLDR:

Care should be taken when a shell script is called from a service, because some of environment variables, such as HOME , which should have existed just naturally, may not be set.

The story

I wrote a web hook which is triggered by git pushes and it update the local repository and restart the application service corresponding to the repository.
The web hook is a nodejs(express) API server, wrapped by supervisor service to a service.
What it does is just request verification and calling a update_and_restart.sh.

# change current directory to avoid troubles..
cd "$(dirname "$0")"

# pull the latest code(a git pull loop is used just because the service is not quite stable)
# maybe checking the return code of `git pull` is a better.
while [ "$(git pull)" != "Already up-to-date." ]
do
echo "will try again"
done

echo "git pull finished successfully, Cheers!!"

# install dependent libs
npm install

# restart service
supervisorctl restart line_seminar_api

# send a notification mail
echo "API deployed" | mail -s "API deployed" foo.bar@example.com

The script worked correctly when executed in terminal, but failed to get the stored git credentials when called from my web hook, with the error message of “unable to set up default path; use –file”.

From the source of credential-store.c, we can see that this error happens when the file “~/.git-credentials” does not exist, but in my case, it does!
So what’s the cause?
Is it possible that the tilde character (‘ ~ ‘) is not expanded correctly?
We know that ‘~’ uses the value of the environment variable HOME , so the next question is , is HOME correctly set?
After printing all the environment variables of the web hook service, it becomes obvious that HOME is the bad guy: it is absent!
Here are the environment variables when the web hook is from supervisiord, which are inherited by the update_and_restart.sh.

SUPERVISOR_GROUP_NAME=git_web_hook
TERM=linux
SUPERVISOR_SERVER_URL=unix:///var/run/supervisor.sock
SUPERVISOR_PROCESS_NAME=gitee_hook
RUNLEVEL=2
UPSTART_EVENTS=runlevel
PREVLEVEL=N
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
UPSTART_INSTANCE=
UPSTART_JOB=rc
SUPERVISOR_ENABLED=1
runlevel=2
PWD=/
previous=N

After knowing the cause of the problem , the solution will be simple: just add the HOME environment variable to the supervisor service configuration file, as shown below of /etc/supervisor/conf.d/git_web_hook.conf .

[program:git_web_hook]
environment = 
    CMD="/path/to/update_and_restart.sh",
    HOME="/root"


directory=/root/git_web_hook
command=node index.js
autostart=true
autorestart=true
stderr_logfile=/var/log/git_web_hook/stderr
stdout_logfile=/var/log/git_web_hook/stdout

References:

1. Tilde-Expansion of GNU libc
2. source of credential-store