git-jenkins阶段04 jenkins自由风格项目(发布, 自动触发器, 返回状态给gitlab), maven项目, pipeline流水线项目
点击创建一个新任务
#类型: 自由风格项目: php,c,python项目 maven项目:要编译的,像java项目 pipeline:流水线操作
创建一个自由风格项目,如图
项目配置中,可以选择丢弃旧的构建,只保存7天的构建记录,最多保留10个构建记录。(可配可不配)
构建一般只用shell就够了,这里选择,写pwd,看在哪操作的
点击保存
点击构建,蓝色圆球表示成功,红色表示失败,如图
点击控制台输出,如图
红框为执行输出,也就是执行路径(所有的家目录),如图
#jenkins创建的项目家目录都在这里 [root@jenkins plugins]# cd /var/lib/jenkins/workspace/dazhuanpan-job [root@jenkins dazhuanpan-job]# ls [root@jenkins dazhuanpan-job]# ll /var/lib/jenkins/workspace total 0 drwxr-xr-x 2 root root 6 Jun 13 00:28 dazhuanpan-job
#从gitee上找开源项目,搜索幸运大转盘,复制https下载路径 项目地址:https://gitee.com/wjfbuild/wheel-of-fortune-
gitlab上根据地址进行导入项目,如图
#克隆一个新的环境web01,10.0.0.84,作为web端,把服务上线上去 #在web01上准备环境 #安装nginx [root@web01 ~]# vim /etc/yum.repos.d/nginx.repo #从官网复制的配置 [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [root@web01 ~]# yum install -y nginx [root@web01 ~]# systemctl start nginx #删除默认的配置文件 [root@web01 ~]# ll /etc/nginx/conf.d/ total 4 -rw-r--r-- 1 root root 1072 May 30 03:06 default.conf [root@web01 ~]# rm -rf /etc/nginx/conf.d/default.conf [root@web01 ~]# vim /etc/nginx/conf.d/dazhuanpan.conf server { listen 80; server_name localhost; location / { root /code/; index index.html; } } [root@web01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful #把项目通过https方式下载下来 [root@web01 ~]# mkdir /code [root@web01 ~]# git clone http://10.0.0.81/OPS/dzp.git Cloning into 'dzp'... Username for 'http://10.0.0.81': root Password for 'http://root@10.0.0.81': remote: Counting objects: 19, done. remote: Compressing objects: 100% (19/19), done. remote: Total 19 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (19/19), done. #把项目内容拷出到外部 [root@web01 code]# mv dzp/* ./ [root@web01 code]# rm -rf dzp #在浏览器中访问,看大转盘是否正常显示 http://10.0.0.84/
#开发修改源代码,将其推送到gitlab [root@dev01 ~]# git clone git@10.0.0.81:OPS/dzp.git Cloning into 'dzp'... remote: Counting objects: 19, done. remote: Compressing objects: 100% (19/19), done. remote: Total 19 (delta 0), reused 0 (delta 0) Receiving objects: 100% (19/19), 863.40 KiB | 0 bytes/s, done. [root@dev01 dzp]# vim index.html [root@dev01 dzp]# git commit -am "modify index.html 1" [master c3e6a50] modify index.html 1 1 file changed, 2 insertions(+), 2 deletions(-) [root@dev01 dzp]# git push -u origin master Counting objects: 5, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 353 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) To git@10.0.0.81:OPS/dzp.git 730984d..c3e6a50 master -> master Branch master set up to track remote branch master from origin.
#在jenkens机器上创建秘钥对,放到的部署key中 [root@jenkins ~]# 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. ... [root@jenkins ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1y...mxTVLUAHe5ynifqdSKgkLdv root@jenkins Gitlab上点击dzp项目,点击setting,点击Repository,点击Deploy Keys复制公钥,进行添加
Jenkens上进入项目,点击配置,点击源码管理,点击git
输入下载地址显示报错,蓝框命令异常,如图
#把报错命令,在jenkens服务器上执行这条命令(有一次交互,会生成一个东西),确保jenkens有git命令 [root@jenkins ~]# git ls-remote -h git@10.0.0.81:OPS/dzp.git The authenticity of host '10.0.0.81 (10.0.0.81)' can't be established. ECDSA key fingerprint is SHA256:2SQdzwgUOhQNWwPZEZSV9NaJ2i3tS2GgUaCazKOyOew. ECDSA key fingerprint is MD5:24:68:a9:38:f3:00:b4:7b:8a:72:0f:2a:c9:77:82:e0. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '10.0.0.81' (ECDSA) to the list of known hosts. c3e6a50779de94e19ba043133a0d6476dd6cf396 refs/heads/master
再次复制下载地址到源码管理,就不再报错,如图
点击构建
#在jenkens服务器上查看,代码是否拉取下来 [root@jenkins ~]# ll /var/lib/jenkins/workspace/dazhuanpan-job/ total 8 drwxr-xr-x 2 root root 185 Jun 13 02:54 images -rw-r--r-- 1 root root 6205 Jun 13 02:54 index.html drwxr-xr-x 2 root root 47 Jun 13 02:54 js
#jenkins内部变量(jenkins的shell中可以直接调用) $WORKSPACE #工作目录 /var/lib/jenkins/workspace/dazhuanpan-job $BUILD_NUMBER #构建序号 7 #在jenkins服务器上创建一个脚本目录,统一管理 [root@jenkins ~]# mkdir -p /server/scripts [root@jenkins ~]# vim deploy.sh #!/bin/bash Web_Ip=10.0.0.84 BUILD_NUMBER=$(( $BUILD_NUMBER - 1 )) Old_Code_Dir=${WORKSPACE##*/}_$BUILD_NUMBER ssh root@$Web_Ip "[ -d /opt/$Old_Code_Dir ] || mkdir -p /opt/$Old_Code_Dir" #1.将旧的代码进行移动到别的目录 ssh root@$Web_Ip "mv /code/* /opt/$Old_Code_Dir" #2.将代码拷贝到站点目录 scp -rp $WORKSPACE/* root@${Web_Ip}:/code ----------------------------------- #分发公钥给web服务器,为了ssh免密传输 [root@jenkins scripts]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.84
把脚本放到jenkins执行shell中,如图
点立即构建
#在web服务器端查看 [root@web01 opt]# ll /opt/ total 0 drwxr-xr-x 4 root root 48 Jun 13 05:16 dazhuanpan-job_5 [root@web01 code]# ll /code total 8 drwxr-xr-x 2 root root 185 Jun 13 02:54 images -rw-r--r-- 1 root root 6205 Jun 13 02:54 index.html drwxr-xr-x 2 root root 47 Jun 13 02:54 js #浏览器查看,幸运大转盘是否已修改 http://10.0.0.84/
允许分支里点击通过名字过滤分支, 里面include和exclude不用写。token点击生成,复制token,如图
点击保存
gitlab进入dzp项目,点击左侧栏的Settings,点击Integrations,输入上面jenkins复制的Service Url和token,如图
点击下方的add webhook
点击下面的Test,点Push events,推送一次测试,如图
显示200表示推送成功了,如图
#开发用户进行修改代码测试 [root@dev01 dzp]# vim index.html [root@dev01 dzp]# git commit -am "modify index.html father back" [master 63ea9e5] modify index.html father back 1 file changed, 1 insertion(+), 1 deletion(-) [root@dev01 dzp]# git push -u origin master ...
#把jenkins的构建结果返回给gitlab
在gitlab以root用户,点击settings,如图
点击Access Tokens,Name随便写,点击api。因为jenkins要调用gitlab的api来同通知,点击create personal access token,如图
复制生成的token,如图
进入jenkins网页,进入首页,点击系统管理,点击系统设置,如图
选择api token,api token写入上面jenkins生成的token。ID和Description随便写。点击Add如图
然后在 Credentials 选择 GitLab API token
点击Test Connection进行测试,如图,出现Sucess,说明测试成功
点击页面最下面的保存按钮
#在jenkins上点击立即构建 #在gitlab首页查看,如图,看到勾说明构建成功。点击勾,能看到pipeline信息,如图
maven #编译器 #安装maven [root@jenkins ~]# wget https://archive.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz [root@jenkins ~]# tar xf apache-maven-3.3.9-bin.tar.gz [root@jenkins ~]# mv apache-maven-3.3.9/ /usr/local/maven-3.3.9 [root@jenkins ~]# ln -s /usr/local/maven-3.3.9/ /usr/local/maven #把mvn命令加入环境变量 [root@jenkins ~]# echo "export PATH=/usr/local/maven/bin/:$PATH" >> /etc/profile [root@jenkins ~]# source /etc/profile [root@jenkins ~]# mvn -v Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00) Maven home: /usr/local/maven Java version: 1.8.0_181, vendor: Oracle Corporation Java home: /usr/java/jdk1.8.0_181-amd64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix" #安装tomcat #创建虚拟机tomcat-10.0.0.85 [root@tomcat ~]# rz jdk-8u181-linux-x64.rpm [root@tomcat ~]# wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.27/bin/apache-tomcat-8.0.27.tar.gz apache-tomcat-8.0.27.tar.gz #安装jdk [root@tomcat ~]# rpm -ivh jdk-8u181-linux-x64.rpm #安装tomcat [root@tomcat ~]# mkdir /application #企业中一般会将tomcat放在该路径下 [root@tomcat ~]# tar xf apache-tomcat-8.0.27.tar.gz -C /application [root@tomcat ~]# ln -s /application/apache-tomcat-8.0.27/ /application/tomcat #加速启动tomcat的配置 [root@tomcat ~]# vim /usr/java/jdk1.8.0_181-amd64/jre/lib/security/java.security securerandom.source=file:/dev/urandom #117行random前面加个u #启动tomcat [root@tomcat ~]# /application/tomcat/bin/startup.sh Using CATALINA_BASE: /application/tomcat Using CATALINA_HOME: /application/tomcat Using CATALINA_TMPDIR: /application/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar Tomcat started. #8080端口是tomcat的 [root@tomcat ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 6127/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6925/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7275/master tcp6 0 0 :::8009 :::* LISTEN 9648/java tcp6 0 0 :::111 :::* LISTEN 6127/rpcbind tcp6 0 0 :::8080 :::* LISTEN 9648/java tcp6 0 0 :::22 :::* LISTEN 6925/sshd tcp6 0 0 ::1:25 :::* LISTEN 7275/master tcp6 0 0 127.0.0.1:8005 :::* LISTEN 9648/java #浏览器上面访问 http://10.0.0.85:8080/
#dev01服务器(开发环境) #装java [root@dev01 ~]# rz jdk-8u181-linux-x64.rpm [root@dev01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm #装maven [root@dev01 ~]# rz apache-maven-3.3.9-bin.tar.gz [root@dev01 ~]# tar xf apache-maven-3.3.9-bin.tar.gz [root@dev01 ~]# mv apache-maven-3.3.9/ /usr/local/maven-3.3.9 [root@dev01 ~]# ln -s /usr/local/maven-3.3.9 /usr/local/maven [root@dev01 ~]# echo "export PATH=/usr/local/maven/bin:$PATH" >>/etc/profile [root@dev01 ~]# source /etc/profile #检查安装是否成功 [root@dev01 ~]# mvn -v #maven使用 test #测试 package #打包编译 clean #清楚上一次的编译缓存 jeesns的java项目包参考: https://blog.csdn.net/qq_37510195/article/details/133377071 #解压jeesns [root@dev01 ~]# rz jeesns.tar.gz [root@dev01 ~]# tar xf jeesns.tar.gz #移除之前的远程仓库 [root@dev01 jeesns]# git remote origin [root@dev01 jeesns]# git remote remove origin #把sql数据传到tomcat服务器上 [root@dev01 jeesns]# ll jeesns-web/database/ total 40 -rwxr-xr-x 1 root root 28667 Nov 19 2018 jeesns.sql -rw-r--r-- 1 root root 3491 Nov 19 2018 update_1.2.0to1.2.1.sql -rw-r--r-- 1 root root 1026 Nov 19 2018 update_1.2.1to1.3.sql -rw-r--r-- 1 root root 1344 Nov 19 2018 update_1.3to1.3.1.sql [root@dev01 jeesns]# scp -rp jeesns-web/database/jeesns.sql root@10.0.0.85: #tomcat服务器 #安装数据库mariadb [root@tomcat ~]# yum install -y mariadb-server #启动mariadb [root@tomcat ~]# systemctl start mariadb.service #设置密码(一定要用这个密码) [root@tomcat ~]# mysqladmin password "root" #登录 [root@tomcat ~]# mysql -uroot -proot MariaDB [(none)]> create database jeesns; Query OK, 1 row affected (0.00 sec) #导入库中 [root@tomcat ~]# mysql -uroot -proot jeesns < jeesns.sql #查看 [root@tomcat ~]# mysql -uroot -proot MariaDB [(none)]> use jeesns; MariaDB [jeesns]> show tables; +---------------------------+ | Tables_in_jeesns | +---------------------------+ | tbl_action | | tbl_action_log | ... #dev01服务器 [root@dev01 jeesns]# pwd /root/jeesns #进行编译 (时间久,要到后台中央数据仓库同步下载) [root@dev01 jeesns]# mvn package #编译好,在web下会有一个target文件夹,里面会有一个war包。就要这个war包 [root@dev01 jeesns]# ll jeesns-web/target/ total 25496 drwxr-xr-x 4 root root 181 Jun 14 01:12 classes drwxr-xr-x 3 root root 25 Jun 14 01:12 generated-sources drwxr-xr-x 5 root root 104 Jun 14 01:12 jeesns-web -rw-r--r-- 1 root root 26106011 Jun 14 01:12 jeesns-web.war drwxr-xr-x 2 root root 28 Jun 14 01:12 maven-archiver drwxr-xr-x 3 root root 35 Jun 14 01:12 maven-status #把war包传给tomcat服务器 [root@dev01 jeesns]# scp -rp jeesns-web/target/jeesns-web.war root@10.0.0.85:/application/tomcat/webapps/ROOT.war #浏览器,tomcat页面刷新(自动变为jeesns界面) http://10.0.0.85:8080/ 账户:admin 密码:jeesns
#gitlab上创建新的项目,OPS组,名字就叫jeesns
#dev01服务器 [root@dev01 jeesns]# pwd /root/jeesns #清空原来的编译(jeesns-web/target会删掉) [root@dev01 jeesns]# mvn clean #每次使用前都要清空,防止受之前的编译影响 #添加远程仓库 [root@dev01 jeesns]# git remote add origin git@10.0.0.81:OPS/jeesns.git #上传仓库 [root@dev01 jeesns]# git add . [root@dev01 jeesns]# git commit -m "one commit" #这里第一次提交没有权限,改dev01用户的权限再次提交 [root@dev01 jeesns]# git push -u origin master
在jenkins上,新建项目,如图
创建一个maven项目,如图
配置里General,点击丢弃旧的构建,保持构建天数7,保留最大构建个数7
这里可以不用再创建部署key,只要点击enable,就能让已有的key兼容多个项目。如图
enkins配置里,源码管理,git输入下载地址,如图,不再报错(以为上面的部署key兼容该项目)
如图,Build栏输入maven要执行的命令clean package。点击保存。上面Maven版本报错,等下处理
进入jenkins首页左侧系统管理里,点击全局工具配置,如图
往下拉,找到Maven,点击新增Maven,Name随便写,这里就写maven,把自动安装去掉
maven家目录,填入/usr/local/maven,点击新增maven,如图
点击save保存。再回到jenkins项目配置里,Build栏不再报Maven版本错误
#jenkins上mmavn-jeesns-job项目,点击立即编译(因为要官网下载包所以时间比较久) #第一次打包慢,后面编译会快(下载过的包会被记录) [root@jenkins local]# ll /var/lib/jenkins/workspace/ total 0 drwxr-xr-x 5 root root 60 Jun 13 06:25 dazhuanpan-job drwxr-xr-x 10 root root 226 Jun 14 04:00 maven-jeesns-job #编译成功,里面会有target,要里面的war包 [root@jenkins local]# ll /var/lib/jenkins/workspace/maven-jeesns-job/jeesns-web/target/ total 25496 drwxr-xr-x 4 root root 181 Jun 14 04:18 classes drwxr-xr-x 3 root root 25 Jun 14 04:18 generated-sources drwxr-xr-x 5 root root 104 Jun 14 04:19 jeesns-web -rw-r--r-- 1 root root 26106030 Jun 14 04:19 jeesns-web.war drwxr-xr-x 2 root root 28 Jun 14 04:19 maven-archiver drwxr-xr-x 3 root root 35 Jun 14 04:18 maven-status #编写发布脚本,把war包发送到tomcat主机上线 [root@jenkins local]# vim /server/scripts/jeesns_deploy.sh #!/bin/bash Web_Ip=10.0.0.85 Code_Dir=/application/tomcat/webapps Old_Code_Dir=${WORKSPACE##*/}_$(($BUILD_NUMBER - 1 )) #1.判断目录是否存在 ssh root@$Web_Ip "[ -d /opt/$Old_Code_Dir ] || mkdir -p /opt/$Old_Code_Dir" #2.将旧的war包进行移动保存 ssh root@$Web_Ip "mv $Code_Dir/ROOT.war /opt/$Old_Code_Dir" #3.将新编译的war包进行存送到站点目录 scp -rp $WORKSPACE/jeesns-web/target/jeesns-web.war root@$Web_Ip:$Code_Dir/ROOT.war ------------------------------------------ #jenkins服务器把key传给tomcat主机服务器,否则脚本文件无法免密传递 [root@jenkins local]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.85 #jenkins进入项目配置,在Post Steps下,点Execute shell,输入脚本,然后点击保存 /bin/bash /server/scripts/jeesns_deploy.sh
#点击立即构建,jeesns页面属性还能登录说明发版成功了 http://10.0.0.85:8080/ [root@tomcat opt]# ls maven-jeesns-job_6 [root@tomcat webapps]# ll total 25500 drwxr-xr-x 14 root root 4096 Jun 13 21:28 docs drwxr-xr-x 6 root root 83 Jun 13 21:28 examples drwxr-xr-x 5 root root 87 Jun 13 21:28 host-manager drwxr-xr-x 5 root root 103 Jun 13 21:28 manager drwxr-xr-x 5 root root 104 Jun 14 05:01 ROOT -rw-r--r-- 1 root root 26106054 Jun 14 05:01 ROOT.war
CI/CD的概念 持续集成 开发 持续部署 测试 运维 持续交付 运维 Pipeline 流水线模式 构建 测试 发布 #jenkins首页点击新建Item创建项目,点击Pipeline类型。名称叫pipeline-job,点击确定 #在配置栏里,点击上方Pipeline,输入pipeline脚本(脚本不用写,用的都是同样的) pipeline{ agent any stages{ stage("get code"){ steps { echo 'get code from scm' } } stage("package"){ steps { echo 'package code' } } stage("deploy"){ steps{ echo 'deploy package to node1' } } } }
#点击立即构建
如图,看到3个过程,哪个过程出错,直接能看到
#用大转盘项目进行pipeline 选择pipeline script from SCM,选git,输入大转盘项目地址。 脚本路径Jenkinsfile记住,后面要用,保存
#在gitlab的大转盘项目下添加 Jenkinsfile的文件
#如上图,写入jenkins项目中的Jenkinsfile,点提交,实现真正的发布功能 pipeline{ agent any stages{ stage("get code"){ steps { echo 'get code' } } stage("unit test"){ steps { echo 'unit test' } } stage("package"){ steps { echo 'package code' } } stage("deploy"){ steps{ sh '/bin/bash /server/scripts/deploy.sh' #sh是pipeline的命令,里面是自己的命令 } } } } #下面为了方便测试,防止干扰,把jenkins的大转盘配置中构建触发器取消 #在开发dev01服务器里修改代码 [root@dev01 dzp]# vim index.html #浏览器查看大转盘 http://10.0.0.84/ #jenkins的pipeline项目点击构建,如下图 #浏览器查看大转盘(更改代码已生效) http://10.0.0.84/