简体   繁体   English

Gitlab CI & Django:如何使用 pip 安装自定义 package

[英]Gitlab CI & Django: How to install custom package with pip

I have a Django project that have many dependencies and among those are several custom private Django package listed in our requirements.txt file at the project root.我有一个 Django 项目,它有很多依赖项,其中有几个自定义私有项目 Django package 在项目根目录的requirements.txt文件中列出。

I want to setup simple CI that triggers our tests each time a commit is made.我想设置简单的 CI,在每次提交时触发我们的测试。
To do so I have written a simple .gitlab-ci.yaml file that tries to run those tests but I am having trouble installing our custom dependencies.为此,我编写了一个简单.gitlab-ci.yaml文件来尝试运行这些测试,但我在安装自定义依赖项时遇到了问题。
They are listed in our requirements like follow:它们列在我们的要求中,如下所示:

...
Django==3.2.12
...
-e git+ssh://git@gitlab.com/{organization}/{project}.git@{commit-sha}#egg={project}
-e git+ssh://git@gitlab.com/{organization}/{project}.git@{{commit-sha}#egg={project}
...

Note: All the mentionned projects lies under the same Gitlab organization注意:所有提到的项目都属于同一个 Gitlab 组织

Here is what my .gitlab-ci.yaml file looks like:这是我的.gitlab-ci.yaml文件的样子:

stages:
  - test

run-test: 
  image: ubuntu:18.04
  stage: test
  before_script: # installing python, pip & installing requirements
    - apt -y update

    - apt -y install apt-utils git net-tools
    - apt -y install python3.8 python3-pip
    - apt -y upgrade
    
    - python3 -m pip install --upgrade pip
    - cd servers/api
    - pip3 install -r ../requirements.txt
  script:
    - python3 manage.py test

This obviously fails giving the following error:这显然无法给出以下错误:

Obtaining {project} from git+ssh://****@gitlab.com/{organization}/{project}.git@{commit-sha}#egg={project} (from -r ../requirements.txt (line 32))
Cloning ssh://****@gitlab.com/{organization}/{project}.git (to revision {commit-sha}) to ./src/{project}
Running command git clone --filter=blob:none -q 'ssh://****@gitlab.com/{organization}/{project}.git' /builds/{organization}/platform/servers/api/src/{project}
  Host key verification failed.
  fatal: Could not read from remote repository.
  Please make sure you have the correct access rights
  and the repository exists.

Reading this topic from the Gitlab doc I have tried adding SSH key in the mix but it did not work either.从 Gitlab 文档中阅读此主题我尝试在组合中添加 SSH 密钥,但它也没有用。

I have also found this Gitlab issue that seems to talk about the same topic but it requires to create PyPi private package and I am not quite sure how to do it neither if I should我还发现了这个Gitlab 问题,它似乎在谈论相同的主题,但它需要创建 PyPi private package,我不太确定该怎么做,如果我应该的话

Any help is appreciated任何帮助表示赞赏

Fixing pip install over ssh修复 pip 安装超过 ssh

If you want to continue using ssh to install with pip, you'll need to fix the ssh host key verification issue.如果您想继续使用 ssh 与 pip 一起安装,则需要修复 ssh 主机密钥验证问题。

Host key verification failed.主机密钥验证失败。

You can fix this issue by setting GIT_SSH_OPTIONS to ignore host key verification.您可以通过将GIT_SSH_OPTIONS设置为忽略主机密钥验证来解决此问题。

before_script:
  - export GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

Of course, this is not ideal since you're no longer verifying the identity of the git server.当然,这并不理想,因为您不再验证 git 服务器的身份。

Alternatively if you don't want to skip host key verification, you can verify the host keys, as described here and add the host key for your server to the known_hosts file.或者,如果您不想跳过主机密钥验证,您可以按照此处所述验证主机密钥,并将服务器的主机密钥添加到 known_hosts 文件中。

You might also avoid host key issues altogether by using HTTPS instead of ssh, using HTTP basic auth with pip .您还可以通过使用 HTTPS 而不是 ssh,使用HTTP 基本身份验证和 pip来完全避免主机密钥问题。 That is to say use git+https instead of git+ssh .也就是说使用git+https而不是git+ssh

Use the package registry (recommended!)使用 package 注册表(推荐!)

As mentioned in the post you found in your question, GitLab has a PyPI package registry that allows you to publish Python packages as well as use them with pip. This will require you to publish the packages and setup the (additional) index url(s) in your pip configuration.正如您在问题中找到的帖子中所述,GitLab 有一个PyPI package 注册表,允许您发布 Python 包并将它们与 pip 一起使用。这将需要您发布包并设置(附加)索引 url ) 在您的 pip 配置中。 The documentation covers the setup and usage.该文档涵盖了设置和使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM