如何在Jenkins管道中使用SSH? [英] How do I use SSH in a Jenkins pipeline?

查看:1135
本文介绍了如何在Jenkins管道中使用SSH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用Jenkins管道模型定义定义的Jenkins作业,该定义建立了NPM项目.我使用Docker容器构建这些项目(使用带有 只是Node.js + npm + yarn).

I have some Jenkins jobs defined using a Jenkins Pipeline Model Definition, which builds NPM projects. I use Docker containers to build these projects (using a common image with just Node.js + npm + yarn).

构建的结果包含在我使用zip管道命令压缩的dist/文件夹中.

The results of the builds are contained in the dist/ folder that I zipped using a zip pipeline command.

我想使用SSH/SCP(使用私钥身份验证)将此ZIP文件复制到另一台服务器.我的私钥已添加到Jenkins环境(凭证管理器)中,但是当我使用Docker容器时,无法建立SSH连接.

I want to copy this ZIP file to another server using SSH/SCP (with private key authentication). My private key is added to the Jenkins environment (credentials manager), but when I use Docker containers, an SSH connection cannot be established.

我试图添加agent { label 'master' }以使用主要的Jenkins节点进行文件传输,但是似乎使用新的Git fetch创建了一个干净的工作空间,而没有我的内置文件.

I tried to add agent { label 'master' } to use the master Jenkins node for file transfer, but it seems to create a clean workspace with new Git fetch, and without my built files.

尝试了SSH代理插件后,我得到以下输出:

After I tried the SSH Agent Plugin, I have this output:

Identity added: /srv/jenkins3/workspace/myjob-TFD@tmp/private_key_370451445598243031.key (rsa w/o comment)
[ssh-agent] Started.
[myjob-TFD] Running shell script
+ scp -r dist test@myremotehost:/var/www/xxx
$ docker exec bfda17664965b14281eef8670b34f83e0ff60218b04cfa56ba3c0ab23d94d035 env SSH_AGENT_PID=1424 SSH_AUTH_SOCK=/tmp/ssh-k658r0O76Yqb/agent.1419 ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 1424 killed;
[ssh-agent] Stopped.
Host key verification failed.
lost connection

如何添加授权的远程主机?

How do I add a remote host as authorized?

推荐答案

我遇到了类似的问题.我没有使用标签"master",而当我这样做时,我发现文件传输可以跨从属进行:

I had a similar issue. I did not use label 'master', and I identified that the file transfer works across slaves, when I do it like this:

第1步-在远程主机服务器中创建SSH密钥,包括指向authorized_keys的密钥

Step 1 - create SSH keys in remote host server, include the key to authorized_keys

第2步-在Jenkins中使用SSH密钥创建凭据,使用远程主机上的私钥

Step 2 - Create credential using SSH keys in Jenkins, use the private key from the remote host

以下步骤已添加到管道中:

The below step is added to the pipeline:

stage ('Deploy') {
    steps{
        sshagent(credentials : ['use-the-id-from-credential-generated-by-jenkins']) {
            sh 'ssh -o StrictHostKeyChecking=no user@hostname.com uptime'
            sh 'ssh -v user@hostname.com'
            sh 'scp ./source/filename user@hostname.com:/remotehost/target'
        }
    }
}

这篇关于如何在Jenkins管道中使用SSH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆