Docker Jenkins Ocean Blue无法在Pipeline中运行ssh scp

时间:2018-06-05 09:42:50

标签: docker jenkins ssh jenkins-pipeline scp

我已经用jenkins指令建造了一个Jenkins码头工具:

docker run -u root --rm -d -p 8080:8080 -p 50000:50000 -v /root/.ssh/id_rsa:/root/.ssh/id_rsa -v jenkins-home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkinsci/blueocean

它符合我的预期。

然后我设置了一个新的Pipeline作业,Jenkinsfile就像这样简单:

pipeline {
agent {
    docker {
        image 'maven:3-alpine'
        args '-v /root/.m2:/root/.m2'
    }
}
stages {
    stage('Build') {
        steps {
            sh 'mvn -B -DskipTests clean package'
        }
    }
    stage('Test') {
        steps {
            echo "Testing!"
        }

    } 
    stage('Deploy') {
        steps {
            sh "ssh docker@192.168.99.101 rm -rf /home/docker/wildfly_deployments/*" 
            sh "scp target/test-docker-app-1.0.war docker@192.168.99.101:/home/docker/wildfly_deployments/"
        }

    } 
}
}

当我在“Deploy”阶段运行此作业时,它会显示此错误消息

[test-docker-app] Running shell script
+ ssh docker@192.168.99.101 rm -rf /home/docker/wildfly_deployments/*
/var/jenkins_home/workspace/test-docker-app@tmp/durable-4cd12dd8/script.sh: line 1: ssh: not found
script returned exit code 127

我确定Jenkins容器可以执行ssh和scp,因为我试图在Jenkins容器中运行相同的命令,我工作了。

你知道我在Jenkinsfile中的错误吗?请帮忙

感谢。

===============================

我在jenkinsfile中发现了这个问题,应该是:

pipeline {
    agent any
    stages {
        stage('Build') {
            agent {
                docker {
                    image 'maven:3-alpine'
                    args '-v m2_repos:/root/.m2'
                }
            }
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
        stage('Test') {
            steps {
                echo "Testing!"
            }

        } 
        stage('Deploy') {
            steps {
                sh "ssh docker@192.168.99.101 rm -rf /home/docker/wildfly_deployments/*" 
                sh "scp target/test-docker-app-1.0.war docker@192.168.99.101:/home/docker/wildfly_deployments/"
                /*sh 'bash ./deploy.sh'*/
            }

        } 
    }
}

1 个答案:

答案 0 :(得分:0)

对我来说,理解的关键是名为“ Master”的构建执行器是blueocean jenkinsci FROM jenkinsci / blueocean映像的运行时“ Agent”上下文

在Jenkinsfile中,代理{标签'master'}表示在Master上运行步骤,代理{标签'Dockerfile'}表示在项目docker容器中运行步骤。

您需要一个多代理程序管道,因此从agent none开始,并在针对其代理程序环境的每个阶段添加一个代理程序。在这种情况下,agent { label 'master' }jenkinsci/blueocean容器

运行ssh

太明显了,任何人都无法解释...!

您将需要SSH代理插件来创建证书,并复制一个私密ssh密钥。它将为您提供一个ID,供您在Jenkinsfile DSL脚本中使用

pipeline {
  agent none

  stages {
    stage('Hello Docker') {
      agent { dockerfile {filename 'Dockerfile'}}
      steps {
        echo 'Hello Docker'
      }
    }

 stage('Make useful SSH connections') {  
      agent { label 'master' }
        steps{

             //Your in the in Master Container
             sh 'hostname'

             //In this working directory
             sh 'pwd'

             //You provide SSH credentails via the SSHagent 
             sshagent(credentials : ['<ID OF THE SSH AGENT CREDENTIAL>']) {

             //Connect to hostip/s with ssh, run scripts, go wild!
             sh 'ssh -o StrictHostKeyChecking=no user@hostip uptime'


             //and SCP 
             sh 'scp ./source/filename user@hostip:/remotehost/target 
            }
      }
}

Jenkins节点由代理标签cool :)

控制