使用 Poetry 从 Github 安装多个私有包并部署密钥

Joa*_*him 7 python ssh github python-poetry deploy-keys

我想使用诗歌将多个私有 Github 存储库作为 python 包安装到我的存储库中。这在poetry install本地运行时效果很好,因为我将公共 SSH 密钥添加到 Github,这允许诗歌访问私有存储库。问题是我想在我的 CI/CD 管道中安装这些相同的私有包,为此我需要为每个 repo 添加一个部署密钥。正确的部署密钥需要用于正确的存储库,为了使其工作,我需要使用以下格式的一些别名设置一个配置(我实际上还没有知道这是否真的有效) ):

// /.ssh/config
Host repo-1.github.com
 IdentityFile ~/.ssh/repo-1-deploy-key
Host repo-2.github.com
 IdentityFile ~/.ssh/repo-2-deploy-key
Run Code Online (Sandbox Code Playgroud)

repo-1repo-2是私有仓库我需要安装的名称。在本地运行时,pyproject.toml需要按以下格式设置包:

// pyproject.toml
...
[tool.poetry.dependencies]
repo-1 = { git = "ssh://git@github.com/equinor/repo-1.git" }
repo-2 = { git = "ssh://git@github.com/equinor/repo-2.git" }
...
Run Code Online (Sandbox Code Playgroud)

因为这将允许开发人员在没有任何配置的情况下安装软件包(假设他们有权访问)。然而,对于 CI/CD 管道,URL 需要匹配 SSH 配置文件中的别名,因此它需要看起来像这样:

// pyproject.toml
...
[tool.poetry.dependencies]
repo-1 = { git = "ssh://git@repo-1.github.com/equinor/repo-1.git" }
repo-2 = { git = "ssh://git@repo-2.github.com/equinor/repo-2.git" }
...
Run Code Online (Sandbox Code Playgroud)

现在,我似乎陷入困境的是如何在同一个 pyproject 文件中包含两个不同的 git 路径?我尝试了以下方法:

//pyproject.toml

[tool.poetry.dependencies]
repo-1 = { git = "ssh://git@repo-1.github.com/equinor/repo-1.git", optional=true }
repo-2 = { git = "ssh://git@repo-2.github.com/equinor/repo-2.git", optional=true }

[tool.poetry.dev-dependencies]
repo-1 = { git = "ssh://git@repo-1.github.com/equinor/repo-1.git" }
repo-2 = { git = "ssh://git@repo-2.github.com/equinor/repo-2.git" }

[tool.poetry.extras]
cicd_modules = ["repo-1", "repo-2"]
Run Code Online (Sandbox Code Playgroud)

这样我就可以在poetry install本地运行,它会使用开发依赖项并poetry install --no-dev --extras cicd_modules在 CI/CD 管道中使用别名路径。可悲的是,这给了我一个,CalledProcessError因为尽管可选标志设置为 true,但似乎诗歌试图安装可选包。

我在这里做错了什么,我是否以某种方式错误地使用了可选标志?有没有更好的方法来解决这个问题?总而言之,我只是希望能够在 CI/CD 管道中使用诗歌和 Github 部署密钥将多个私有存储库安装为包,而不会破坏本地安装行为,如果以这种方式或其他更好的方式,我真的没有强烈的意见。

Sam*_*Sam 1

我认为这里的问题是您正在发明新主机(例如,repo-1.github.com),但这些主机名实际上并不存在,并且您没有将它们映射到正确的现有主机名(github.com)。

您的 .ssh/config 应该如下所示:

Host repo-1.github.com
 Hostname github.com
 IdentityFile ~/.ssh/repo-1-deploy-key
Host repo-2.github.com
 Hostname github.com
 IdentityFile ~/.ssh/repo-2-deploy-keyHost repo-0
Run Code Online (Sandbox Code Playgroud)

然后,您可以像使用真实主机一样使用 repo-1.github.com 和 repo-2.github.com ,它们将被路由到 github.com:

...
[tool.poetry.dependencies]
repo-1 = { git = "ssh://git@repo-1.github.com/equinor/repo-1.git" }
repo-2 = { git = "ssh://git@repo-2.github.com/equinor/repo-2.git" }
...
Run Code Online (Sandbox Code Playgroud)