以下是我的githook:
import os
print 'hi from draj pre-receive githook'
import gitlab, subprocess
当我进入我的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)
我查看了这两个帖子:How can I run a virtualenv python script as a git pre-commit hook和remote: ImportError: No module named gitlab,他们似乎认为环境变量是导致问题的原因。
所以我修改了我的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]'}
但是,当我尝试执行一个调用pre-receive hook的push时,我得到了:
(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'
但那些文章没有提供的是我应该如何使用pre-receive githook修改环境以便能够导入gitlab?
答案 0 :(得分:0)
remote: ImportError: No module named gitlab对解决方案有一些暗示,但它对我不起作用,因为bash pre-receive hooks并没有在我的gitlab实例上工作,只有python的实例。所以我创建了一个通用的预接收:
#!/usr/bin/env python
import os, subprocess
subprocess.call(['/usr/bin/python2.7', './custom_hooks/pre-receive.py'])
正如你所看到的那样,只是使用安装了gitlab的环境中的python安装来调用它的实际预接收。