Bluemix Dev Ops:使用私有git子模块构建项目

时间:2016-02-12 05:45:36

标签: docker containers ibm-cloud pipeline devops

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

在我的管道中,我有一个'Build'作业,其类型为'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。

我正在部署的应用程序的repo托管在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

3 个答案:

答案 0 :(得分: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在“环境属性”选项卡上定义为“安全”属性。它有点笨重而且非干,但它启用了我想要的行为。

我使用Checkout作业作为Build作业的输入(我可能已经把它作为一项重要工作,但我希望能够在视觉上区分结账和构建中的失败。)

答案 1 :(得分:0)

这对我有用,使用了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的优势

答案 2 :(得分:0)

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

  1. 将Build阶段类型从“简单”更改为“ 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/

相关问题