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

问题描述

我是 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

知道我做错了什么吗?

解决方法

您在 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
               """
           }
      }
}