如何将 git 子模块与 Bitbucket 管道一起使用?

Posted

技术标签:

【中文标题】如何将 git 子模块与 Bitbucket 管道一起使用?【英文标题】:How to use git submodules with Bitbucket pipelines? 【发布时间】:2019-04-06 21:32:58 【问题描述】:

如何在 Bitbucket 管道中使用 git 子模块?

我正在使用 Bitbucket 管道构建我的项目,但在拉入我的子模块时遇到问题,我可能没有正确配置 SSH 密钥。

我做了什么:

    在我的计算机中创建了 SSH 密钥对。 在设置/SSH 密钥下的两个存储库(将运行构建的存储库和依赖存储库)中粘贴了相同的密钥对。

构建错误:

Submodule 'dependencies/my-dependency' (git@bitbucket.org:mycompany/my-dependency.git) registered for path 'dependencies/my-dependency'
Cloning into 'dependencies/my-dependency'...
Warning: Permanently added the RSA host key for IP address '18.205.93.2' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Clone of 'git@bitbucket.org:mycompany/my-dependency.git' into submodule path 'dependencies/my-dependency' failed

我的 yml 文件

image:
  name: myuser/my-image-name
  username: $DOCKER_HUB_USERNAME
  password: $DOCKER_HUB_PASSWORD
  email: $DOCKER_HUB_EMAIL

pipelines:
  branches:
    pipelines-setup:
      - step:
          script:
            - git submodule update --init

【问题讨论】:

【参考方案1】:
    您的源存储库应包含文件 MyProject/.gitmodules 以及子模块的路径(.gitmodules 文件中的存储库路径应采用 git@bitbucket.org:....git 格式):
[submodule "modules"]
    path = modules
    url = git@bitbucket.org:....git

[submodule "translations"]
    path = translations
    url = git@bitbucket.org:....git
打开要在哪里运行管道的存储库 打开设置PIPELINES 部分打开 SSH 密钥 点击生成密钥 复制公共密钥

现在您需要将 ssh 密钥添加到子模块存储库

打开子模块存储库 打开设置GENERAL 部分打开 访问键 添加复制的 ssh 公钥

【讨论】:

在我的设置中,我无权访问子模块 repo 的“设置”(我只有读取权限),所以我必须将管道密钥添加到我的个人 SSH 密钥中——这很有效,虽然可能不理想? 想强调的是,.gitmodules 文件中的 repos 的路径应该是 git@bitbucket.org:....git 格式。【参考方案2】:

找到了解决办法。我必须将 ssh 公钥添加到 Settings / Access Keys not Settings / SSH Keys 下的子模块存储库。

【讨论】:

【参考方案3】:

这是另一个例子

使用默认图片 添加子模块 仅压缩所需文件并上传到 bitbucket 下载
image: atlassian/default-image:2
pipelines:
  default:
    - step:
        deployment: production
        script:
          - git submodule update --recursive --init
          - apt-get update && apt-get install -y zip
          - zip -r Test.zip . -x bitbucket-pipelines.yml *.git*
          - pipe: atlassian/bitbucket-upload-file:0.1.3
            variables:
              BITBUCKET_USERNAME: $BITBUCKET_USERNAME
              BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
              FILENAME: Test.zip

【讨论】:

【参考方案4】:

如果您已经在主存储库中初始化了一个子模块,那么您可以在 bitbucket 管道步骤脚本中添加此命令:

- git submodule update --init --recursive

无需使用 SSH 密钥方法。

【讨论】:

错了。如果不访问子模块,管道将无法读取它。您可以使用 ssh 密钥轻松访问,但如果您想向所有黑客发布您的所有秘密,也可以在 yml 中发布您的密码。

以上是关于如何将 git 子模块与 Bitbucket 管道一起使用?的主要内容,如果未能解决你的问题,请参考以下文章