通过版本号发布完成!
(6)Jenkins添加邮件报警
-设置管理员邮箱
-配置项目
#注释 ①Disable Extended Email Publisher:勾选后,邮件就不发送,看自己的情况喽,如果你想调试某些东西,又不想发邮件出去就可以勾选这个。 ②Project Recipient List:收件人地址;多个收件人邮件地址用逗号进行分割;想使用全局默认配置的话,可以使用$DEFAULT_RECIPIENTS。 ③Project Reply-To List:允许回复人的地址;想使用系统设置中的默认值的话,可以使用$DEFAULT_REPLYTO; ④Content Type:邮件文档的类型,可以设置HTML等格式; ⑤Default Subject:默认主题,也就是邮件标题;同理可以使用$DEFAULT_SUBJECT ⑥Default Content:默认邮件内容;这里是关键; ⑦Attach Build Log:发送的邮件是否包含日志; 下面几个默认就好,最后一个Triggers非常关键; 假设最后一个不改的话,邮件是接收不到的,这个是官方留下的一个大坑,一定要自己再添加一个Recipient List。 #需要在“增加构建后操作步骤”添加的 E-mail Notification Editable Email Notification
此时只需要构建项目时出错,就会发送邮件
收到邮件是这样的,其实就是构建时控制台的输出结果
(7)Jenkins远程管理部署节点
Jenkins远程管理的方式有三种:
- Shell ssh(免密)
- SSH Plugin (插件)
- ansible或saltstack
-Shell ssh的方法
- Jenkins在项目构建时,其实就是使用了Execute Shell直接对远程主机操作,同步了代码到目标主机上,并且可以对远程主机进行操作
- 下面直接修改项目的配置来达到效果
这里先把脚本删除,然后直接写两行命令,修改204web主机的网页页面
直接进行构建即可,无需管选项,因为已经把脚本删除了
成功构建成功
页面成功修改,并且收到了邮件,这是因为上面做的邮件报警功能
-SSH plugin的方法
第二种方法是使用SSH的插件,SSH plugin over ssh
修改项目配置
在web主机上去掉jenkins主机的公钥
******(1)删除密钥 [root@apache ~]# cd .ssh/ [root@apache .ssh]# ll 总用量 4 -rw------- 1 root root 398 7月 1 00:42 authorized_keys [root@apache .ssh]# rm -rf * [root@apache .ssh]# ll 总用量 0 ******(2)在jenkins主机测试 [root@jenkins ~]# ssh root@192.168.100.204 root@192.168.100.204's password: #需要输入密码就是删除密钥成功
在jenkins主机上点击构建,进行测试
利用SSH插件的方法实验成功!
-Jenkins配合Ansible的方法
******(1)在jenkins上安装ansible [root@jenkins ~]# ll 总用量 605968 -rwxr-xr-x 1 root root 114 7月 4 14:50 aaa.sh -rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg drwxr-xr-x 3 root root 4096 7月 5 15:39 ansible #上传ansible的yum库 -rw-r--r-- 1 root root 8842660 7月 2 09:29 apache-maven-3.5.4-bin.tar.gz -rwxr-xr-x 1 root root 274 7月 2 19:05 bbb.sh -rw-r--r-- 1 root root 6 7月 5 15:03 index.html -rw-r--r-- 1 root root 181367942 6月 30 22:11 jdk-8u91-linux-x64.tar.gz -rw-r--r-- 1 root root 66044542 6月 30 22:16 jenkins-2.222.4-1.1.noarch.rpm drwxr-xr-x 6 root root 99 7月 2 09:29 maven -rw-r--r-- 1 root root 122904706 7月 2 09:34 nexus-3.13.0-01-unix.tar.gz -rw-r--r-- 1 root root 241319636 6月 30 23:49 plugins.tar.gz drwxr-xr-x 3 root root 20 7月 2 09:34 sonatype-work drwxr-xr-x 3 root root 36 7月 4 14:46 test [root@jenkins ~]# vim /etc/yum.repos.d/centos.repo #修改yum文件 [aaa] name=aaa baseurl=file:///mnt enabled=1 gpgcheck=0 [ansible] name=ansible baseurl=file:///root/ansible enabled=1 gpgcheck=0 #保存退出 [root@jenkins ~]# yum -y install ansible 。。。。。。 完毕! ******(2)测试Ansible [root@jenkins ~]# cd /etc/ansible/ [root@jenkins ansible]# > hosts [root@jenkins ansible]# vim hosts [web] 192.168.100.204 #保存退出 [root@jenkins ansible]# ssh-copy-id 192.168.100.204 #重新给web主机上传公钥 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /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.100.204's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.100.204'" and check to make sure that only the key(s) you wanted were added. [root@jenkins ansible]# ansible web -m ping #测试ping模块 192.168.100.204 | SUCCESS => { "changed": false, "ping": "pong" } ******(3)新建一个测试目录和测试的剧本 [root@jenkins ansible]# cd [root@jenkins ~]# mkdir -p /data/ansible-playbook [root@jenkins ~]# vim /data/ansible-playbook/jenkins.yml --- - hosts: web remote_user: root gather_facts: no tasks: - name: 创建一个目录 file: path=/root/abc state=directory #保存退出 [root@jenkins ansible-playbook]# ansible-playbook -C jenkins.yml #测试剧本 PLAY [web] ****************************************************************************************************************************** TASK [创建一个目录] *************************************************************************************************************************** changed: [192.168.100.204] PLAY RECAP ****************************************************************************************************************************** 192.168.100.204 : ok=1 changed=1 unreachable=0 failed=0
配置jenkins,安装Ansible、Ansible Tower两个插件,同样的下载失败就手动下载
新建任务进行测试
查看执行成功的控制台,可以看到和剧本的输出信息一模一样
#在web主机上验证效果 [root@apache ~]# ll 总用量 186328 drwxr-xr-x 2 root root 6 7月 6 00:01 abc #成功创建 -rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg -rw-r--r-- 1 root root 9417469 7月 3 02:08 apache-tomcat-8.5.16.tar.gz -rw-r--r-- 1 root root 6 7月 5 23:30 index.html -rw-r--r-- 1 root root 181367942 7月 3 02:02 jdk-8u91-linux-x64.tar.gz
Jenkins配合Ansible成功!!!
(8)Jenkins集群
在业务量逐渐增大时,单台的jenkins进行自动化构建部署显然是不够灵活的,所以就引出的jenkins集群
jenkins集群并不是像web服务器、mysql集群那样做一些负载均衡和高可用,jenkins的集群只是去增加工作节点增加效率,并且jengkins的集群无需在额外的主机安装jenkins,只需要安装jdk即可,但是对于java项目来说,要使用集群功能就需要安装maven了
使用Jenkins集群需要安装的插件有:ssh、ssh agent、ssh build agents
添加节点之前需要先和Jenkins主机同步时间
-为节点主机和jenkins同步时间
增加一台主机作为Jenkins的节点:node1,ip:192.168.100.205,两块网卡,一块桥接网卡
******(1)在Jenkins安装ntpdate服务向阿里云同步时间 [root@jenkins ~]# yum -y install ntpdate 。。。。。。 完毕! [root@jenkins ~]# ntpdate ntp1.aliyun.com 6 Jul 17:21:03 ntpdate[1582]: adjust time server 120.25.115.20 offset -0.000628 sec [root@jenkins ~]# cat .ssh/id_rsa #先查看jenkins主机的私钥 -----BEGIN RSA PRIVATE KEY----- MIIEpgIBAAKCAQEA3FIIZI24n+7xRc5lH3lmqJ9PPlQxd9eLIvBWtMaJxN3RW6X6 9bc0i5pXWrpY/MgcF777Uu1yiVLoeeIKoEMnKK/09WqX8nz0ix7QqHmVmM2wArn5 vaXpyggNFEpGME5DkFPR6Tsde6fOZwTusnaOl89JfFATar6Mu/7Tu5iLJXIoJ4/q +poLJE8QqDpPbo8A0kN0Jn4S5nP+weMBklnJbxE2ckRsm/IN68nu/ten6TxDaatW GsHqIjQMrXW/iGQVY4HSE9JNj1YIFXWMZ7aANCLpQmMKJU9SnIyZZXEpuOyB7Qy+ CfAIDxjvGV3+6CTPrzLWUYh5VEYy5UM23AJzoQIDAQABAoIBAQCcVtW/zNVLIDoz Z89YM0SGuW149DOaL+h6pYNRimTuvty2JSTdQeDWg3IjXoKQ9csBirM62GXjTmLL Qnx51s0t0RaJI9eo6cR6CCbWtlhScvrIJII0ckxg4a+uSzpdQtwsyV32G83DFoOf lN9w4jIzRZOG1CfDIm4pZR2ITCnJKlRv+hL4gGDTEh3oFQHMJZwxA6qiwXqmRxJ+ jUNP9S2LHl2Zsz3QwTHCsdHUoJisLz348M9bjia5Kn7i/kJONdk3inxBTWhxX9sA j/GDTO++H4bklRgtJuB+hL7wpYuL5T/dK0VShtWTZVQQZTycPNsmBEf6XYYx37K2 07ZRyCjBAoGBAPNBuWYP1RgIShVBaQsEXjWdP+4orN9cDvvueMKroCkg7QGv2m0v SDmpKWkhLNurnd3QoqyW1Q1w0+iw1QOKmjmHxo95tu3ieNAQ5TOK8fAiEA996zpC DiADVjo0qMrUncf1taWCytmhSu0kPDOVsniy8BMlJdzTlgL5QJ+G55NJAoGBAOfc trZ9b9gI0bBG2Jx9khmY54RZReaJF2q21eR75YTB9myh9AWZ0a+7KukJoay7hk6b 2n3tKx05RV1xbXYM7QKQ2f6Xbvcc8gZbW6viWkrst3ic57stZ2F7Bdg6Q8gH4Wy8 vExvAutE8A7GMlHOjKfx6NByhj8I5jJoPWA84wWZAoGBAOn+AdokRqXsa17Q4btg Q0MtdS6hX9EnW56JgQ45S0Hk0zb5MpIuljoo95WR/PV9usiBBcog+OYJ7z14xB64 96t5aM3glc90NyRl7CPNdva3vV05kckjezTeINayit848GFFgKqqCniXx1wfq+0v BVNHRNoVtLsbqMRJgRkzCpVJAoGBAJDjCkYFInPUJvxJeShpC3f1ZY79nkGOH1Tv Bb/ucCFCd3OwzGUHltGCWzEedKoU0Lukhqc0IkHzvNXEgItJTn3Q+HgwNwYyT0cL pcBGBPhW7dtv6Rq9J6Hxl1hkKGECE6q4Uc5NfHx6KqyAy4b1VE3KTBqdxifzrq2H F/tgcNjZAoGBAMoF+HmoroSp0obSTn/CHK15wF7aZBB6clA9d0Ezz63PAd2yLMe3 dMfTpRHVKcMiDrCeYVj/XNjwhA+GXexq4h1WgGBBHuiRwiFP67p5p4Jlad7t6qP9 pnh+oFpRclAmJEJwkj45u5J8oUFeQ7AztILU3qWFMpaYoYLYSdAbkdQR -----END RSA PRIVATE KEY----- [root@jenkins ~]# ssh-copy-id 192.168.100.205 #传输公钥到node1主机 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.100.205 (192.168.100.205)' can't be established. ECDSA key fingerprint is SHA256:VhTZ5YxS5af2rHtfCvyc6ehXh3PD2A8KY2MyE6rHjiU. ECDSA key fingerprint is MD5:e8:41:d2:8a:7e:e9:a9:47:a3:f0:29:be:e9:6d:df:51. Are you sure you want to continue connecting (yes/no)? yes /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.100.205's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.100.205'" and check to make sure that only the key(s) you wanted were added. [root@jenkins local]# scp -r java root@192.168.100.205:/usr/local/ #传输java到node1 ******(2)在node1主机安装ntpdate同样也向阿里云同步时间 [root@Centos7 ~]# hostnamectl set-hostname node1 [root@Centos7 ~]# su [root@node1 ~]# yum -y install ntpdate 。。。。。。 完毕! [root@node1 ~]# ntpdate ntp1.aliyun.com 6 Jul 17:20:44 ntpdate[1162]: adjust time server 120.25.115.20 offset -0.001442 sec [root@node1 ~]# mkdir jenkins #创建节点目录 [root@node1 ~]# echo """ > JAVA_HOME=/usr/local/java > PATH=$JAVA_HOME/bin:$PATH > CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar > export PATH JAVA_HOME CLASSPATH """ >> /etc/profile #填加环境变量 [root@node1 ~]# source /etc/profile #使环境变量生效 [root@node1 ~]# java -version #查看java版本 java version "1.8.0_91" Java(TM) SE Runtime Environment (build 1.8.0_91-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
-配置jenkins
成功添加节点!,可以看到各节点的硬件地址
-分配任务到节点
可以把之前做的实验的配置先删除,留下git拉仓库文件就行
#在选择限制项目的运行节点时,可以使用正则来筛选,例如node* 等 #选择了限制项目的运行节点后,下面的构建操作等,脚本、剧本之类的都必须在指定的节点中存在才行 ******编写node1节点中的aaa.sh脚本 [root@node1 ~]# vim aaa.sh #!/bin/bash scp /root/jenkins/workspace/aaa/index.html root@192.168.100.204:/var/www/html/index.html #保存退出 [root@node1 ~]# chmod +x aaa.sh [root@node1 ~]# ssh-keygen -t rsa #生成密钥 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:mcAmNjk5OUzIQK4FvTJYZdTa4l3RtxWk3LOf1n0lyU4 root@node1 The key's randomart image is: +---[RSA 2048]----+ |+= +=. . .o. | |..=+ =. . ...o. | |.o..%o+ . .ooo | |=...oB...o .. + | |.o . o .S E .| | . . o o=| | .o=| | . .| | | +----[SHA256]-----+ [root@node1 ~]# ssh-copy-id 192.168.100.204 #传输公钥到web主机 /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /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 root@192.168.100.204's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.100.204'" and check to make sure that only the key(s) you wanted were added. ******查看仓库的文件内容 [root@jenkins ~]# rm -rf test/ [root@jenkins ~]# git clone git@192.168.100.202:root/test.git 正克隆到 'test'... remote: Enumerating objects: 67, done. remote: Counting objects: 100% (67/67), done. remote: Compressing objects: 100% (38/38), done. remote: Total 67 (delta 1), reused 60 (delta 1) 接收对象中: 100% (67/67), 7.51 KiB | 0 bytes/s, done. 处理 delta 中: 100% (1/1), done. [root@jenkins ~]# cd test/ [root@jenkins test]# cat index.html dddddd
-测试
先看一下204之前的页面
(9)jenkins流水线
在流水线任务项目构建的过程中会通过Pipeline来呈现每个构建阶段的细节信息
-创建流水线任务
-配置流水线任务并且测试
pipeline { agent any stages { stage("Hellp") { steps { echo "Hellp world" } } } } #有点像剧本,但是是使用pipeline流水线专用的语法来进行构建,stages中可以包含多个stage,stage()中的字符就像是剧本中的name标识,而echo "Hellp world"就是每个name标识执行的命令
可以看到执行完成之后会自动生成阶段视图
- 查看控制台输出信息
-通过gitlab来存放pipeline文件
- 先在gitlab上创建存放pipeline的仓库
#在jenkins主机上克隆gitlab刚刚创建的仓库 [root@jenkins ~]# git config --global user.name "Administrator" [root@jenkins ~]# git config --global user.email "1248873545@qq.com" [root@jenkins ~]# git clone git@192.168.100.202:root/pipeline-test.git 正克隆到 'pipeline-test'... warning: 您似乎克隆了一个空版本库。 [root@jenkins ~]# cd pipeline-test/ [root@jenkins pipeline-test]# git --bare init 初始化空的 Git 版本库于 /root/pipeline-test/ [root@jenkins pipeline-test]# vim jenkinsfile pipeline { agent any stages { stage("hello") { steps { echo "hello world" } } } } #保存退出 [root@jenkins pipeline-test]# git add jenkinsfile #添加到暂存区 [root@jenkins pipeline-test]# git commit -m "pipeline" #添加到本地仓库 [master(根提交) da10bee] pipeline 1 file changed, 8 insertions(+) create mode 100644 jenkinsfile [root@jenkins pipeline-test]# git push -u origin master #推送到远程仓库 Counting objects: 3, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 284 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@192.168.100.202:root/pipeline-test.git * [new branch] master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master。
- 为流水线项目进行配置
查看输出信息
- 使用Pipeline Syntax 生成git流水线脚本
有些linux命令在流水线的语法里是不支持的,可以使用流水线自带的工具进行翻译,自动生成相应的语法
-利用流水线发布从gitlab拉取项目进行发布
- 可以使用Pipeline Syntax 先生成要使用的语句
#生成的语法 git credentialsId: 'ae9ffc3a-85f4-49a1-8439-b602ce07f6ce', url: 'git@192.168.100.202:root/test.git' sh label: '', script: 'scp /var/lib/jenkins/workspace/test/index.html root@192.168.100.204' #编写jenkinsfile文件 [root@jenkins pipeline-test]# vim jenkinsfile #使用流水线拉下来的仓库还是会拉到workspace目录,目录名称还是项目名称 pipeline { agent any stages { stage("git") { steps { git credentialsId: 'ae9ffc3a-85f4-49a1-8439-b602ce07f6ce', url: 'git@192.168.100.202:root/test.git' } } stage("scp") { steps { sh label: '', script: 'scp /var/lib/jenkins/workspace/pipeline-test/index.html root@192.168.100.204:/var/www/html/index.html' } } } } #保存退出 [root@jenkins pipeline-test]# git add jenkinsfile [root@jenkins pipeline-test]# git commit -m "pip1" [master 4479c93] pip1 1 file changed, 17 insertions(+), 2 deletions(-) [root@jenkins pipeline-test]# git push -u origin master Counting objects: 5, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 456 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@192.168.100.202:root/pipeline-test.git 9a852c9..4479c93 master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master。 #重新编写gitlab库中的文件内容 [root@jenkins ~]# rm -rf test/ [root@jenkins ~]# git clone git@192.168.100.202:root/test.git 正克隆到 'test'... remote: Enumerating objects: 67, done. remote: Counting objects: 100% (67/67), done. remote: Compressing objects: 100% (38/38), done. remote: Total 67 (delta 1), reused 60 (delta 1) 接收对象中: 100% (67/67), 7.51 KiB | 0 bytes/s, done. 处理 delta 中: 100% (1/1), done. [root@jenkins ~]# cd test/ [root@jenkins test]# ll 总用量 4 -rw-r--r-- 1 root root 7 7月 7 10:02 index.html [root@jenkins test]# cat index.html dddddd [root@jenkins test]# echo "<h1>pipeline</h1>" > index.html [root@jenkins test]# cat index.html <h1>pipeline</h1> [root@jenkins test]# git add index.html [root@jenkins test]# git commit -m "add 1" [master 2d6b532] add 1 1 file changed, 1 insertion(+), 1 deletion(-) [root@jenkins test]# git push -u origin master Counting objects: 5, done. Writing objects: 100% (3/3), 252 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@192.168.100.202:root/test.git 5b934c3..2d6b532 master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master。
构建项目
(10)部署Blue Ocean
等待安装完成即可,也可以手动下载,然后上传文件即可,手动上传需要传二十多个文件