Git jenkins,在shell脚本中使用ssh

Git jenkins,在shell脚本中使用ssh,git,jenkins,jenkins-plugins,Git,Jenkins,Jenkins Plugins,更新:2/7-见下面的答案 我似乎无法让git/ssh签出在脚本中工作 什么有效: 我的jenkins作业有一个SSH密钥 Jenkins(通过SSH)通过GIT插件从bitbucket中检出内容,没有问题 我的詹金斯工作是这样的: 詹金斯的工作基本上有两件事: 检查带有shell脚本的git模块(在linux机器上) 使用传递的参数执行git模块(即:参数化 建造) 脚本确实(或应该)完成了沙盒目录中的所有工作,这包括签出大约8个其他GIT模块,根据作为参数化构建的一部分传递的值选择分支等 我

更新:2/7-见下面的答案

我似乎无法让git/ssh签出在脚本中工作

什么有效:

我的jenkins作业有一个SSH密钥

Jenkins(通过SSH)通过GIT插件从bitbucket中检出内容,没有问题

我的詹金斯工作是这样的:

詹金斯的工作基本上有两件事:

  • 检查带有shell脚本的git模块(在linux机器上)

  • 使用传递的参数执行git模块(即:参数化 建造)

  • 脚本确实(或应该)完成了沙盒目录中的所有工作,这包括签出大约8个其他GIT模块,根据作为参数化构建的一部分传递的值选择分支等

    我知道的有用,我看到的问题在哪里

    我有SSH代理,并且已启用。我已经验证了代理是否存在,通过在脚本中使用SET命令,我可以看到SSH\u代理shell变量

    在jenkins作业结束时,我看到jenkins声明它正在杀死SSH代理

    詹金斯使用URLssh://git@someserver.com/path/to/repo.git-作为URL,我使用相同的URL

    签出-Jenkins使用许多命令来完成基本签出

    我的脚本使用了相当基本的:“git克隆URL目录”

    当Jenkins这样做时,它说它正在使用代理脚本中的密钥。 我的脚本运行时未看到此消息

    我已经看到很多东西建议使用Groovy脚本或管道作业等。对不起,这对我来说不起作用,我需要shell脚本来完成这项工作。原因:shell脚本用于开发人员环境,我们的签出过程涉及许多模块——GIT子模块对我来说不起作用

    我的shell脚本通过“构建步骤-执行shell”执行

    我的shell脚本可以看到代理,但由于某种原因,我不知道为什么/如何不能获得SSH密钥。我也不知道如何测试钥匙是否还在那里?也许詹金斯会把他们从探员那里删除

    生成/执行脚本是否支持SSH凭据

    (编辑2/6-添加显示问题的示例日志输出) 詹金斯日志输出

    [EnvInject] - Loading node environment variables.
    Building remotely on myslave - tiopbld (tiop_linux) in workspace /scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh
    [ssh-agent] Looking for ssh-agent implementation...
    [ssh-agent]   Exec ssh-agent (binary ssh-agent on a remote machine)
    $ ssh-agent
    SSH_AUTH_SOCK=/tmp/ssh-PRUTedOo0d7o/agent.16607
    SSH_AGENT_PID=16609
    [ssh-agent] Started.
    $ ssh-add /scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh@tmp/private_key_2048740533538871194.key
    Identity added: /scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh@tmp/private_key_2048740533538871194.key (/scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh@tmp/private_key_2048740533538871194.key)
    [ssh-agent] Using credentials tiopbld
    Cloning the remote Git repository
    Cloning repository ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git
     > git init /scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh # timeout=10
    Fetching upstream changes from ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git
     > git --version # timeout=10
    using GIT_SSH to set credentials jenkins-tiop-myuser
     > git fetch --tags --progress ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git +refs/heads/*:refs/remotes/origin/*
     > git config remote.origin.url ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git # timeout=10
     > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
     > git config remote.origin.url ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git # timeout=10
    Fetching upstream changes from ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git
    using GIT_SSH to set credentials jenkins-tiop-myuser
     > git fetch --tags --progress ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git +refs/heads/*:refs/remotes/origin/*
     > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
     > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
    Checking out Revision 5255667cfed01bdfcecc558a2c63bd6679503fc7 (refs/remotes/origin/master)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f 5255667cfed01bdfcecc558a2c63bd6679503fc7
    Commit message: "test commit from new linus machine"
    First time build. Skipping changelog.
    [shellscript_test.sh] $ /bin/sh -xe /tmp/jenkins2668814567868620035.sh
    + git clone ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git OUTPUTDIR
    Cloning into 'OUTPUTDIR'...
    Permission denied (publickey).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    Build step 'Execute shell' marked build as failure
    $ ssh-agent -k
    unsetenv SSH_AUTH_SOCK;
    unsetenv SSH_AGENT_PID;
    echo Agent pid 16609 killed;
    [ssh-agent] Stopped.
    Unable to connect to Elasticsearch service. Check Elasticsearch is running in the correct namespace
    Finished: FAILURE
    

    ```

    始终仔细检查您的SSH密钥名称。。。 确保他们的名字清楚。。。 例如,“jenkins主从密钥” 还有另一个“jenkins是gitrepo的奴隶”

    在我的例子中,一个字母不同的键名。
    (在上面)由于其他原因而被隐藏的密钥名称。

    此处是否需要密码短语保护私钥?否-无密码短语。注意:刚刚添加了显示问题的日志输出。很好+1.您能介绍一下凭证是如何设置的吗?谢谢