gitlab pre-receive hook python导入错误
gitlab pre-receive hook python import error
以下是我的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)
我看了这两个帖子:How can I run a virtualenv python script as a git pre-commit hook and ,他们似乎暗示环境变量是问题的原因。
所以我修改了我的 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'
但是这些文章没有提供的是我应该如何使用预接收 githook 修改环境以便能够导入 gitlab?
对解决方案有提示,但它对我不起作用,因为 bash 预接收挂钩仅在我的 gitlab 实例上不起作用 python .所以我创建了一个通用的预接收:
#!/usr/bin/env python
import os, subprocess
subprocess.call(['/usr/bin/python2.7', './custom_hooks/pre-receive.py'])
如您所见,只是使用 python 的安装调用了它的实际预接收,该安装在安装了 gitlab 的环境中工作。
以下是我的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)
我看了这两个帖子:How can I run a virtualenv python script as a git pre-commit hook and
所以我修改了我的 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'
但是这些文章没有提供的是我应该如何使用预接收 githook 修改环境以便能够导入 gitlab?
#!/usr/bin/env python
import os, subprocess
subprocess.call(['/usr/bin/python2.7', './custom_hooks/pre-receive.py'])
如您所见,只是使用 python 的安装调用了它的实际预接收,该安装在安装了 gitlab 的环境中工作。