tl;dr
The command git fetch origin [branch-name]
is not working when invoked within a script, but works when invoked from shell.
Both script and terminal are working just fine on another box. The box is behind a corporate proxy.
I assume there is some difference in the environment in the script compared to the shell.
Question
How can I get this command to run from within the script?
Detailed Info
Back Story
Everything worked on both boxes until the repository was migrated to GitHub. The origin was updated accordingly on both systems. The development system worked immediately, while the staging system just won't work.
Script
This is how the part of the script looks it checks all repositories are reached before acutally starting and therefor stops here:
check_git_connection_mapbender() {
cd ${installation_folder}mapbender
git fetch origin ${git_mapbender_repositoryBranch} &>/dev/null
if [ $? -ne 0 ]; then
echo "Fetching Mapbender repository failed!"
echo "Abording update..."
exit 1
fi
set_git_filemode_config
}
The two echo
lines are echoed out and the script exits.
The variables are all set.
What I've tried
compare shells
As I am suspecting some problem with environment variables I was trying to invoke the failing command git fetch origin [branch-name]
from the terminal directly. The command worked in all the cases, when invoked from shell:
me@box:/path/to/repo$ git fetch origin [branch-name]
me@box:/path/to/repo$ sudo git fetch origin [branch-name]
me@box:$ sudo -i cd /path/to/repo && git fetch origin [branch-name]
me@box:$ sudo su -
->root@box: cd /path/to/repo && git fetch origin [branch-name]
What does not seem to work is the following way:
me@box:$ sudo -i
->root@box: cd /path/to/repo && git fetch origin [branch-name]
<-- this does throw the error like in the script
git config
At first I assured the effective git config is the same on both systems.
Since then I tried adding certain options on the staging system to fix the problem, but without success.
I tried setting:
url.http://.insteadof=https://
http.https://github.com.sslverify=false
http.proxy
https.proxy
http.sslCert
As it didn't help I removed it again.
debugging connection
I added export GIT_TRACE_CURL=true
to the script so it looks like this:
check_git_connection_mapbender() {
cd ${installation_folder}mapbender
# DEBUG
export GIT_TRACE_CURL=true
source /etc/environment
echo $http_proxy
echo $https_proxy
# END DEBUG
echo $git_mapbender_repositoryBranch
git fetch origin ${git_mapbender_repositoryBranch} # &>/dev/null
if [ $? -ne 0 ]; then
echo "Fetching Mapbender repository failed!"
echo "Abording update..."
exit 1
fi
set_git_filemode_config
}
... and the output is
[correct http_proxy environment variable]
[correct https_proxy environment variable]
[correct branch name]
16:52:17.600248 http.c:599 == Info: Couldn't find host github.com in the .netrc file; using defaults
16:52:17.603973 http.c:599 == Info: Trying 140.82.121.4...
16:52:17.604035 http.c:599 == Info: TCP_NODELAY set
16:52:17.605297 http.c:599 == Info: connect to 140.82.121.4 port 443 failed: Verbindungsaufbau abgelehnt
16:52:17.605372 http.c:599 == Info: Failed to connect to github.com port 443: Verbindungsaufbau abgelehnt
16:52:17.605386 http.c:599 == Info: Closing connection 0
fatal: unable to access 'https://github.com/LVGL-SL/mapbender-sl.git/': Failed to connect to github.com port 443: Verbindungsaufbau abgelehnt
Fetching Mapbender repository failed!
Abording update...
proxy configuration
I'm told that the systems should be subjected to the same firewall rule sets as there is one for the subnet.
GIT_TRACE_CURL
compare when it works, and when it doesn't? Do you have$http_proxy
or$https_proxy
set in your environment when it works, and unset when it doesn't? When you say it doesn't work in a script, is this script called fromcrontab
, or does it fail when you run the script directly from your (otherwise working) command line? – Chris Davies Aug 27 '21 at 14:13&>/dev/null
do you see any error messages that might shed light on what is going wrong in the script environment? – DopeGhoti Aug 27 '21 at 14:25sudo ./script.bash
, no cron involved. – SvenTUM Aug 27 '21 at 14:39fatal
error message would not be displaced on a normal run. That's my bad I'll add the debugging version to the question. – SvenTUM Aug 27 '21 at 14:44sudo
, but here in a comment you're saying thatsudo ./script.bash
fails. Can you clarify please – Chris Davies Aug 27 '21 at 14:45sudo
and fails, but the commandgit fetch origin [branch-name]
will work in the terminal. – SvenTUM Aug 27 '21 at 14:47env | grep -i proxy
and thensudo -i
/env | grep -i proxy
. I suspect you've got settings in your usual account that are stripped out bysudo -i
. Failing that, you've gotgit
configuration under your normal$HOME
but when yousudo -i
theHOME
variable is changed to that of the target user – Chris Davies Aug 27 '21 at 15:42sudo git config --system https.proxy [PROXY]
(again) and now it works! I did set that previously already, but possibly another setting messed that up again. I haven't fully understood the problem yet, though. What stresses me he most is that I've got another system, that just worked without the setting; as well as that everything worked before migrating to GitHub... Being able to test in asudo -i
session enabled me to quickly try some settings and test. – SvenTUM Aug 27 '21 at 15:55