一.安装配置Git
1. 安装Git
直接使用yum安装:yum install git
注意:git安装于jenkins服务器
2. 配置账号密码:配置账号密码使之能够克隆库时免于输入
(1)cd ~/ && touch .git-credentials
(2)vi .git-credentials
(3)写入https://username:password@github.com(视情况进行替换)
(4)保存并退出
(5)存至配置文件:git config --global credential.helper store
3. 拉取代码
(1)mkdir /home/git/example
(2)cd /home/git/example
(3)git init
(4)git pull http://192.168.18.76:8081/example.git
二.编辑pom文件
1. 加入docker插件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.1</version>
</plugin>
2. 加入一些操作
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>image-tag</id>
<phase>package</phase>
<goals>
<goal>tag</goal>
</goals>
<configuration>
<image>${project.artifactId}:${project.version}</image>
<newName>
${dt-soft.registry.url}/${project.artifactId}:${project.version}
</newName>
</configuration>
</execution>
<execution>
<id>package-push</id>
<phase>package</phase>
<goals>
<goal>push</goal>
</goals>
<configuration>
<retryPushCount>1</retryPushCount>
<imageName>
${dt-soft.registry.url}/${project.artifactId}:${project.version}
</imageName>
</configuration>
</execution>
</executions>
(1)加在plugin标签中
(2)其中phase代表maven命令,goals代表maven命令后需进行的Docker命令,configuration为一些所需参数
(3)这个配置代表maven打包后生成docker镜像并推送到docker镜像库
3. 加入一些所需参数
<configuration>
<forceTags>true</forceTags>
<imageName>${project.artifactId}</imageName>
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
<DEFAULT_PORT>${default-port}</DEFAULT_PORT>
</buildArgs>
</configuration>
(1)其中主要配置的是dockerFile文件目录及项目jar文件
4. 加入一些properties
<properties>
<dt-soft.registry.url>192.168.18.85:5000</dt-soft.registry.url>
<default-port>8089</default-port>
</properties>
(1)配置docker镜像库地址与项目端口号
三.SonarQube配置
1. 安装中文插件(可选)
(1)下载中文插件:https://github.com/SonarQubeCommunity/sonar-l10n-zh
(2)放入项目目录下的\extensions\plugins
(3)重启SonarQube
1)su - sonar
2)./home/rpm/sonarqube-7.3/bin/linux-x86-64/sonar.sh start
2. 生成Token
(1)进入管理页面,以管理员账户登录
(2)进入权限-用户-令牌-生成令牌
(3)记下生成令牌的名称与Token(只会显示一次)
四. Docker配置
1. docker服务器配置镜像库
(1)vi /etc/docker/daemon.json
(2)修改配置文件,将需要用的docker私服加入配置文件中,并设置为默认,如下
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://192.168.18.85:5000"
],
"insecure-registries": [
"192.168.18.85:5000"
]
}
(3)保存并退出
(4)重启docker
五.Jenkins配置
1. 安装相关插件
(1)Maven插件:Maven Integration plugin(用于新建Maven任务)
(2)SSH相关插件:SSH plugin、publish over SSH(用于远程SSH)
(3)Gitlab相关插件:Gitlab plugin、Gitlab Hook plugin(用于生成Webhook钩子)
(4)Sonar插件:SonarQube Scanner for Jenkins(用于与Sonar集成)
2. 配置SSH
(1)进入系统管理-配置-系统配置
(2)配置 SSH Servers:包括ip、登录用户名、登陆后操作的目录
3. 配置git环境变量
(1)进入系统管理-配置-全局工具配置
(2)配置git环境变量为/usr/bin/git(需先在jenkins服务器上安装git)
4. 配置Sonar Server
(1)进入系统管理-配置-系统配置
(2)配置名称,URL与Token
其中名称与Token为之前Sonar生成的,URL为Sonar访问地址
六.建立任务
1. 新建一个Maven项目
(1)以管理员账户登录Jenkins
(2)新建项目 - Maven项目
2. 任务配置
(1)配置源码管理:选择git,配置项目git路径以及git用户名密码
(2)配置构建触发器:选择当git代码推送时触发
展开高级配置,生成一个secret token
记下此处的webhook URL以及secret token
(3)构建环境勾选Sonar代码审查
(4)Pre Steps:加入执行Shell与Execute SonarQube Scanner
1)执行的Shell
I. 切换工作目录:cd /home/workspace/plcsuite/example
II. 拉取最新代码:git pull http://root:W7zqv076nz@192.168.18.76:8081/plc-suite/example.git
III.Maven清理: mvn clean
IV.Maven编译(Sonar审查前需先编译): mvn compile
2)Execute SonarQube Scanner:在Analysis properties处加入如下配置(需视情况修改):
sonar.projectKey=plcsuite-network-service
sonar.projectName=plcsuite-network-service
sonar.projectVersion=0.0.1
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.sources=./src
sonar.java.binaries=./target/classes
JDK选择Inherit From Job
(5)Build处留空
(6)Post Steps:加入执行Shell与Send files or execute commands over SSH
1)需执行的Shell
I.设置docker镜像库:registry_addr="192.168.18.85:5000"
II.项目名称:project_name=$POM_ARTIFACTID
其中$POM_ARTIFACTID为Maven环境变量代表项目名称
III.docker容器名称:docker_container_name="network"
IV.项目暴露的端口:docker_container_port="8089"
V.项目版本:version=$POM_VERSION
其中$POM_VERSION为Maven环境变量代表项目版本
VI.docker镜像名称:image_tag="${registry_addr}/${project_name}:${version}"
VII.Maven打包并设置端口与跳过测试:mvn -Ddefault-port=${docker_container_port} package -Dmaven.test.skip=true
因为配置了POM文件所以打包时自动生成Docker镜像
VIII.将镜像推送到Docker镜像库docker push ${image_tag}
IX.移除本地镜像docker rmi ${image_tag}
X.将上文中设置的环境变量写入配置文件:
echo -e "image_tag=${image_tag}\nproject_name=${project_name}\nversion=${version}\ndocker_container_name=${docker_container_name}\ndocker_container_port=${docker_container_port}" > ${WORKSPACE}/network-service.properties
最后的配置文件名需视情况修改
2)Send files or execute commands over SSH配置如下
SSH Server与Source file,SSH目标为Docker,文件名为上一步中的配置文件名。
需配置的指令为:
I.设置配置文件访问权限:chmod 777 ./network-service.properties
II.将配置文件中的变量释放至服务器:. /network-service.properties
III.停止之前的容器:docker stop ${docker_container_name}
IV.移除之前的容器:docker rm ${docker_container_name}
V.移除之前的镜像:docker rmi ${image_tag}
VI.启动最新的容器:
docker run -d --name ${docker_container_name} -p ${docker_container_port}:${docker_container_port} ${image_tag}
会自动从镜像库下载,需先配置docker镜像库
(7)保存
七.配置GitLab
1. 以管理员账户登录
2. 打开项目
3. 打开Settings-Integrations(也称作Webhook)
(1)配置URL为jenkins构建触发器中webhook URL
(2)配置token为jenkins构建触发器中secret token
(3)勾选上Push events
(4)保存
至此配置完成