gitlab ci& Django:如何使用PIP安装自定义软件包

发布于 2025-01-22 11:15:54 字数 2218 浏览 2 评论 0 原文

我有一个django项目,有很多依赖关系,其中包括我们 unignts.txt 文件中的几个自定义私人django软件包。

我想设置简单的CI,每次提交时都会触发我们的测试。
为此,我写了一个简单的 .gitlab-ci.yaml 试图运行这些测试的文件,但我在安装我们的自定义依赖项时遇到了麻烦。
它们在我们的要求中列出,如以下内容:

...
Django==3.2.12
...
-e git+ssh://[email protected]/{organization}/{project}.git@{commit-sha}#egg={project}
-e git+ssh://[email protected]/{organization}/{project}.git@{{commit-sha}#egg={project}
...

注意:所有提到的项目都位于同一gitlab组织下,

是我的 .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

这 显然会失败给出以下错误:

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.

阅读此主题在组合中添加SSH键,但也无法使用。

我还发现 this gitlab问题似乎是同样的话主题,但它需要创建PYPI私人包,我不太确定如果我有

任何帮助,我都不知道该怎么做

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.

I want to setup simple CI that triggers our tests each time a commit is made.
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.
They are listed in our requirements like follow:

...
Django==3.2.12
...
-e git+ssh://[email protected]/{organization}/{project}.git@{commit-sha}#egg={project}
-e git+ssh://[email protected]/{organization}/{project}.git@{{commit-sha}#egg={project}
...

Note: All the mentionned projects lies under the same Gitlab organization

Here is what my .gitlab-ci.yaml file looks like:

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.

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

Any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

櫻之舞 2025-01-29 11:15:54

修复PIP安装在SSH上,

如果您想继续使用SSH与PIP安装,则需要修复SSH主机密钥验证问题。

主机密钥验证失败。

您可以通过设置 git_ssh_options 来解决此问题,以忽略主机密钥验证。

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

当然,这不是理想的选择,因为您不再验证GIT服务器的身份。

另外,如果您不想跳过主机键验证,则可以验证主机键, ,并将服务器的主机密钥添加到已知的_host文件中。

您还可以使用 http Basic Auth具有PIP 。也就是说,使用 git+https 而不是 git+ssh

使用问题注册表(推荐!)

如您在问题中发现的帖子中所述,Gitlab具有 PYPI软件包注册表允许您发布Python软件包并将其与PIP一起使用。这将需要您发布软件包,并在PIP配置中设置(附加)索引URL。该文档涵盖了设置和用法。

Fixing pip install over ssh

If you want to continue using ssh to install with pip, you'll need to fix the ssh host key verification issue.

Host key verification failed.

You can fix this issue by setting GIT_SSH_OPTIONS to ignore host key verification.

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.

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.

You might also avoid host key issues altogether by using HTTPS instead of ssh, using HTTP basic auth with pip. That is to say use git+https instead of git+ssh.

Use the package registry (recommended!)

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. The documentation covers the setup and usage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文