jenkins部署任务提示“Host key verification failed.”

报错如下

在这里插入图片描述

原因分析

运行Jenkins的用户是程序用户jenkins,但是程序用户jenkins是非正常用户

[root@localhost ~]# ps -elf | grep java
0 S jenkins    1890      1  8  80   0 - 1195702 futex_ 14:06 ?      00:04:30 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
0 R root       4139   2605  0  80   0 - 28163 -      14:56 pts/0    00:00:00 grep --color=auto java

非正常主要体现在登陆shell异常 如下

[root@localhost ~]# grep "jenkins" /etc/passwd
jenkins:x:988:983:Jenkins Automation Server:/var/lib/jenkins:/bin/false

解决办法

(1)改变jenkins用户的登陆shell

[root@localhost ~]# vim /etc/passwd
jenkins:x:988:983:Jenkins Automation Server:/var/lib/jenkins:/bin/bas
h

(2)切换jenkins用户 测试

[root@localhost ~]# su jenkins
bash-4.2$ exit
exit

(3)重新定义jenkins用户的命令提示符

[root@localhost ~]# vim .bash_profile 
末行追加
export PS1='[\u@\h \W]\$ '
[root@localhost ~]# source .bash_profile 

(4)再次切换到jenkins用户测试

[root@localhost ~]# su jenkins
[jenkins@localhost root]$ exit
exit

(5)jenkins用户对目标主机做免密登陆

[root@localhost ~]# su jenkins
[jenkins@localhost root]$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
9b:7a:bd:bf:5d:c5:67:70:e5:20:17:b6:09:a6:29:af [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|            + =..|
|           + = =.|
|        . o   + o|
|         o     + |
|        S .     =|
|         +     .o|
|        E.      .|
|       .. .  . . |
|      ..  .oo..  |
+-----------------+
[jenkins@localhost root]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

(6)再次执行任务 SUCCESS

在这里插入图片描述
在这里插入图片描述

自动部署的脚本

[root@localhost ~]# cat /tmp/autodeploy.sh 
#!/bin/bash
WARFILE=/var/lib/jenkins/workspace/game-of-life/gameoflife-web/target/gameoflife.war
scp $WARFILE [email protected]:/tmp
ssh [email protected] /tmp/deploy.sh

[root@localhost ~]# cat /tmp/deploy.sh 
#!/bin/bash
DAY=/backup/$(date +%Y%m%d)
HOUR=$DAY/$(date +%H%M%S)
APPBASE=/usr/local/tomcat/webapps/gameoflife
WAR=/tmp/gameoflife.war
[ -d /backup ] || mkdir /backup
[ -d $DAY ] || mkdir $DAY
if [ -d $APPBASE ]
  then 
      mkdir $HOUR
      mv $APPBASE/* $HOUR
fi

unzip $WAR -d $APPBASE
发布了67 篇原创文章 · 获赞 56 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43557605/article/details/103804426