简体   繁体   English

当 ssh 进入 EC2 时,Jenkins 无法创建目录“/var/lib/jenkins/.ssh”

[英]Jenkins unable to create directory '/var/lib/jenkins/.ssh' when ssh into EC2

Problem: During my Jenkins process I am able to establish a connection with the EC2 instance I want to copy files to but I keep getting the following errors:问题:在我的 Jenkins 过程中,我能够与我想将文件复制到的 EC2 实例建立连接,但我不断收到以下错误:

Could not create directory '/var/lib/jenkins/.ssh'

Failed to add the host to the list of known hosts (/var/lib/jenkins/.ssh/known_hosts).

and

Host key verification failed.

Background: My Jenkins job is triggered by a github webhook after I push code to 'master' branch.背景:在我将代码推送到“master”分支后,我的 Jenkins 工作由 github webhook 触发。 Jenkins reads the repo's Jenkinsfile and creates a Docker agent to build the app and then deploy the built files to an EC2 container. Jenkins 读取存储库的 Jenkinsfile 并创建一个 Docker 代理来构建应用程序,然后将构建的文件部署到 EC2 容器。 During the deploy phase I use Jenkin's sshagent to establish a connection and then use commands to delete the old files and then copy the new files to the EC2.在部署阶段,我使用 Jenkin 的 sshagent 建立连接,然后使用命令删除旧文件,然后将新文件复制到 EC2。

pipeline {
  agent {
    docker {
      image 'node:buster'
      args '-p 20001-20100:3000'
      args '-v /etc/passwd:/etc/passwd'
    }
  }
   environment {
    CI = 'true'
    HOME = '.'
    npm_config_cache = 'npm-cache'
  }
  stages {
    stage('Install') {
      ...install code... <<<<<<<[works, no issues]   
    stage('Build') {
      ...build code... <<<<<<<[works, no issues]   
    }
    stage('Deploy') {
      parallel {
        stage('Deploy frontend') {
        ...deploy frontend code to S3 bucket... <<<<<<<[works, no issues]   
        }

        stage('Deploy backend') {
          steps {
            dir('backend') {
               sshagent(['code_commit_key']) {
                 sh 'ssh -o StrictHostKeyChecking=no ec2-user@ecx-xx-xx-x-xx.compute-1.amazonaws.com "whoami"' <<<<<[this return ec2-user after list of errors]
                 sh 'ssh -o StrictHostKeyChecking=no ec2-user@ecx-xx-xx-x-xx.compute-1.amazonaws.com "sudo su -; pm2 delete -s order-form-nestjs; rm -rf ./dist"' <<<<<[this returns list of errors]
                 sh 'scp -r ./dist/* ec2-user@ecx-xx-xx-x-xx.compute-1.amazonaws.com:/home/ec2-user' [this returns list of errors]
                 sh 'ssh -o StrictHostKeyChecking=no  ec2-user@ecx-xx-xx-x-xx.compute-1.amazonaws.com "sudo su -; pm2 start dist/main.js --name=backend-app-nestjs"' <<<<<[this returns list of errors]
                 echo 'Ssh successful'
               }
            }
          }
        }
      }
    }
  }
}```

Can you ssh your EC2s from your jenkins server?你可以从你的 jenkins 服务器 ssh 你的 EC2s 吗? Can you check你能检查一下吗

Found my answer here :在这里找到我的答案:

By default, when the user is not specified, docker lauches [ sic ] the container with the user defined in the dockerfile which if not specified is root.默认情况下,当未指定用户时,docker 使用在 dockerfile 中定义的用户启动 [原文如此] 容器,如果未指定,则为 root。

I added args '-u root:root -v /var/lib/jenkins/workspace/myworkspace:/tmp/' + ' -v /var/lib/jenkins/.ssh:/root/.ssh' to my docker agent code, and viola, success!:我将args '-u root:root -v /var/lib/jenkins/workspace/myworkspace:/tmp/' + ' -v /var/lib/jenkins/.ssh:/root/.ssh'到我的 docker 代理代码和中提琴,成功!:

agent {
    docker {
      image 'node:buster'
      args '-p 20001-20100:3000'
      args '-v /etc/passwd:/etc/passwd -v /etc/group:/etc/group'
      args '-u root:root -v /var/lib/jenkins/workspace/myworkspace:/tmp/' + ' -v /var/lib/jenkins/.ssh:/root/.ssh'
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM