Jenkins+主从+Pipeline+Webhook
jenkins安装
下载地址:https://pkg.jenkins.io/debian-stable/
sudo apt-get update apt install daemon sudo apt-get install openjdk-8-jdk java -version dpkg -i jenkins_2.204.6_all.deb systemctl enable --now jenkins
安装插件
登录后台,点击 Manage Jenkins --> Manage Plugins,搜索关键字Gitlab Hook Email
jenkins基本配置
!注意配置邮箱需要开启smtp和授权码
https://zhuanlan.zhihu.com/p/22810691
jenkins 主从配置
配置免密码
#主jenknis su jenkins ssh-keygen ssh-copy-id root@192.168.88.11
从jenkins安装jdk
apt install daemon sudo apt-get install openjdk-8-jdk
复制主jenkins私钥
https://www.cnblogs.com/jsonhc/p/7372359.html
Pipeline配置
安装2台tomcat用于测试
sudo apt-get install tomcat8 /etc/init.d/tomcat8 start mkdir -p /data/tomcat/test rm -rf /var/lib/tomcat8/webapps/* ln -s /data/tomcat/test/ROOT/ /var/lib/tomcat8/webapps/
需要提前开通gitlab账号密码
Jenkinsfile 提前上传到gitlab项目中
node("jekins-agent1") 表示在jenkins从执行任务
Jenkinsfile
node("jekins-agent1"){ stage("clone 代码"){ sh "cd /home/jenkins/workspace/test && rm -rf ./*" git credentialsId: '234d25df-a08d-46f6-a039-b7751e1944c3', url: 'http://192.168.88.10/root/test.git' } stage("代码构建"){ sh "cd /home/jenkins/workspace/test && tar czvf devops.tar.gz ./*" } stage("停止服务"){ sh 'ssh sdn@192.168.88.12 "sudo /etc/init.d/tomcat8 stop && sudo rm -rf /data/tomcat/test/*"' sh 'ssh sdn@192.168.88.13 "sudo /etc/init.d/tomcat8 stop && sudo rm -rf /data/tomcat/test/*"' } stage("代码copy"){ sh "cd /home/jenkins/workspace/test && scp devops.tar.gz sdn@192.168.88.12:/data/tomcat/test/" sh "cd /home/jenkins/workspace/test && scp devops.tar.gz sdn@192.168.88.13:/data/tomcat/test/" } stage("代码部署"){ sh 'ssh sdn@192.168.88.12 "cd /data/tomcat/test/ && tar xvf devops.tar.gz -C /data/tomcat/test/ && rm -rf devops.tar.gz"' sh 'ssh sdn@192.168.88.13 "cd /data/tomcat/test/ && tar xvf devops.tar.gz -C /data/tomcat/test/ && rm -rf devops.tar.gz"' } stage("启动服务"){ sh 'ssh sdn@192.168.88.12 "sudo /etc/init.d/tomcat8 start"' sh 'ssh sdn@192.168.88.13 "sudo /etc/init.d/tomcat8 start"' } }
webhook配置
生成 token 后 复制 URL token 到 gitlab
如下图返回200 OK
Pipeline 构建邮件通知
https://www.jianshu.com/p/c8fab60ed58e
Pipeline 脚本试和声明式
#脚本试 node { stage("clone 代码"){ sh "cd /var/lib/jenkins/workspace/pipline-linux40-app1-develop && rm -rf ./*" git branch: 'develop', credentialsId: '0792719f-b4fe-412a-a511-e8ecf60dd760', url: 'git@172.31.0.101:magedu/app1.git' echo "代码 clone完成" } stage("代码构建"){ sh "cd /var/lib/jenkins/workspace/pipline-linux40-app1-develop && tar czvf linux40.tar.gz ./*" } stage("停止服务"){ sh 'ssh www@172.31.0.106 "/etc/init.d/tomcat stop && rm -rf /data/tomcat/tomcat_webapps/linux40/*"' sh 'ssh www@172.31.0.107 "/etc/init.d/tomcat stop && rm -rf /data/tomcat/tomcat_webapps/linux40/*"' } stage("代码copy"){ sh "cd /var/lib/jenkins/workspace/pipline-linux40-app1-develop && scp linux40.tar.gz www@172.31.0.106:/data/tomcat/tomcat_appdir/" sh "cd /var/lib/jenkins/workspace/pipline-linux40-app1-develop && scp linux40.tar.gz www@172.31.0.107:/data/tomcat/tomcat_appdir/" } stage("代码部署"){ sh 'ssh www@172.31.0.106 "cd /data/tomcat/tomcat_appdir/ && tar xvf linux40.tar.gz -C /data/tomcat/tomcat_webapps/linux40/"' sh 'ssh www@172.31.0.107 "cd /data/tomcat/tomcat_appdir/ && tar xvf linux40.tar.gz -C /data/tomcat/tomcat_webapps/linux40/"' } stage("启动服务"){ sh 'ssh www@172.31.0.106 "/etc/init.d/tomcat start"' sh 'ssh www@172.31.0.107 "/etc/init.d/tomcat start"' } } https://www.jenkins.io/zh/doc/book/pipeline/syntax/ #声明式: pipeline{ //agent any //全局必须带有agent,表明此pipeline执行节点 agent { label 'jenkins-node1' } stages{ stage("代码clone"){ //#agent { label 'master' } //具体执行的步骤节点,非必须 steps{ sh "cd /var/lib/jenkins/workspace/pipline-test && rm -rf ./*" git branch: 'develop', credentialsId: '0792719f-b4fe-412a-a511-e8ecf60dd760', url: 'git@172.31.0.101:magedu/app1.git' echo "代码 clone完成" } } stage("代码构建"){ steps{ sh "cd /var/lib/jenkins/workspace/pipline-linux40-app1-develop && tar czvf linux40.tar.gz ./*" } } stage("停止服务"){ steps{ sh 'ssh www@172.31.0.106 "/etc/init.d/tomcat stop && rm -rf /data/tomcat/tomcat_webapps/linux40/*"' sh 'ssh www@172.31.0.107 "/etc/init.d/tomcat stop && rm -rf /data/tomcat/tomcat_webapps/linux40/*"' } } stage("代码copy"){ steps{ sh "cd /var/lib/jenkins/workspace/pipline-linux40-app1-develop && scp linux40.tar.gz www@172.31.0.106:/data/tomcat/tomcat_appdir/" sh "cd /var/lib/jenkins/workspace/pipline-linux40-app1-develop && scp linux40.tar.gz www@172.31.0.107:/data/tomcat/tomcat_appdir/" } } stage("代码部署"){ steps{ sh 'ssh www@172.31.0.106 "cd /data/tomcat/tomcat_appdir/ && tar xvf linux40.tar.gz -C /data/tomcat/tomcat_webapps/linux40/"' sh 'ssh www@172.31.0.107 "cd /data/tomcat/tomcat_appdir/ && tar xvf linux40.tar.gz -C /data/tomcat/tomcat_webapps/linux40/"' } } stage("启动服务"){ steps{ sh 'ssh www@172.31.0.106 "/etc/init.d/tomcat start"' sh 'ssh www@172.31.0.107 "/etc/init.d/tomcat start"' } } } }