1、环境准备
1)安装jdk工具
- 安装yum epel源
[root@k8s-node02 ~]# yum install -y epel-release
- 安装jdk
[root@k8s-node02 ~]# yum install -y java-1.8.0-openjdk
- 验证jdk
[root@k8s-node02 ~]# java -version openjdk version "1.8.0_232" OpenJDK Runtime Environment (build 1.8.0_232-b09) OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)
2)安装maven工具
- 安装maven官方源
[root@k8s-node02 ~]# yum-config-manager --add-repo http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo [root@k8s-node02 ~]# yum-config-manager --enable epel-apache-maven
- 安装maven
[root@k8s-node02 ~]# yum install -y apache-maven
- 验证maven
[root@k8s-node02 ~]# mvn -version Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T03:58:13-04:00) Maven home: /usr/share/apache-maven Java version: 1.8.0_232, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-957.27.2.el7.x86_64", arch: "amd64", family: "unix"
2、安装tomcat
- 官网下载地址:http://tomcat.apache.org/
- 下载tomcat
[root@k8s-node02 tools]# wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.50/bin/apache-tomcat-8.5.50.tar.gz
- 安装tomcat
[root@k8s-node02 tools]# tar -zxf apache-tomcat-8.5.50.tar.gz -C /opt/ [root@k8s-node02 tools]# ln -s /opt/apache-tomcat-8.5.50 /opt/tomcat [root@k8s-node02 tools]# echo 'export TOMCAT_HOME=/opt/tomcat'>>/etc/profile [root@k8s-node02 tools]# echo 'export PATH=$PATH:$TOMCAT_HOME/bin' >>/etc/profile [root@k8s-node02 tools]# source /etc/profile
- 启动tomcat
[root@k8s-node02 tools]# /opt/tomcat/bin/startup.sh start Using CATALINA_BASE: /opt/tomcat Using CATALINA_HOME: /opt/tomcat Using CATALINA_TMPDIR: /opt/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar Tomcat started.
- 设置权限
[root@k8s-node02 java_demo]# chown -R staff:staff /opt/apache-tomcat-8.5.50
- 浏览器验证
3、准备gitlab的" .gitlab-ci.yml "配置文件
[root@k8s-node02 java_demo]# vim .gitlab-ci.yml
# 定义stages
stages:
- build
- install
# 定义安装包的存放位置和Tomcat服务器的地址的变量,便于后续部署使用.
variables:
CIDEMO_PACKAGE_DIR: '/home/gitlab-runner/packages/cidemo'
SERVER_HOME_DIR: '/home/gitlab-runner/tomcat/cidemo-tomcat'
###################### 构建编译和Maven打包编译 #######################
#编译测试任务:
deploy-build-test:
stage: build
only:
- master
script:
- mvn clean package -Dmaven.test.skip=true
- rm -rf $CIDEMO_PACKAGE_DIR/*.war
- cp -a target/*.war $CIDEMO_PACKAGE_DIR/cidemo.war
###################### deploy部署安装 #######################
#打包任务:
deploy-install-test:
stage: install
script:
- echo '准备将最新的war包复制、保存到某个目录里面供后续使用.'
- rm -rf $SERVER_HOME_DIR/*.war
- cp -a $CIDEMO_PACKAGE_DIR/cidemo.war $SERVER_HOME_DIR/ROOT.war
- /opt/tomcat/bin/startup.sh stop
- rm -rf /opt/tomcat/webapps/*
- cp -a $SERVER_HOME_DIR/ROOT.war /opt/tomcat/webapps/
- /opt/tomcat/bin/startup.sh start
把编写的“.gitlab-ci.yml”提交到master端
4、 浏览器访问