目录
在jenkins上部署maven
配置maven
jenkins回滚
Git方式回滚
通过发布时备份,回滚是选择指定备份文件进行回滚
jenkins添加邮件报警
Jenkins如何远程管理部署节点
Jenkins的远程管理方式:
SSH Plugin
Publish over SSH
jenkins整合Ansible
jenkins集群
#解包
tar zxvf apache-maven-3.9.1-bin.tar.gz
mv apache-maven-3.9.1 maven
ln -s /root/maven/bin/mvn /usr/bin/mvn
Manage Jenkins --->Global Tool Configuration
上传java项目
百度网盘链接:https://pan.baidu.com/s/1YoMfGMF1MtBFNz3Lb2sNtQ?pwd=2r9x
提取码:2r9x
git add .
git commit -m "add java"
git push origin main
创建新项目
编写脚本
vim /root/tomcat/update.sh
#!/bin/bash
source /etc/profile
scp /var/lib/jenkins/workspace/maven/test1/target/test1.war [email protected]:/root/tomcat
ssh [email protected] "docker exec -it a502a881e225 ./bin/shutdown.sh"
sleep 3
ssh [email protected] "docker start a502a881e225"
chmod +x tomcat_update.sh
#在docker服务器
mkdir /root/tomcat
docker run -itd -p 8080:8080 -v /root/tomcat/:/usr/local/tomcat/webapps --name=tomcat tomcat:10.1.7
上一篇文章的分支回滚太low了,采用下面两种方法
第一种:
Git可以通过代码提交的commit ID号,就可以回到之前的状态,需要安装gogs插件
这些就是commit id号
#克隆
git clone [email protected]:liuyuhao/test.git
cd test
vim index.html
lzz v1 !!!
#上传
[root@localhost test]# git add .
[root@localhost test]# git commit -m "add v1"
[main df2589b] add v1
1 file changed, 1 insertion(+), 1 deletion(-)
[root@localhost test]# git push origin main
Counting objects: 5, done.
Writing objects: 100% (3/3), 243 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:liuyuhao/test.git
b022061..df2589b main -> main
在jenkins配置项目,项目还是上一章的
在Gogs Webhook 中√上This project is parameterized选项,添加Choice Parameter和
String Parameter
添加Build Steps
#$ststus必须和上面Choice Parameter的名称一样
#!/bin/bash
case $status in
Rollback)
echo"----本次操作为回滚,版本号为$version-------"
git reset --hard $version
echo "------回滚完成,打包中"
;;
*)
exit
esac
改完后保存,构建
开始构建
回滚
选择这种方式的好处:回滚java代码时需要maven编译,maven非常慢,构建以后在回滚还需要重新构建。通过这种方式可以解决这个问题。
更改脚本
case $status in
Deploy)
echo "----发布前进行打包备份----"
echo "status:$status"
BACK_DIR=/var/lib/jenkins/workspace
path="${BACK_DIR}/bak"
if [ -d $path ]
then
echo "The files is already exists"
else
mkdir -p $path
fi
cd ${BACK_DIR}/${JOB_NAME}
#${JOB_NAME}=项目名称
#${BUILD_NUMBER}=第几次构建
tar czf ${JOB_NAME}-${BUILD_NUMBER}.tar.gz *
mv ${JOB_NAME}-${BUILD_NUMBER}.tar.gz ${BACK_DIR}/bak
scp ${WORKSPACE}/* [email protected]:/var/www/html/
echo "Completin"
;;
Rollback)
BACK_DIR=/var/lib/jenkins/workspace
cd ${BACK_DIR}/bak
tar zxvf ${JOB_NAME}-${version}.tar.gz
scp ${BACK_DIR}/bak/index.html [email protected]:/var/www/html/
rm -rf ${JOB_NAME}
;;
*)
exit
;;
esac
删除
保存构建
再次构建
回滚到v1,即回到第65次构建
在Manage Jenkins --->Configure System 找到 Jenkins Location和邮件通知
在项目配置里,在最后找到构建后操作,添加E-mail Notification和Editable Email Notification
构建测试,将脚本随便修改一下就行,比如ip换了
Shell ssh
SSH Plugin
ansible、saltstack
安装ssh插件
然后在Manage Jenkins --->Configure System找到 SSH remote hosts
在项目配置里,删掉邮件报警,在Build Steps里添加Execute shell script on remote host using ssh
然后构建,在控制台输出
在Manage Jenkins --->Configure System找到 Publish over SSH
在项目配置里,删掉Gogs Webhook和Build Steps里的内容,在构建后操作里加Send build artifacts over SSH
改完构建,看控制台输出的内容
#安装ansible
[root@localhost ~]#yum -y install ansible
[root@localhost ~]#cd /etc/ansible/
[root@localhost ansible]#>hosts
[root@localhost ansible]#vim hosts
[client]
192.168.100.5 #gitlabip
[root@localhost ~]#ansible client -m ping
192.168.100.5 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@localhost ~]#mkdir -p /data/Ansible-playbook
[root@localhost ~]#vim /data/Ansible-playbook/jenkins-mkdir.yml
---
- hosts: client
gather_facts: no
tasks:
- name: 创建alex目录
file: path=/data/alex state=directory
jenkins上装插件:ansible、ansible Tower,装完以后构建一个新项目
直接构建
再开一台机子,安装jenkins
安装过程看GitLab与jekins结合构建持续集成(cl)环境(2)
安装插件:ssh、ssh agent 、publish Over SSHversion
配置环境变量
vim /etc/profile
export MAVEN_HOME=/root/maven
export PATH=$PATH:$MAVEN_HOME
export JAVA_HOME=:/usr/local/java
source /etc/profile
在Manage Jenkins --->configure global security修改代理端口
添加节点
在100.5需要
[root@localhost ~]# ssh-copy-id [email protected]
如果连接失败,在主节点服务器上
[root@localhost ~]#ssh-keygen -t rsa
[root@localhost .ssh]#ssh-copy-id [email protected]
[root@localhost ~]#cat .ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA2ykFPeRSDsyZ2HpvaJaWsluycRnIxu8/OkpMXRQmWKamTDBI
D2t7+W8RrjSPfy0pwjQ4TVdg0bodiGdZx9orbLOuRzBVBsRCsGKlTUtqC9ccHpih
P46MVJ89NhaVT2NX6Oo74sOtUL5+lZN0JUY58r6zTcvcr7DpCyt+XIKro8JtIefc
3Dst/ktMQekRNmDLSRAUINJj3QyHaNsA5VJcJk/iC2OjRceDtb1EwDDivymRYEHZ
fp7kZefZMYXTcC0Wq3gHEGPtESCny2I59tArT9bYux6h/q5R5pgy1XUoJL5uutzU
GB2ZB0TIDMg7zXZNDcQ7Msylcun8dtG+8jjtkwIDAQABAoIBAH8Jry0qPVqwxPkU
N6IzsFThDHW9EYInSspaKRvHQ1cd+sWtMwglMO7ssB3//7IjltIo/P3wCYJo1Ret
eQB8+bEAEqTXYmISv+mItPMyJN2KKpWdudKiqbuftpa0zoX/OwUbSe1vqfZlBfFk
UO4OYCbS3hLZeiopbVz5MD0lzaKVTXZ0U6hmCdcD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ATE KEY-----
将这一串复制到
gitlab上的秘钥也需要换
让项目在指定节点运行
保存构建。如下图是以node01节点构建的
如果失败的话,可以试试将node01和网站服务器之间设置免密登录