报错如下

在这里插入图片描述

原因分析

运行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/bash

(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 jenkins@localhost.localdomain
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 root@192.168.20.107
/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
root@192.168.20.107's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.20.107'"
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 root@192.168.20.107:/tmp
ssh root@192.168.20.107 /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
Logo

鸿蒙生态一站式服务平台。

更多推荐