gitlab预接收钩子python导入错误

gitlab预接收钩子python导入错误,gitlab,githooks,gitlab-omnibus,Gitlab,Githooks,Gitlab Omnibus,以下是我的githook: import os print 'hi from draj pre-receive githook' import gitlab, subprocess 当我ssh到我的gitlab ubuntu实例并运行它时,它会正确地打印“hi from draj pre-receive githook”,并成功退出 但是,当我推送到安装githook的存储库时,我得到: remote: import os, gitlab, subprocess remote: Imp

以下是我的githook:

import os
print 'hi from draj pre-receive githook'
import gitlab, subprocess
当我ssh到我的gitlab ubuntu实例并运行它时,它会正确地打印“hi from draj pre-receive githook”,并成功退出

但是,当我推送到安装githook的存储库时,我得到:

remote:     import os, gitlab, subprocess
remote: ImportError: No module named gitlab
To https://gitlab.learningdollars.com/root/00-boilerplate-draj.git
 ! [remote rejected] master -> master (pre-receive hook declined)
我看了这两篇文章:它们似乎表明环境变量是问题的原因

因此,我修改了githook以打印环境:

import os
print 'hi from draj pre-receive githook'
print 'os.environ: ', os.environ
import gitlab, subprocess
因此,我直接在gitlab实例上运行了预接收钩子,得到:

hi from draj pre-receive githook
os.environ:  {'LANG': 'en_US.UTF-8', 'USERNAME': 'root', 'TERM': 'xterm-256color', 'SHELL': '/bin/bash', 'MAIL': '/var/mail/root', 'SUDO_UID': '[OBFUSCATED]', 'SUDO_GID': '[OBFUSCATED]', 'SUDO_COMMAND': '/var/opt/gitlab/git-data/repositories/root/00-boilerplate-draj.git/custom_hooks/pre-receive', 'LOGNAME': 'root', 'USER': 'root', 'HOME': '/home/ubuntu', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'SUDO_USER': 'ubuntu', 'LS_COLORS': '[OBFUSCATED]'}
但是,当我尝试执行调用预接收钩子的推送时,我得到:

(venv)Govindas-MacBook-Pro:boilerplate_draj govindadasu$ git push origin master
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 284 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: hi from draj pre-receive githook
remote: os.environ:  {'HOME': '', 'GIT_DIR': '.', 'LD_LIBRARY_PATH': '', 'GL_ID': 'user-1', 'PATH': '/opt/gitlab/bin:/opt/gitlab/embedded/bin:/opt/gitlab/embedded/libexec/git-core:/opt/gitlab/embedded/libexec/git-core:/opt/gitlab/bin:/opt/gitlab/embedded/bin:/bin:/usr/bin'}
remote: Traceback (most recent call last):
remote:   File "/var/opt/gitlab/git-data/repositories/root/00-boilerplate-draj.git/custom_hooks/pre-receive", line 9, in <module>
remote:     import gitlab, subprocess
remote: ImportError: No module named gitlab
To https://gitlab.learningdollars.com/root/00-boilerplate-draj.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://gitlab.learningdollars.com/root/00-boilerplate-draj.git'
(venv)Govindas MacBook Pro:boilerplate\u draj govindadasu$git推送原版主机
计数对象:2,完成。
增量压缩最多使用4个线程。
压缩对象:100%(2/2),完成。
写入对象:100%(2/2),284字节| 0字节/秒,完成。
总计2(增量1),重复使用0(增量0)
远程:来自draj预接收githook的hi
远程:os.environ:{'HOME':''GIT_DIR':'','LD_LIBRARY_PATH':'','GL_ID':'user-1','PATH':'/opt/gitlab/bin:/opt/gitlab/embedded/bin:/opt/gitlab/embedded/libexec/GIT core:/opt/gitlab/embedded/GIT core:/opt/gitlab/bin:/usr/bin'}
远程:回溯(最近一次呼叫最后一次):
远程:文件“/var/opt/gitlab/git data/repositories/root/00样板文件draj.git/custom_hooks/pre receive”,第9行,在
远程:导入gitlab,子流程
远程:导入错误:没有名为gitlab的模块
到https://gitlab.learningdollars.com/root/00-boilerplate-draj.git
! [远程拒绝]主机->主机(拒绝预接收挂钩)
错误:无法将某些引用推送到'https://gitlab.learningdollars.com/root/00-boilerplate-draj.git'
但是这些文章没有提供的是,为了能够导入gitlab,我应该如何使用预接收githook修改环境

对解决方案有一个提示,但它对我不起作用,因为bash pre-receive钩子在我的gitlab实例上不起作用,只有python钩子起作用。所以我创建了一个通用的预接收:

#!/usr/bin/env python
import os, subprocess
subprocess.call(['/usr/bin/python2.7', './custom_hooks/pre-receive.py'])
正如您所看到的,我们只是使用python安装从它调用了实际的预接收,而python安装在安装了gitlab的环境中。

对解决方案有一个提示,但它对我不起作用,因为bash预接收钩子在我的gitlab实例上不起作用,只有python钩子起作用。所以我创建了一个通用的预接收:

#!/usr/bin/env python
import os, subprocess
subprocess.call(['/usr/bin/python2.7', './custom_hooks/pre-receive.py'])
正如您所看到的,我们只是使用安装在gitlab环境中的python调用了实际的预接收