[英]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.