Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Jenkins Blue Ocean can not run ssh scp in Pipeline

I have built a Jenkins docker from jenkins instructions like this:

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

It works as my expected.

Then I setup a new Pipeline job, the Jenkinsfile simple like this:

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 [email protected] rm -rf /home/docker/wildfly_deployments/*" 
            sh "scp target/test-docker-app-1.0.war [email protected]:/home/docker/wildfly_deployments/"
        }

    } 
}
}

When I run this job, at stage "Deploy", it show me this error message

[test-docker-app] Running shell script
+ ssh [email protected] 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

I sure that the Jenkins container can execute ssh and scp because I have tried to run the same those commands inside the Jenkins container, I worked.

Do you know what I a wrong in Jenkinsfile ? Please help

Thanks.

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

I found the issue in my jenkinsfile, it should be:

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 [email protected] rm -rf /home/docker/wildfly_deployments/*" 
                sh "scp target/test-docker-app-1.0.war [email protected]:/home/docker/wildfly_deployments/"
                /*sh 'bash ./deploy.sh'*/
            }

        } 
    }
}
like image 323
Lap Tran Avatar asked Jun 05 '18 09:06

Lap Tran


1 Answers

For me the key to understanding was that the Build-Executor named "Master" is the runtime "Agent" context for the blueocean jenkinsci FROM jenkinsci/blueocean image

In the Jenkinsfile saying agent { label 'master' } means run steps on Master, agent { label 'Dockerfile' } means run steps in the projects docker container.

You need a multi agent pipeline, so start with agent none and add an agent in each stage targeting its agent enviroment. in this case agent { label 'master' } to run ssh from the jenkinsci/blueocean container

Too obvious for anyone to explain ... !

You'll need the SSH Agent Plugin to create a credential, and copy in a private ssh key. It will give you an ID to use in your Jenkinsfile DSL script

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 nodes are controlled by the agent label, cool :)

like image 53
Kickaha Avatar answered Oct 16 '22 20:10

Kickaha