jenkins ssh 代理无法将战争复制到远程服务器

Posted

技术标签:

【中文标题】jenkins ssh 代理无法将战争复制到远程服务器【英文标题】:jenkins ssh agent fail to copy war to remote server 【发布时间】:2021-09-16 23:04:48 【问题描述】:

我是 jenkin 的新手,我根据以下教程创建了一个 jenkinFile 来构建战争并将其复制到 tomcat 服务器上的远程机器上:

https://thenucleargeeks.com/2020/05/31/declarative-jenkins-pipeline-to-deploy-java-web-application/

请在下面找到我创建的 jenkinFile:

#!/usr/bin/env groovy


pipeline 
environment 
    NAME = readMavenPom().getArtifactId()

agent any

options 
    timeout(time: 1, unit: 'HOURS')
    buildDiscarder(logRotator(daysToKeepStr: '10', numToKeepStr: '5', artifactNumToKeepStr: '2'))

stages 

     stage("Git Checkout")
        steps
             git branch: 'develop',
              credentialsId: 'jenkins', url: 'https://gitlab.gov/ih/ih-por.git'
             
            

    stage('Maven build') 

        steps 

         sh "mvn clean package"
         sh "mv target/*.war target/UI.war"

        

    

                 stage("deploy-dev")
                     steps
                        sshagent(['user-id-tomcat-deployment']) 
                        sh """
                        scp -o StrictHostKeyChecking=no target/UI.war
                        root@192.168.1.000:/opt/tomcat/webapps/
                        ssh root@192.168.1.000 /opt/tomcat/bin/shutdown.sh
                        ssh root@192.168.1.000 /opt/tomcat/bin/startup.sh
                         """
                          
                        
            

我还在我的 jenkin 服务器上创建了添加私钥。

但是,当我在 jenkins 上构建项目时,ssh 代理会显示以下错误:

    [Pipeline]  (deploy-dev)
[Pipeline] sshagent (hide)
[ssh-agent] Using credentials root (This defines the credential to login remote server where tomcat is installed for deployment purpose)
[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-r2GnAq9cX2F8/agent.37244
SSH_AGENT_PID=37247
Running ssh-add (command line suppressed)
Identity added: /var/lib/jenkins/workspace/InfoHighway/portal-ui-deploy@tmp/private_key_6145932096571627059.key (root@localhost.localdomain)
[ssh-agent] Started.
[Pipeline] 
[Pipeline] sh
+ scp -o StrictHostKeyChecking=no target/portalUI.war
usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]
           [[user@]host1:]file1 ... [[user@]host2:]file2
[Pipeline] 
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 37247 killed;
[ssh-agent] Stopped.
[Pipeline] // sshagent
[Pipeline] 
[Pipeline] // stage
[Pipeline] 
[Pipeline] // timeout
[Pipeline] 
[Pipeline] // withEnv
[Pipeline] 
[Pipeline] // withEnv
[Pipeline] 
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

知道我做错了什么吗?

【问题讨论】:

【参考方案1】:

target/UI.war 之后有一个新行,因此scp 命令缺少目标参数。 尝试在一行中使用完整的 scp 命令运行它:

stage("deploy-dev")
     steps
          sshagent(['user-id-tomcat-deployment']) 
               sh """
               scp -o StrictHostKeyChecking=no target/UI.war root@192.168.1.000:/opt/tomcat/webapps/
               ssh root@192.168.1.000 /opt/tomcat/bin/shutdown.sh
               ssh root@192.168.1.000 /opt/tomcat/bin/startup.sh
               """
           
      

【讨论】:

以上是关于jenkins ssh 代理无法将战争复制到远程服务器的主要内容,如果未能解决你的问题,请参考以下文章

Bamboo 远程代理池

将文件复制到管道詹金斯中的远程窗口服务器

无法将 slave.jar 复制到 slave 上的“/home/jenkins”

Jenkins - 通过 SSH 插件发布:如何复制目录

jenkins打包发布到远程服务器配置

Jenkins SSH 插件/SSH 代理插件:异常:超时:未建立套接字