catch22
catch22

Reputation: 1693

Connection to EC2 instance from Jenkins fails : Host key verification failed

I'm trying to automate a deployment with Jenkins to an EC2 instance for the first time.

I have installed tomcat8 in the EC2 instance and changed the permissions of the tomcat8/webapps folder to 777 ($ chmod 777 webapps).

The .ssh from EC2 is in the known_hosts file.

I'm able to connect and copy the .war file into the server folder using scp from my console but it fails during the automation.

$ scp -i /Users/Shared/Jenkins/aws.pem /Users/Shared/Jenkins/Home/jobs/fully-automated/builds/28/archive/webapp/target/webapp.war [email protected]:/var/lib/tomcat8/webapps

== copies the *.war file to tomcat8/webapps ==

In Jenkins, I am getting:

[Deploy to Staging] + scp -i /Users/Shared/Jenkins/aws.pem /Users/Shared/Jenkins/Home/jobs/fully-automated/builds/28/archive/webapp/target/webapp.war [email protected]:/var/lib/tomcat8/webapps
[Deploy to Staging] Host key verification failed.
[Deploy to Staging] lost connection

The command from the console and from the Groovy Jenkins file is the exact same. Why would it work from my machine and not from Jenkins?

Jenkinsfile:

pipeline {
  agent any

  tools {
    maven 'localMaven'
  }

  parameters {
    string(name: 'production', defaultValue: '54.93.78.130', description: 'Staging server')
    string(name: 'staging', defaultValue: '35.158.118.56', description: 'Production server')
  }

  triggers {
    pollSCM('* * * * *')
  }

  stages {
    stage('Build') {
      steps {
        sh 'mvn clean package'
      }
      post {
        success {
          echo 'Now Archiving...'
          archiveArtifacts artifacts: '**/target/*.war'
        }
      }
    }

    stage('Deployments') {
      parallel {
        stage('Deploy to Staging') {
          steps {
            sh "scp -i /Users/Shared/Jenkins/aws.pem /Users/Shared/Jenkins/Home/jobs/fully-automated/builds/28/archive/webapp/target/*.war ec2-user@${params.staging}:/var/lib/tomcat8/webapps"
          }
        }

        stage('Deploy to Production') {
          steps {
            sh "scp -i /Users/Shared/Jenkins/aws.pem /Users/Shared/Jenkins/Home/jobs/fully-automated/builds/28/archive/webapp/target/*.war ec2-user@${params.production}:/var/lib/tomcat8/webapps"
          }
        }
      }
    } 
  }
}

Thanks for your help!

Upvotes: 1

Views: 2013

Answers (1)

rohit thomas
rohit thomas

Reputation: 2312

This is a common mistake many people perform. You have given permission to your "USERNAME" to access EC2 not "JENKINS" user. Just do the same thing, but this time do it for Jenkins user.

Jenkins has its own user called "jenkins" which you can observe in users folder, create the ssh key here and pass this to EC2 and everything should work fine :)

For conformation, just ssh into the server using your username and give it a try with Jenkins username it will not work until you do it the above changes

Hope this helps :)

Upvotes: 2

Related Questions