注意:本片配置:jenkins服务器和应用服务器不在同一台机器上,且应用服务器 负责流执行程 (推荐)。 流程:代码上传->gitlab通知->jenkins服务器执行构建->远程应用服务器git下拉->远程应用服务器maven打包->发送到远程应用服务器的tomcat->发布html
如下是配置说明: 须要两台服务器:一台部署jenkins服务器、另外一台部署应用服务器。 要求:两台服务器能够互相通讯。 部署jenkins服务器(Linux1): jdk、jenkins、tomcat(可选,但不推荐) 部署应用服务器(Linux2): jdk、git、maven、tomcatjava
说明一下:部署的jenkins服务器能够是内网本地,应用服务器能够是外网环境的云服务器。Jenkins也是能够实现手动或轮询的方式构建到应用服务器的,但,不能使用gitlab通知jenkins自动构建,因,gitlab找不到你的内网地址。python
yum install java
复制代码
前提环境:jdk1.8 下载地址:jenkins.war 稳定版linux
[root@VM_0_11_centos ~]# ls
jdk1.8 jdk-8u221-linux-x64.tar.gz jenkins.war
[root@VM_0_11_centos ~]# java -jar jenkins.war
复制代码
访问:ip:8080 http://49.235.71.163:8080 建立用户:admin/admin 用户名:zhengjagit
Safe Restart 方便重启jenkins,每次配置jenkins须要重启让配置生效 Rebuilder 方便从新构建 Git Parameters 肯定构建哪一个分支github
1.去掉 容许用户注册 2.选用 安全矩阵 ->Add user or group... ->输入刚建立的admin,赋予所有权限,保存web
环境: jdk1.8,git,maven,tomcat8spring
[root@VM_0_6_centos ~]# netstat -anp|grep :22
tcp 0 0 0.0.0.0:22 0.0.0.0:*
tcp 0 36 172.16.0.6:22 211.161.248.60:22447
[root@VM_0_6_centos ~]# service sshd start
Redirecting to /bin/systemctl start sshd.service
复制代码
安装iptables 若是你要改用iptables的话,须要安装iptables服务:docker
#安装
sudo yum install iptables-services
#开启iptables
sudo systemctl enable iptables
sudo systemctl enable ip6tables
#启动服务
sudo systemctl start iptables
sudo systemctl start ip6tables
复制代码
关闭防火墙:apache
[root@VM_0_6_centos ~]# service iptables stop
Redirecting to /bin/systemctl stop iptables.service
[root@VM_0_6_centos ~]# service iptables status
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled) Active: inactive (dead) since Thu 2019-09-26 21:28:33 CST; 6s ago Process: 9537 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS) Process: 9354 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 9354 (code=exited, status=0/SUCCESS) Sep 26 21:27:28 VM_0_6_centos systemd[1]: Starting IPv4 firewall with iptables... Sep 26 21:27:28 VM_0_6_centos iptables.init[9354]: iptables: Applying firewall rules: [ OK ] Sep 26 21:27:28 VM_0_6_centos systemd[1]: Started IPv4 firewall with iptables. Sep 26 21:28:33 VM_0_6_centos systemd[1]: Stopping IPv4 firewall with iptables... Sep 26 21:28:33 VM_0_6_centos iptables.init[9537]: iptables: Setting chains to policy ACCEPT: filte... ] Sep 26 21:28:33 VM_0_6_centos iptables.init[9537]: iptables: Flushing firewall rules: [ OK ] Sep 26 21:28:33 VM_0_6_centos systemd[1]: Stopped IPv4 firewall with iptables. Hint: Some lines were ellipsized, use -l to show in full. [root@VM_0_6_centos ~]# 复制代码
Stopped IPv4 firewall with iptables 说明防火墙已关闭
yum install java
java -version
复制代码
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm
复制代码
chmod +x jdk-8u131-linux-x64.rpm
复制代码
rpm -ivh jdk-8u131-linux-x64.rpm
复制代码
java -version
复制代码
默认安装路径为 /usr/java/jdk1.8.0_131
vim /etc/profile
复制代码
环境变量为
export JAVA_HOME=/usr/java/jdk1.8.0_131
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
复制代码
source /etc/profile
复制代码
javac
java -version
echo $PATH
复制代码
下载:jdk-8u221-linux-x64.tar.gz 放到 /usr/local ,解压
tar -zxvf jdk-8u221-linux-x64.tar.gz
复制代码
更名 jdk1.8
mv jdk1.8.0_221 jdk1.8
复制代码
[root@VM_0_6_centos local]# ls
bin etc games include jdk1.8 lib lib64 libexec qcloud sbin share src yd.socket.server
复制代码
配置jdk1.8环境变量
[root@VM_0_6_centos jdk1.8]# pwd
/usr/local/jdk1.8
[root@VM_0_6_centos jdk1.8]# vi /etc/profile
复制代码
环境变量为
export JAVA_HOME=/usr/local/jdk1.8
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
复制代码
退出文件,保存 按Esc键 输入:wq 保存并退出 让配置文件生效:
[root@VM_0_6_centos jdk1.8]# source /etc/profile
复制代码
查看是否配置成功:
[root@VM_0_6_centos jdk1.8]# java -version
java version "1.8.0_221"
复制代码
若出现jdk版本号,则安装并配置环境变量成功 若是提示命令找不到的话,查看一下jdk的配置路径是否错误。 删除安装包(可选):rm -rf jdk-8u221-linux-x64.tar.gz
[root@VM_0_6_centos local]# yum install git
[root@VM_0_6_centos local]# git --version
git version 1.8.3.1
复制代码
一、获取github最新的Git安装包下载连接,下载新版本 git-2.9.5.tar.gz 上传到linux; 二、压缩包解压:
sudo tar -zxvf git-2.9.5.tar.gz
复制代码
三、进入解压后的文件夹:
cd git-2.9.5
复制代码
四、执行编译:耐心等待编译便可
make prefix=/usr/local/git all
复制代码
五、安装Git至/usr/local/git路径,命令为
make prefix=/usr/local/git install
复制代码
六、打开环境变量配置文件,命令 ,在底部加上Git相关配置信息:
vim /etc/profile
复制代码
PATH=$PATH:/usr/local/git/bin export PATH
而后wq保存,退出! 让配置文件生效:
source /etc/profile
复制代码
七、输入命令 git --version ,查看安装的git版本,校验经过,安装成功。 八、删除安装包(可选):rm -rf git-2.9.5.tar.gz 九、查看git安装路径: Linux-若是不清楚本身的Git安装在哪一个路径下,终端执行
whereis git
复制代码
配置:/usr/local/git/bin/git
ssh公钥能够配置gitlab、github等, ssh私钥可配置jenkins凭证等。 用户和邮箱不要求真实
[root@VM_0_6_centos ~]# git config --global user.name "zhengja"
[root@VM_0_6_centos ~]# git config --global user.email "zhengja@dist.com"
[root@VM_0_6_centos ~]# ssh-keygen -t rsa -C "zhengja@dist.com"
Enter file in which to save the key (/root/.ssh/id_rsa):
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:EqKVJUBpxVYpswJifGiXyplLTUcBYFNaMxaODZQPUmc zhengja@dist.com
The key's randomart image is:
+---[RSA 2048]----+
|oOO&E++. |
|=B%BB=. |
|*=@+=+. |
| *.=.. . |
|. o. . S |
| . . |
| |
| |
| |
+----[SHA256]-----+
复制代码
拿到公钥配置gitlab
[root@VM_0_6_centos local]# cd /root/.ssh/
[root@VM_0_6_centos .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@VM_0_6_centos .ssh]# vim id_rsa.pub
复制代码
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcArp9fBpi7CWJ50dQQTHbLfRkMyhVuox7bFEeYu3kaFYO3ECInfiBEK8PdYAKPjGWRirYQGCBQlsSzGd4F3sVTQH4ePk0bk/iAwnZsgOBgMgyZW6c00u+afJ/h8PNRK2xr60kB3n4no4UDov4hsuGdCKhavrHRzs3s6H+b7WKxILdSwZ7BpduGZGCOug8JtcnwF1mmMKi0YH0KrIxnrOR9GHT+wHAyJ5WjYU4Xzr+ZIGN6MfrM7X99y/uyMsrv8nzliMxWpFxduYAkKkfrqf46Y18oZfn5VdXhfnvyd+FqWTPdtJF2EZtKdhwGnouuyTdESQP3PPacFM1ZeW58R/T zhengja@dist.com
测试:git命令克隆sshUrl到本地
git clone sshURL
复制代码
下载 apache-maven-3.6.2-bin.zip ,上传到linux系统并解压
[root@VM_0_6_centos local]# ls
apache-maven-3.6.2-bin.zip etc include lib libexec sbin src
bin games jdk1.8 lib64 qcloud share yd.socket.server
[root@VM_0_6_centos local]# unzip apache-maven-3.6.2-bin.zip
[root@VM_0_6_centos local]# ls
apache-maven-3.6.2 bin games jdk1.8 lib64 qcloud share yd.socket.server
apache-maven-3.6.2-bin.zip etc include lib libexec sbin src
复制代码
配置maven环境变量
[root@VM_0_6_centos apache-maven-3.6.2]# pwd
/usr/local/apache-maven-3.6.2
[root@VM_0_6_centos apache-maven-3.6.2]# vim /etc/profile
复制代码
末尾添加(路径要正确)
export MAVEN_HOME=/usr/local/apache-maven-3.6.2
export PATH=$MAVEN_HOME/bin:$PATH
复制代码
从新加载配置-->测试maven是否配置成功
[root@VM_0_6_centos apache-maven-3.6.2]# . /etc/profile
[root@VM_0_6_centos apache-maven-3.6.2]# mvn -version
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
Maven home: /usr/local/apache-maven-3.6.2
Java version: 1.8.0_221, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8/jre
复制代码
删除安装包(可选):rm -rf apache-maven-3.6.2-bin.zip
下载 tomcat8 ,上传到linux,并解压
[root@VM_0_6_centos local]# unzip apache-tomcat-8.5.46.zip
[root@VM_0_6_centos local]# ls
apache-maven-3.6.2 bin include lib64 sbin yd.socket.server
apache-tomcat-8.5.46 etc jdk1.8 libexec share
apache-tomcat-8.5.46.zip games lib qcloud src
复制代码
配置tomcat 给tomcat下的全部文件授给任何人均可执行的权限
[root@VM_0_6_centos local]# cd apache-tomcat-8.5.46/
[root@VM_0_6_centos apache-tomcat-8.5.46]# chmod a+x -R *
复制代码
修改tomcat端口为8090
[root@VM_0_6_centos apache-tomcat-8.5.46]# vim conf/server.xml
复制代码
启动tomcat:
[root@VM_0_6_centos apache-tomcat-8.5.46]# bin/startup.sh
复制代码
查看tomcat是否启动成功
[root@VM_0_6_centos apache-tomcat-8.5.46]# ps -ef|grep tomcat
复制代码
root 22857 1 29 19:12 pts/0 00:00:02 /usr/local/jdk1.8/bin/java -Dcatalina.base=/usr/local/apache-tomcat-8.5.46 -Dcatalina.home=/usr/local/apache-tomcat-8.5.46 -Djava.io.tmpdir=/usr/local/apache-tomcat-8.5.46/temp org.apache.catalina.startup.Bootstrap start root 27474 19080 0 10:10 pts/0 00:00:00 grep --color=auto tomcat
启动成功标识!
关闭tomcat: 首先,进入Tomcat下的bin目录
cd /usr/local/tomcat/bin
使用./shutdown.sh或者是sh shutdown.sh关闭Tomcat
复制代码
想直接直接杀死Tomcat进程(进程号: 22857),使用kill命令
kill -9 22857
jenkins主机环境:java、jenkins
做为slave的Linux机器为centos系统. ip: 129.204.207.127 环境:java1.八、maven、git、tomcat:8090 路径:
java : /usr/local/jdk1.8 maven : /usr/local/apache-maven-3.6.2 git : /usr/local/git tomcat: /usr/local/apache-tomcat-8.5.46
环境变量:vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8 export PATH=
PATH export CLASSPATH=.:
JAVA_HOME/lib/tools.jar PATH=
MAVEN_HOME/bin:$PATH
用户:root/123 生成ssh:私钥、公钥 [Linux生成SSH密钥方式](其它/SSH 证书生成(Linux、windows).md) 节点Slave生成:authorized_keys 并受权
[root@VM_0_6_centos ~]# cd .ssh
[root@VM_0_6_centos .ssh]# ls
id_rsa id_rsa.pub known_hosts
[root@VM_0_6_centos .ssh]# cat id_rsa.pub > authorized_keys
[root@VM_0_6_centos .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@VM_0_6_centos .ssh]# chmod 700 authorized_keys
复制代码
获取节点Slave私钥:id_rsa
[root@VM_0_6_centos ~]# cd /root/.ssh
[root@VM_0_6_centos .ssh]# vim id_rsa
复制代码
建立节点凭证:root/私钥
三、建立节点slave 系统管理->节点管理->新建节点: 节点名称:Linux_127 、 选着固定节点 -->肯定
查看节点列表:显示节点磁盘大小说明配置成功
测试节点: 新建任务:任务名称:springboot-test-gitlab、选择:构建一个自由风格的软件项目
控制台输出:
成功控制台输出
Started by user zhengja
Running as SYSTEM
Building remotely on Linux_127 in workspace /root/.jenkins/workspace/springboot-test-gitlab
[springboot-test-gitlab] $ /bin/sh -xe /tmp/jenkins1901112003247678190.sh
+ ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:edff:fe35:28c8 prefixlen 64 scopeid 0x20<link> ether 02:42:ed:35:28:c8 txqueuelen 0 (Ethernet) RX packets 510 bytes 75763 (73.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 489 bytes 56366 (55.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 group default link/ether c2:51:e9:ca:47:56 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::c051:e9ff:feca:4756/64 scope link valid_lft forever preferred_lft forever Finished: SUCCESS 复制代码
中文解释:
成功控制台输出 由用户zhengja发起 做为系统运行 上远程构建Linux_127在工做区/root/.jenkins/workspace/springboot-test-gitlab [springboot-test-gitlab] $ / bin / sh -xe /tmp/jenkins1901112003247678190.sh + ifconfig docker0:标志= 4163 <UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1网络掩码255.255.0.0广播172.17.255.255 inet6 fe80 :: 42:edff:fe35:28c8 prefixlen 64 scopeid 0x20 <link> 以太网02:42:ed:35:28:c8 txqueuelen 0(以太网) RX数据包510字节75763(73.9 KiB) 接收错误0掉线0超限0帧0 TX数据包489字节56366(55.0 KiB) TX错误0掉线0超限0载波0冲突0 组默认 连接/以太c2:51:e9:ca:47:56 brd ff:ff:ff:ff:ff:ff:ff连接netnsid 0 inet6 fe80 :: c051:e9ff:feca:4756/64做用域连接 永久有效_lft永久首选_lft 已完成:成功 复制代码
配置节点成功!
目的:代码上传->gitlab通知->jenkins服务器执行构建->远程应用服务器git下拉->远程应用服务器maven打包->发送到远程应用服务器的tomcat->发布
deploy.sh脚本在springboot-test-gitlab/deploy.sh 项目根目录下 其中项目中的脚本:deploy.sh
#!/usr/bin/env bash
#编译+部署项目站点
#须要配置以下参数
# 项目路径, 在Execute Shell中配置项目路径, pwd 就能够得到该项目路径
# export PROJ_PATH=这个jenkins任务在部署机器上的路径
# export PROJ_PATH=/root/.jenkins
# 输入你的环境上tomcat的全路径
# export TOMCAT_APP_PATH=tomcat在部署机器上的路径
# export TOMCAT_APP_PATH=/usr/local/apache-tomcat-8.5.46
### base 函数
killTomcat()
{
pid=`ps -ef|grep tomcat|grep java|awk '{print $2}'`
echo "tomcat Id list :$pid"
if [ "$pid" = "" ]
then
echo "no tomcat pid alive"
else
kill -9 $pid
fi
#上面注释的或者下面的
#cd $TOMCAT_APP_PATH/bin
#sh shutdown.sh
}
cd $PROJ_PATH/workspace/springboot-test-gitlab2/springboot
mvn clean install
# 停tomcat
killTomcat
# 删除原有工程
#rm -rf $TOMCAT_APP_PATH/webapps/ROOT
#rm -f $TOMCAT_APP_PATH/webapps/ROOT.war
rm -f $TOMCAT_APP_PATH/webapps/springboot-test-gitlab.war
# 复制新的工程到tomcat上
cp $PROJ_PATH/workspace/springboot-test-gitlab2/springboot/target/springboot-test-gitlab-0.0.1-SNAPSHOT.war $TOMCAT_APP_PATH/webapps/
cd $TOMCAT_APP_PATH/webapps/
mv springboot-test-gitlab-0.0.1-SNAPSHOT.war springboot-test-gitlab.war
# 启动Tomcat
cd $TOMCAT_APP_PATH/
sh bin/startup.sh
复制代码
建立一个任务 名称:springboot-test-gitlab2 类型: 一个自由风格的任务
源码管理: gitlab配置节点的ssh公钥,配置参考:[SSH 证书生成(Linux、windows).md](其它/SSH 证书生成(Linux、windows).md) 中的 “2、Linux生成证书-->3.证书使用-例如:配置gitlab”。
构建触发器配置: 请参考:jenkins构建触发器配置中的_代码提交—>gitlab通知jenkins构建.
构建配置:
BUILD_ID=DONTKILLME . /etc/profile export PROJ_PATH=/root/.jenkins export TOMCAT_APP_PATH=/usr/local/apache-tomcat-8.5.46 sh $PROJ_PATH/workspace/springboot-test-gitlab2/springboot/deploy.sh
保存->执行构建 执行构建结果:
Started by GitLab push by zhengja
Started by user zhengja
Rebuilds build #7
Running as SYSTEM
Building remotely on Linux_127 in workspace /root/.jenkins/workspace/springboot-test-gitlab2
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url ssh://git@elbgit-1200450932.cn-northwest-1.elb.amazonaws.com.cn:5337/zhengja/springboot-test-gitlab.git # timeout=10
Fetching upstream changes from ssh://git@elbgit-1200450932.cn-northwest-1.elb.amazonaws.com.cn:5337/zhengja/springboot-test-gitlab.git
> git --version # timeout=10
> git fetch --tags --progress ssh://git@elbgit-1200450932.cn-northwest-1.elb.amazonaws.com.cn:5337/zhengja/springboot-test-gitlab.git +refs/heads/*:refs/remotes/origin/*
...........
.........
..........
09:28:22.884 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.8.RELEASE) 2019-09-30 09:28:23.541 INFO 29490 --- [ main] d.s.SpringbootTestGithubApplicationTests : Starting SpringbootTestGithubApplicationTests on VM_0_6_centos with PID 29490 (started by root in /root/.jenkins/workspace/springboot-test-gitlab2/springboot) ................ ............ ........ [INFO] Installing /root/.jenkins/workspace/springboot-test-gitlab2/springboot/pom.xml to /root/.m2/repository/com/dist/springboot-test-gitlab/0.0.1-SNAPSHOT/springboot-test-gitlab-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 16.105 s [INFO] Finished at: 2019-09-30T09:28:31+08:00 [INFO] ------------------------------------------------------------------------ tomcat Id list :29272 Tomcat started. Finished: SUCCESS 复制代码
访问:http://129.204.207.127:8090/springboot-test-gitlab/
html页面显示:
进入首页 测试自动构建是否成功 测试gitlab-->通知jenkins自动构建
到此,项目的自动部署及发布完成!
目的:代码上传->gitlab通知->jenkins服务器执行构建->远程应用服务器git下拉->远程应用服务器maven打包->发送到远程应用服务器的tomcat->发布->邮箱通知
参考 :jenkins 邮箱通知配置