最近折腾了2天,终于完整得搭建了一套自动打包,自动部署得环境,里面坑很多,分享出来,攒点人品,让后来者避开这些坑。
部署机器:
- 192.168.0.94 (centos6),运行gitlab,jenkins
- 192.168.0.93 (centos6),运行tomcat
图简单得话,可以直接将防火墙关闭,否则就需要开放这些服务用到得端口。这里面gitlab使用13001,Jenkins使用13002.
打开配置文件
vim /etc/sysconfig/iptables
加入如下语句:
-A INPUT -p tcp -m state --state NEW -m tcp--dport 13001 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp--dport 13002 -j ACCEPT
保存(按Esc退出编辑)
:wq
重启防火墙,修改完成
service iptables restart
- 安装JDK(目前Jenkins支持jdk8到jdk11)
讲jdk-8u11-linux-x64.tar.gz解压到/opt/java下面,/etc/profile文件添加好相关路径即可。
tar -zxvf jdk 将解压后得文件夹放在/usr/local下面
export JAVA_HOME=/usr/local/jdk1.8.0_11
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile(让改动生效)
- Maven安装
tar-zxvf apache-maven-3.5.0-bin.tar.gz
mv apache-maven-3.5.0/* /usr/local/maven/
vim /etc/profile
exportM2_HOME=/usr/local/maven
exportPATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$M2_HOME/bin:$PATH
source /etc/profile
配置仓库信息和镜像源,使用阿里云:
在/usr/local/maven/conf/settings.xml下面配置仓库信息:
<localRepository>/opt/maven/repo</localRepository>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
- gitlab安装
A. 安装依赖软件
yum -y install policycoreutils openssh-server openssh-clientspostfix
B. 获取rpm包并安装
wget https://mirrors.tuna.tsinghua...
rpm -ivh gitlab-ce-9.3.6-ce.0.el7.x86_64.rpm
C. 自定义端口和存放路径
vim /etc/gitlab/gitlab.rb
external_url 'http://192.168.0.94:13001'
git_data_dirs({ "default" => { "path" => "/var/opt/gitlab/git-data", 'gitaly_address' => 'unix:/var/opt/gitlab/gitaly/gitaly.socket' } })
git_data_dir "/oracle/gitlab"
增加一个 git_data_dir
D. 启动gitlab
gitlab-ctl reconfigure
gitlab-ctl restart
- 安装jenkins
root用户下:
下载依赖
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat...
导入秘钥
sudo rpm --import https://pkg.jenkins.io/redhat...
安装jenkins
yum install jenkins
配置jenkins的端口以及修改用户为root:
[root@localhost ~]# vi /etc/sysconfig/jenkin
配置jenkins得java路径:
vim /etc/init.d/jenkins
找到 candidates,里面加入你本机的实际java路径
启动jenkins: service jenkins restart
日志位置:/var/log/jenkins/jenkins.log
一个注意得地方:
刚启动jenkins得时候,会提示你该jenkins处于离线状态,需要处理一下:
- 打开配置界面
http://localhost:13002/pluginManager/advanced
- http://localhost:13002/restart
- 编写一个springboot得测试程序,改pom文件为编译成一个war包
package com.avictc.springlearn;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@RequestMapping("/test1")
public class SpringlearnApplication extends SpringBootServletInitializer {
@RequestMapping("/getJSon")
public JSONObject returnJson(){
JSONObject o = new JSONObject();
o.put("hello","yes");
return o;
}
@RequestMapping("/getString")
public String returnJson2(){
return "hello";
}
public static void main(String[] args) {
SpringApplication.run(SpringlearnApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringlearnApplication.class);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<packaging>war</packaging>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.avictc</groupId>
<artifactId>springlearn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springlearn</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 添加fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
<!-- 添加AOP-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<!-- 应与application.properties(或application.yml)中context-path保持一致 -->
<finalName>springtest</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
打包命令:
mvn clean package -Dmaven.test.skip=true
- 开始配置jenkins,,系统管理--》全局工具配置
注意:git需要配置到bin目录下得git,其他JDK,Maven只需要配置到主目录即可。
系统管理-》系统设置,配置jenkins需要ssh到的服务器,比如192.168.0.93
需要首先在Jenkins所在的机器192.168.0.94
ssh-keygen -t rsa
生成了私钥文件id_rsa,公钥文件id_rsa.pub
将公钥文件id_rsa.pub的内容追加到192.168.0.93中的/root/.ssh/authorized_keys中,这样,94就可以ssh到93这台机器了。
- 新建一个job,并进行配置:
源码下载
触发器暂时不考虑,手工点击构建即可
使用maven进行打包构建
192.168.0.93上面的restartTomcat.sh文件内容:
!/bin/sh
export BUILD_ID=dontkillme
export JAVA_HOME=/opt/java/jdk1.8.0_11
PID=ps -ef |grep /opt/java/apache-tomcat-9.0.19/conf |grep -v grep | awk '{print $2}'
if [ ! "$PID" ];then
echo "tomcat not start"
else
echo "will kill tomcat process:$PID"
kill -9 $PID
fi
cd /opt/java/apache-tomcat-9.0.19/webapps
rm -rf springtest*
mv /opt/java/apache-tomcat-9.0.19/*.war /opt/java/apache-tomcat-9.0.19/webapps
cd /opt/java/apache-tomcat-9.0.19/bin/
nohup sh ./startup.sh
echo "tomcat start executes"
sleep 3s
注意:一定要export JAVA_HOME,否则Tomcat怎么都启动不了!!!
至此,可以放心的一键部署了,完成了所有步骤。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。