簡體   English   中英

當 ssh 進入 EC2 時,Jenkins 無法創建目錄“/var/lib/jenkins/.ssh”

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

問題:在我的 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).

Host key verification failed.

背景:在我將代碼推送到“master”分支后,我的 Jenkins 工作由 github webhook 觸發。 Jenkins 讀取存儲庫的 Jenkinsfile 並創建一個 Docker 代理來構建應用程序,然后將構建的文件部署到 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'
               }
            }
          }
        }
      }
    }
  }
}```

你可以從你的 jenkins 服務器 ssh 你的 EC2s 嗎? 你能檢查一下嗎

在這里找到我的答案:

默認情況下,當未指定用戶時,docker 使用在 dockerfile 中定義的用戶啟動 [原文如此] 容器,如果未指定,則為 root。

我將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