Jenkins+Pipeline+Maven+Java 实现项目持续集成

Jenkins+Pipeline+Maven+Java 实现项目持续集成

环境:(软件部署过程省略…)

192.168.2.50 jenkins

192.168.2.33 jenkins-slave tomcat

192.168.2.51 git-server

1、创建git 仓库 ,放入源代码

192.168.2.51 上:

[root@git ~]# su - git
Last login: Fri Jun 12 03:59:22 EDT 2020 on pts/0
[git@git ~]$ ls
repos
[git@git ~]$ cd repos/
[git@git repos]$ ls
app.git blog.git jenkinsfile.git
[git@git repos]$ mkdir test1.git
[git@git repos]$ cd test1.git/
[git@git test1.git]$ git --bare init
Initialized empty Git repository in /home/git/repos/test1.git/
[git@git test1.git]$ ls
branches config description HEAD hooks info objects refs

192.168.2.50 客户端 上传代码到master 分支上

[root@jenkins ~]# git clone git@192.168.2.51:/home/git/repos/test1.git
Cloning into 'test1'...
warning: You appear to have cloned an empty repository.

[root@jenkins ~]# git clone https://gitee.com/jflyfox/jfinal_cms.git
Cloning into 'jfinal_cms'...
remote: Enumerating objects: 6435, done.
remote: Counting objects: 100% (6435/6435), done.
remote: Compressing objects: 100% (3221/3221), done.
remote: Total 6435 (delta 2868), reused 6320 (delta 2790), pack-reused 0
Receiving objects: 100% (6435/6435), 39.62 MiB | 10.26 MiB/s, done.
Resolving deltas: 100% (2868/2868), done.

[root@jenkins ~]# ls
apache-maven-3.5.3-bin.tar.gz app blog jdk-8u201-linux-x64.tar.gz jenkinsfile jfinal_cms test1

[root@jenkins ~]# cd jfinal_cms/
[root@jenkins jfinal_cms]# ls
Apache v2 License.license ChangeLog.md doc pom.xml README.md sql src
[root@jenkins jfinal_cms]# mv ./* ../test1/
[root@jenkins jfinal_cms]# ls ../test1
Apache v2 License.license ChangeLog.md doc pom.xml README.md sql src
[root@jenkins jfinal_cms]#cd ../test1
[root@jenkins test1]# git add .
[root@jenkins test1]# git commit -m "test1"
[root@jenkins test1]# git push origin master
Counting objects: 2527, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2415/2415), done.
Writing objects: 100% (2527/2527), 22.14 MiB | 9.08 MiB/s, done.
Total 2527 (delta 375), reused 0 (delta 0)
To git@192.168.2.51:/home/git/repos/test1.git
* [new branch] master -> master

2、编写pipeline 流水线

[root@jenkins item-b]#cd /root/jenkinsfile/item-b

[root@jenkins item-b]# vim Jenkinsfile-java

node ("slave-192.168.2.33") {
//指定节点运行
stage('git checkout') {
checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@192.168.2.51:/home/git/repos/test1.git']]])
}
//指定git 地址;拉取代码,使用jenkins拉下来一个repo在jenkins的workspace中
stage('maven build') {
sh '''export JAVA_HOME=/usr/local/jdk1.8
/usr/local/maven3.5/bin/mvn clean package -Dmaven.test.skip=true'''
}
//maven构建代码
stage('deploy') {
sh '''
JENKINS_NODE_COOKIE=dontkillme
export JAVA_HOME=/usr/local/jdk1.8
TOMCAT_NAME=tomcat
TOMCAT_HOME=/usr/local/$TOMCAT_NAME
WWWROOT=$TOMCAT_HOME/webapps/ROOT

if [ -d $WWWROOT ]; then
mv $WWWROOT /data/backup/${TOMCAT_NAME}-$(date +"%F_%T")
fi
unzip ${WORKSPACE}/target/*.war -d $WWWROOT
PID=$(ps -ef |grep $TOMCAT_NAME |egrep -v "grep|$$" |awk \'{print $2}\')
[ -n "$PID" ] && kill -9 $PID
$TOMCAT_HOME/bin/startup.sh
'''
// JENKINS_NODE_COOKIE=dontkillme避免jenkins自动关闭衍生的子进程;
}
stage('test') {
sh "/usr/bin/echo 'hello word'"
}
}

上传pipeline 到git仓库中

[root@jenkins item-b]# git add .
[root@jenkins item-b]# git commit -m "test"
[master e8aa5c7] test
1 file changed, 2 insertions(+), 2 deletions(-)
[root@jenkins item-b]# git push origin master
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 372 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To git@192.168.2.51:/home/git/repos/jenkinsfile.git
a6391b0..e8aa5c7 master -> master

3、创建任务

Jenkins+Pipeline+Maven+Java 实现项目持续集成
Jenkins+Pipeline+Maven+Java 实现项目持续集成

添加一个参数化构建过程,选择字符参数

Jenkins+Pipeline+Maven+Java 实现项目持续集成

指定git 仓库中,pepeline 的位置

写明 脚本路径

保存

Jenkins+Pipeline+Maven+Java 实现项目持续集成

4、构建任务

Jenkins+Pipeline+Maven+Java 实现项目持续集成
Jenkins+Pipeline+Maven+Java 实现项目持续集成
Jenkins+Pipeline+Maven+Java 实现项目持续集成

构建成功,我们来测试下

5、测试

Jenkins+Pipeline+Maven+Java 实现项目持续集成

本文版权归 飞翔沫沫情 作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接 如有问题, 可发送邮件咨询,转贴请注明出处:https://www.fxkjnj.com/2157/

发表评论

登录后才能评论

评论列表(2条)