【发布时间】:2019-09-11 19:49:40
【问题描述】:
问题:在我的 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.
背景:在我将代码推送到“主”分支后,我的 Jenkins 作业由 github webhook 触发。 Jenkins 读取 repo 的 Jenkinsfile 并创建一个 Docker 代理来构建应用程序,然后将构建的文件部署到 EC2 容器。在部署阶段,我使用 Jenkins 的 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'
}
}
}
}
}
}
}
}```
【问题讨论】:
-
less /etc/passwd | grep jenkins检查是否有任何jenkins用户存在? -
在两台服务器上返回:[ec2-user@ip-xxx-xx-xx-xx ~]$ less /etc/passwd | grep jenkins jenkins:x:1001:1001::/var/lib/jenkins:/bin/bash。 (我已经删除了 ip)
标签: amazon-web-services docker jenkins amazon-ec2 ssh