jenkins 构建细节 - (分支参数化构建)
pipeline 通过 自定义参数(This project is parameterized - String Parame),来构建部署。
( 注释: 参数化构建 - 字符串参数 )
一、 实现内容
Gitlab 主分支、从分支 代码修改后,通过 webhook 触发 jenkins。
jenkins 可以通过 分支变量 来构建部署。
二、实现步骤
1、在 pipeline 流水线项目开启参数化构建
2、修改 pipeline 的 Jenkinsfile 文件,指定变量来拉取代码
注意:此处使用的是 Jenkinsfile - pipeline,每个分支都需要有这样一个文件
脚本如下:
pipeline {
agent any
stages {
stage('pull code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab-manven', url: 'git@192.168.168.4:cheliang/test2.git']]])
}
}
stage('build project') {
steps {
sh label: '', script: '''echo "======= 开始打包 ========"
source /etc/profile
mvn clean package
echo "======= 打包结束、开始部署 ======="'''
}
}
stage('publish project') {
steps {
deploy adapters: [tomcat9(credentialsId: 'b73170b6-fa08-4350-9d35-530b1eb19b75', path: '', url: 'http://192.168.168.5:8080/')], contextPath: null, war: 'target/*.war'
}
}
}
}
3、在 Gitlab 上创建分支,并设置 jenkins 主分支、从分支的 webhook
①、配置webhook,注意有几个分支,就要开几个webhook
主分支webhook:
从分支webhook:
②、配置 jenkins - Pipeline构建任务
4、Gitlab 修改主分支 或者 从分支 内容,Jenkins 自动触发构建
①、修改主分支内容
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>演示项目主页</title>
<body>
如果看到此页面,代表项目部署成功! --- master -0.1 主分支
<a href="/addUser">添加用户</a>
</hr>
<a href="/updateUser">添加用户</a>
</body>
</html>
②、修改从分支内容
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>演示项目主页</title>
<body>
如果看到此页面,代表项目部署成功! --- v1.2 分支
<a href="/addUser">添加用户</a>
</hr>
<a href="/updateUser">添加用户</a>
</body>
</html>