【问题标题】:Bluemix Dev Ops: Building a project with private git submodulesBluemix Dev Ops:使用私有 git 子模块构建项目
【发布时间】:2016-02-12 05:45:36
【问题描述】:

我想知道如何使用 IBM Bluemix Dev Ops Services 构建一个包含私有 git 子模块的项目。

在我的管道中,我有一个类型为“Shell Script”的“构建”工作:

#!/bin/bash
git submodule init
git submodule update --recursive

但我的子模块包含许多私有存储库,我得到:

Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

在我的本地机器上,我能够运行这些命令,因为我可以访问并且我正在使用我的密钥。我该怎么做才能让它在这里工作?我不想将我的私钥提交到 git 中。

我正在部署的应用程序的存储库托管在 GitHub 上。私有子模块托管在 BitBucket 上。

更新

我尝试在构建控制台中使用我的私钥,但它不起作用:

echo "... my private key ..." >> ~/.ssh/throwaway_key
chmod 400 ~/.ssh/throwaway_key
ssh-agent bash -c 'ssh-add ~/.ssh/throwaway_key; git submodule update --recursive'

因为我在 docker 容器内,它不工作吗?我必须更新/etc/ssh/ssh_config 吗?我无法在运行此作业的容器内访问它。

更新 2

我也试过了,没有成功:

echo "Host            bitbucket.org
    Hostname        bitbucket.org
    IdentityFile    ~/.ssh/throwaway_key
    IdentitiesOnly yes" >> ~/.ssh/config

【问题讨论】:

  • 您不能将您的密钥粘贴到构建 shell 脚本中并在那里提供吗?否则,Bluemix DevOps 不提供对 SSH 密钥的任何支持
  • @BenRondeau 我根据您的建议尝试了一些方法。你能看到我的更新并给我任何其他建议吗?
  • 你有没有错误输出?还是默默地失败了?
  • @BenRondeau 它给了我同样的错误,我没有访问权限。我在本地尝试了ssh-agent ... 解决方案,我可以确认它有效。我也试过编辑~/.ssh/config(请看我的另一个编辑)
  • 好的。在这一点上,我已经超出了我的深度。我会看看是否可以让我们 DevOps 团队的人加入。谢谢

标签: docker containers ibm-cloud pipeline devops


【解决方案1】:

我有类似的设置。我定义了一个 Checkout 作业,它的工作是重新提取源,在克隆 URL 中显式传递密码。完成后,子模块更新工作正常。这是脚本:

#!/bin/bash
git clone --recursive https://myname:$PASSWORD@hub.jazz.net/git/myname/my-project
cd my-project
git submodule update --remote

PASSWORD 被定义为环境属性选项卡上的安全属性。它有点笨重且非 DRY,但它实现了我想要的行为。

我使用 Checkout 作业作为 Build 作业的输入(我可能可以将其作为一项大工作来完成,但我希望能够直观地区分结帐和构建中的失败。)

【讨论】:

    【解决方案2】:

    这对我有用,使用 github deploy key for the repo where my submodule resides

        #!/bin/bash
    
        # Build the private key file from the secret environment setting $id_rsa_abacus_pylib
        echo "-----BEGIN OPENSSH PRIVATE KEY-----" > /home/pipeline/.ssh/id_rsa_abacus_pylib
        (echo $id_rsa_abacus_pylib | tr ' ' '\n' | grep -v -e "----" | grep -v -e OPENSSH | grep -v -e PRIVATE) >> /home/pipeline/.ssh/id_rsa_abacus_pylib
        echo "-----END OPENSSH PRIVATE KEY-----" >> /home/pipeline/.ssh/id_rsa_abacus_pylib
        chmod 400 /home/pipeline/.ssh/id_rsa_abacus_pylib
        #cat /home/pipeline/.ssh/id_rsa_abacus_pylib
    
        # Replace the SSH command used by git
        echo 'ssh -vvv -i ~/.ssh/id_rsa_abacus_pylib -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $*' > ssh
        chmod +x ssh
    
        GIT_SSH="./ssh" git submodule init
        GIT_SSH="./ssh" git submodule update --recursive
    

    在我最初的尝试中,该操作不起作用,因为我未能将 SSH 密钥从环境变量正确移动到文件中。

    它利用https://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use/868699#868699中的选项3

    【讨论】:

      【解决方案3】:

      这是一个简单的解决方案:

      1. 将构建阶段类型从“简单”更改为“Shell 脚本”。

      2. 将此添加到脚本中:

         git submodule init
         git config submodule.foo.url https://$USERNAME:$PASSWORD@github.com/foo.git
         git submodule update --recursive
        
      3. 在环境属性中添加USERNAMEPASSWORD

      信用:https://stackoverflow.com/a/7714592/1335313


      上述方法的问题是您需要在交付管道中为每个子模块进行设置。子模块更改时可能会产生问题。

      另一个可能的解决方案是在管道中设置 git 凭证存储:

      git config credential.helper store
      echo "https://LOGIN:${PAT}@github.com/path/to/submodule.git" > ~/.git-credentials
      

      信用: https://stackoverflow.com/a/63939958/1335313

      阅读更多:https://www.shellhacks.com/git-config-username-password-store-credentials/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-05
        • 2015-09-05
        • 2019-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多