Jenkins Level 4 MR Jobs

I have perform the below steps:
Install plugins for ssh, ssh credentials, ssh build agent, git, pipeline and docker and restart after install.
ssh into ststor01 with natasha, and replace index.html with Welcome to xFusionCorp Industries!
git add, commit, and push to dev branch, and check in gitea with sarah credentials.
add sarah, banner and natasha credentials in jenkins credentials.
configure system to add remote host of stapp03.
create new pipeline with script:

pipeline {
    agent any    
    stages {
        stage('Build') {
            steps {
	     	sh 'sshpass -p BigGr33n ssh -o StrictHostKeyChecking=no banner@stapp03'
                sh 'git clone http://git.stratos.xfusioncorp.com/sarah/mr_job.git'
		sh 'cd mr_job'
                sh 'docker build -t stregi01.stratos.xfusioncorp.com:5000/nginx:latest .'
                sh 'docker push stregi01.stratos.xfusioncorp.com:5000/nginx:latest'
            }
        }
        stage('Deploy') {
            steps {
	     	sh 'sshpass -p BigGr33n ssh -o StrictHostKeyChecking=no banner@stapp03'
                sh 'docker stop nginx-app'
                sh 'docker rm nginx-app'
                sh 'docker run -d --name nginx-app -p 8080:80 stregi01.stratos.xfusioncorp.com:5000/nginx:latest'
            }
        }
    }
}

And got the error message below:

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/nginx-container
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
+ sshpass -p BigGr33n@ ssh -o StrictHostKeyChecking=no banner@stapp03
Pseudo-terminal will not be allocated because stdin is not a terminal.
+ rm -rf mr_job
+ git clone http://git.stratos.xfusioncorp.com/sarah/mr_job.git
Cloning into 'mr_job'...
+ cd mr_job
+ docker build -t stregi01.stratos.xfusioncorp.com:5000/nginx:latest .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
Stage "Deploy" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

I have check that in stapp03, docker daemon is running.

Please advise where did i did wrong or missing any steps. Thanks

You’re not running the commands on stapp03.

Each sh call runs one command on the jenkins build server.

sh 'sshpass -p BigGr33n ssh -o StrictHostKeyChecking=no banner@stapp03'

will open the connection to stapp03 then abandon it because the command has ended. The remaining sh commands are executed on the build node, i.e. where jenkins is, which is not where docker is.

I haven’t done this lab. but I suspect what you need is this

sh """sshpass -p BigGr33n ssh -o StrictHostKeyChecking=no banner@stapp03 '
           git clone http://git.stratos.xfusioncorp.com/sarah/mr_job.git
           cd mr_job
           docker build -t stregi01.stratos.xfusioncorp.com:5000/nginx:latest .
           docker push stregi01.stratos.xfusioncorp.com:5000/nginx:latest'
   """
  • The triple quotes denote a multi-line sting in groovy.
  • The commands for SSH to execute remotely are enclosed in single quote.

So that yields one call to sh which executes the entire ssh transaction as a single command.

Hi Alistair,

Apologies, i pasted the wrong code, the latest one used is here:

pipeline {
    agent any    
    stages {
        stage('Build') {
            steps {
	     	sh '''sshpass -p BigGr33n ssh -o StrictHostKeyChecking=no banner@stapp03
                  git clone http://git.stratos.xfusioncorp.com/sarah/mr_job.git
		          cd mr_job
                  docker build -t stregi01.stratos.xfusioncorp.com:5000/nginx:latest .
                  docker push stregi01.stratos.xfusioncorp.com:5000/nginx:latest
               '''
            }
        }
        stage('Deploy') {
            steps {
	     	sh '''sshpass -p BigGr33n ssh -o StrictHostKeyChecking=no banner@stapp03
                  docker stop nginx-app
                  docker rm nginx-app
                  docker run -d --name nginx-app -p 8080:80 stregi01.stratos.xfusioncorp.com:5000/nginx:latest
               '''
            }
        }
    }
}

And not doubt it has the same result.
Note carefully where I put single-quotes and trible double-quotes

You have to quote the whole thing for sh and the commands that you expect ssh to execute must also be quoted as a group

image

Hi Alistair,

Thanks for the advise, the task is completed.

Thanks

1 Like