本文参照:https://blog.csdn.net/achuDk/article/details/78925081
但是有给出我在搭建中遇到的问题,和我解决的方法。现在我的配置是需要手动一键点击部署。后面再持续更新
大佬些,我相信这对你们来说都是很easy的。
cd /etc/yum.repos.d/
wget http://pkg.jenkins.io/redhat/jenkins.repo
rpm --import http://pkg.jenkins.io/redhat/jenkins.io.key
yum install -y jenkins
##启动jenkins服务
systemctl start jenkins
在浏览器打开输入http://IP:8080/访问jenkins。一般你会看到这样的页面
为了安全考虑,需要解锁jenkins,把/var/lib/jenkins/secrets/initialAdminPassword文件内容复制到administrator password下方即可。
[root@Jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
2862d2ffc9aa40e1b80039e8ad9810ea
选择安装的插件:只需要安装gitlab plugin,git plugin,还有ssh plugin插件即可,其他插件无须安装。
gitlab 和 git插件:我们的代码都放在gitlab上面,这两个插件用来做源码管理。
ssh插件:需要使用jenkins来执行shell命令
(1)jenkins生成密钥对
设置jenkins的shell终端为/bin/bash
root@node1 ~]# grep jenkins /etc/passwd
jenkins❌993:990:Jenkins Continuous Integration Server:/var/lib/jenkins:/bin/bash
正常的是出现像上面的那样,但是我这里出现了问题
解决办法:修改passwd
sudo vim /etc/passwd
最后一行改成:
jenkins:x:998:995:Jenkins Continuous Integration Server:/var/lib/jenkins:/bin/bash
再看:
grep jenkins /etc/passwd
这下就对了
然后登录到jenkins用户生成密钥:
su - jenkins
ssh-keygen -t rsa
cat .ssh/id_rsa.pub
把这个密钥添加到gitlab还有jenkins服务器的root用户
为什么把jenkins公钥添加到root用户?
因为jenkins执行部署操作,会出现很多权限问题,一个一个解决比较麻烦(而且不同项目权限问题都不同),就算sudo授权也是需要授予最少10几条命令的权限,而且不能保证以后不会出现新的命令,所以我直接让jenkins用户通过root方式执行部署操作,简单粗暴。
添加ssh key大家都会的,我就不写了、
添加公钥到jenkins服务器root用户,用来执行部署脚本
ssh-copy-id
-i .ssh/id_rsa.pub root@ip
root@ip's password:
(1)准备一个测试脚本,看开发如何提交代码就执行这个构建操作。
[root@slt3bkcqzaq shell]# vi depload.sh
kill -9 $pid
nohup java -jar mfe-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod&
scp /var/lib/jenkins/workspace/Metadata-Function-Enhance-test/target/mfe-0.0.1-SNAPSHOT.jar /usr/web/
pid=$(ps -ef | grep "mfe-0.0.1-SNAPSHOT.jar" | grep -v grep | awk '{print $2}')
echo $pid
if [ -z '$pid']
then
echo "stop"
else
echo "kill pid"
kill -9 $pid
echo "delete ex jar file"
rm -rf /usr/web/mfe-0.0.1-SNAPSHOT.jar
echo "scp jar file"
scp /var/lib/jenkins/workspace/Metadata-Function-Enhance-test/target/mfe-0.0.1-SNAPSHOT.jar /usr/web/
echo "source env"
source /etc/profile
echo "execute new jar file"
nohup java -jar mfe-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod&
fi
(2)创建一个触发构建的项目,选择构建maven项目
在往下源码管理,我遇到这样的问题:
Failed to connect to repository : Command "git ls-remote -h [email protected]:zengjian3/ludp-mtadata-management-view.git HEAD" returned status code 128:
-bash-4.2$ ssh-copy-id -i .ssh/id_rsa.pub [email protected]
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
The authenticity of host '10.122.47.95 (10.122.47.95)' can't be established.
ECDSA key fingerprint is SHA256:RHy25keGgWs+/k9ETi13P8KLjvaFS0P0kV1W9DLQgW8.
ECDSA key fingerprint is MD5:25:c5:04:8e:5f:16:21:f1:53:9d:b2:bb:00:36:0e:08.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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.
继续构建,选择execute shell,执行部署脚本即可,就是之前创建的脚本。
点击保存。回到主页可以看到刚刚的构建。点击这个按钮执行自动打包部署
点击按钮,查看控制台输出:
说明执行成功了。谢谢大家。
后面再更新更详细的版本。任何问题请comment。