Git 便携式 64 Bash 在 Windows Server 2016 任务计划程序中不工作

Git Portable 64 Bash not working in Windows Server 2016 Task Scheduler

我需要帮助来弄清楚为什么 Git 存储库命令在 Windows Server 2016 任务计划程序的脚本中 运行 时没有执行。当我在命令控制台中执行它们时,一切正常。

在 Windows Server 2016 任务计划程序中,我的操作 "Start a Program" 是:C:\Apps\repo.scripts\UpdateMyRepo.bat

UpdateMyRepo.batcmd代码为:

SET HOME=C:\Users\Repo
REM change to MyRepo git working repository
C:
cd \MyRepo
REM execute my script bash script to update my repository
C:\Apps\Git\bin\bash.exe --login -i -c "/c/Apps/repo.scripts/UpdateMyRepo.sh"

UpdateMyRepo.shbash代码为

#!/c/Apps/Git/bin/bash.exe -x
export HOME=/c/Users/Repo
cd /c/MyRepo
# write a log entry so we know we are in the repository folder
ls -al > /c/Apps/repo.scripts/myrepofolder.log
# write the git --version to a log file so we know git is working
/c/Apps/Git/bin/git.exe --version > /c/Apps/repo.scripts/version.log
# write the git status to a log file so we know git repository commands work
/c/Apps/Git/bin/git.exe status > /c/Apps/repo.scripts/status.log
# write a done log entry and quit the bash shell
echo done > /c/Apps/repo.scripts/done.log
exit

一切都在 Windows 2016 任务计划程序 除了 git status 命令。 git status 写入一个空白 status.log 文件。实际上,似乎任何其他命令,如 git addgit commitgit push 等,作用于存储库都会产生空白输出。

如果我以 Repo 用户身份登录并在 Windows 文件资源管理器中双击 C:\Apps\repo.scripts\UpdateMyRepo.bat 或在控制台中双击 运行ning 时手动执行命令,一切正常存储库 git 状态被写入 status.log。我在手动或触发时从任务计划程序执行任务时得到 "null" 结果。

请帮我弄清楚如何在 Windows Server 2016 任务计划程序中 运行 git 存储库命令。我已经尝试了太多命令、脚本和权限的变体,无法列出此处不起作用的每一个。

平台详情: Windows Server 2016 Standard,所有当前更新 Git 便携式 64 位,2.17.1.windows.2 存储库文件存储在操作系统和任务调度程序的本地硬盘驱动器中,而不是网络共享中


更新:当我 运行 WS2016 任务计划程序中的 Git 存储库任务并弄清楚如何记录一些输出时,我收到以下错误:

fatal: this operation must be run in a work tree

存储库文件夹不是 "bare" 存储库。所以我怀疑 WS2016 任务计划程序正在应用不适用于任务中分配的用户帐户的额外权限限制。如果我尝试 运行 管理控制台而不是普通用户控制台中的任务,我会遇到同样的错误。

首先,大多数 Git 命令写入 ,而不是 stdout

因此您需要 redirect stderr to stdout,例如使用 &> 而不是 >

其次,为了确保您所有的 Git 命令都将以正确的方式执行 tree/repo,您可以添加 environment variables for the repository location

export GIT_WORK_TREE=/c/path/to/my/repo
export GIT_DIR=/c/path/to/my/repo/.git