基于centos7实现Jenkins通过Jenkinsfile文件流水线发布
该篇文章主要是讲解基于Jenkinsfile实现流水线发布。如果没有试验环境,可以参考前面的文章,先构建好环境。
7. 基于Jenkinsfile进行流水线发布:
在Jenkins中用pipeline流水线发布,确实很方便。但是存在不好维护,版本控制难的问题。
# 在项目的根文件目录下创建Jenkinsfile文件:
[root@node1 jpress]# pwd
/data/objects/jpress
# 把Jenkins中pipeline的脚本放到该文件中:
[root@node1 jpress]# vim Jenkinsfile
pipeline {
agent any
stages {
stage('pull jpress') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '7be513bf-758a-49b1-8e5b-b0dad901ac95', url: 'http://192.168.75.121/dev1_group/jpress.git']]])
}
}
stage('build jpress') {
steps {
sh 'mvn clean package'
}
}
stage('deploy jpress') {
steps {
deploy adapters: [tomcat9(credentialsId: 'bc20512e-12cf-485c-b5f4-0254d3e7b004', path: '', url: 'http://192.168.75.123:8080/')], contextPath: null, war: '*/target/*.war'
}
}
}
}
# 推送到gitlab的jpress项目:
[root@node1 jpress]# git add .
[root@node1 jpress]# git commit -m "add Jenkinsfile"
[master c8321a5] add Jenkinsfile
1 file changed, 21 insertions(+)
create mode 100644 Jenkinsfile
[root@node1 jpress]# git push
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:
git config --global push.default matching
若要不再显示本信息并从现在开始采用新的使用习惯,设置:
git config --global push.default simple
参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)
Username for 'http://192.168.75.121': xiaoming
Password for 'http://xiaoming@192.168.75.121':
Counting objects: 4, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 628 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://192.168.75.121/dev1_group/jpress.git
4099ceb..c8321a5 master -> master
[root@node1 jpress]#
7.1 Jenkins配置pipeline:
7.2 开始构建:
7.3 部署完成;
到此已经基于Jenkins实现了基于Jenkinsfile的流水线方式构建项目。