实现功能:git提交代码,然后Jenkins自动打包两次,提交前的分支一次,合并后的分支一次
如:dev提交到master,使得Jenkins打两个包,即dev分支在提交合并请求时打一次,
在合并请求完成后master分支打一次
git的webhooks设置
Url填入jenkins的job配置
勾选合并请求触发构建,勾选后在合并请求过程中会根据jenkins的详细控制进行触发
在git合并分支,触发webhooks时,会向Jenkins传递相应参数,如下示例:
以下为打开合并请求时携带的参数:
gitlabBranch : dev
gitlabSourceBranch : dev
gitlabActionType : MERGE
gitlabUserName : Administrator
gitlabUserEmail : admin@example.com
gitlabSourceRepoHomepage : http://10.9.8.185/gitlab-instance-02db178f/demo
gitlabSourceRepoName : demo
gitlabSourceNamespace : GitLab Instance
gitlabSourceRepoURL : git@10.9.8.185:gitlab-instance-02db178f/demo.git
gitlabSourceRepoSshUrl : git@10.9.8.185:gitlab-instance-02db178f/demo.git
gitlabSourceRepoHttpUrl : http://10.9.8.185/gitlab-instance-02db178f/demo.git
gitlabMergeRequestTitle : Update dev3
gitlabMergeRequestDescription :
gitlabMergeRequestId : 6
gitlabMergeRequestIid : 6
gitlabMergeRequestState : opened
gitlabMergedByUser : root
gitlabMergeRequestAssignee :
gitlabMergeRequestLastCommit : 9549b3448ccf41995dba6ec723715ad361676702
gitlabMergeRequestTargetProjectId : 2
gitlabTargetBranch : main
gitlabTargetRepoName : demo
gitlabTargetNamespace : GitLab Instance
gitlabTargetRepoSshUrl : git@10.9.8.185:gitlab-instance-02db178f/demo.git
gitlabTargetRepoHttpUrl : http://10.9.8.185/gitlab-instance-02db178f/demo.git
gitlabBefore :
gitlabAfter :
gitlabTriggerPhrase :
注:当完成合并请求时,gitlabMergeRequestState 的状态会改变,如下:
gitlabMergeRequestState : merged
Jenkins项目的触发设置
Jenkins配置
http://10.9.8.185/project/demo 为job的url,需要填入gitlab webhooks 的配置
根据gitlab的触发请求选择触发构建的条件,控制权在gitlab webhooks,
如gitlab 没有push请求则jenkins不会因为gitlab的push触发构建
但合并请求的详细控制在jenkins,分别对应:打开请求,新的commit提交,完成合并,关闭合并等
Jenkins的maven文件示例
在完成git和Jenkins项目的配置后,需要根据需求书写Jenkins pipeline的maven打包文件文章来源:https://www.toymoban.com/news/detail-507777.html
注:以下示例中的jacoco、junit、sonar需要额外插件文章来源地址https://www.toymoban.com/news/detail-507777.html
pipeline {
agent any
stages {
stage('parameters'){
steps{
sh '''
echo "gitlab webhooks触发构建携带的参数"
echo "
gitlabBranch : ${gitlabBranch}
gitlabSourceBranch : ${gitlabSourceBranch}
gitlabActionType : ${gitlabActionType}
gitlabUserName : ${gitlabUserName}
gitlabUserEmail : ${gitlabUserEmail}
gitlabSourceRepoHomepage : ${gitlabSourceRepoHomepage}
gitlabSourceRepoName : ${gitlabSourceRepoName}
gitlabSourceNamespace : ${gitlabSourceNamespace}
gitlabSourceRepoURL : ${gitlabSourceRepoURL}
gitlabSourceRepoSshUrl : ${gitlabSourceRepoSshUrl}
gitlabSourceRepoHttpUrl : ${gitlabSourceRepoHttpUrl}
gitlabMergeRequestTitle : ${gitlabMergeRequestTitle}
gitlabMergeRequestDescription : ${gitlabMergeRequestDescription}
gitlabMergeRequestId : ${gitlabMergeRequestId}
gitlabMergeRequestIid : ${gitlabMergeRequestIid}
gitlabMergeRequestState : ${gitlabMergeRequestState}
gitlabMergedByUser : ${gitlabMergedByUser}
gitlabMergeRequestAssignee : ${gitlabMergeRequestAssignee}
gitlabMergeRequestLastCommit : ${gitlabMergeRequestLastCommit}
gitlabMergeRequestTargetProjectId : ${gitlabMergeRequestTargetProjectId}
gitlabTargetBranch : ${gitlabTargetBranch}
gitlabTargetRepoName : ${gitlabTargetRepoName}
gitlabTargetNamespace : ${gitlabTargetNamespace}
gitlabTargetRepoSshUrl : ${gitlabTargetRepoSshUrl}
gitlabTargetRepoHttpUrl : ${gitlabTargetRepoHttpUrl}
gitlabBefore : ${gitlabBefore}
gitlabAfter : ${gitlabAfter}
gitlabTriggerPhrase : ${gitlabTriggerPhrase}"
'''
}
}
stage('checkout') {
steps {
script{
if( "${env.gitlabMergeRequestState}" == 'opened' ){
echo "打开合并请求,构建合并分支"
env.BRANCH="${env.gitlabBranch}"
}
if( "${env.gitlabMergeRequestState}" == 'merged' ){
echo "完成合并请求,切换构建分支为主分支"
env.BRANCH="${env.gitlabTargetBranch}"
}
if( "${env.gitlabMergeRequestState}" == '' || "${env.gitlabMergeRequestState}" == 'null' ){
echo "没有合并请求,构建默认分支"
env.BRANCH="${BRANCH}"
}
echo "动态分支 :${env.BRANCH} 拉取指定分支代码"
}
echo "env.BRANCH 为定义的参数变量,根据变量拉取指定分支"
checkout([$class: 'GitSCM', branches: [[name: "${env.BRANCH}"]], extensions: [], userRemoteConfigs: [[credentialsId: '5e3b04f0-93aa-4607-a149-b0ca73de9cfe', url: 'http://10.9.8.185/gitlab-instance-02db178f/demo.git']]])
}
}
stage('BUILD FILE') {
steps {
sh 'echo "build 构建日期文件写入"'
sh '''
L_RELEASE_NOTE=Notes.txt
L_COMMIT=`git log -1 --pretty=format:%H`
echo "Release Date: ` date \'+%Y-%m-%d %H:%M:%S\'`" >> $L_RELEASE_NOTE
echo "Commit: ${L_COMMIT}" >> ${L_RELEASE_NOTE}
echo "Build Number: "${BUILD_NUMBER} >> ${L_RELEASE_NOTE}
echo "Web Address: "${BUILD_URL} >> ${L_RELEASE_NOTE}
NOTE_FILE=BUILD
echo "build: ${VERSION}-b${BUILD_NUMBER}(${BRANCH})" >> $NOTE_FILE
echo "date: `date \'+%Y-%m-%d %H:%M:%S\'`" >> $NOTE_FILE '''
}
}
stage('build') {
steps {
echo "build 安装包"
sh 'mvn versions:set -DnewVersion=${VERSION}'
sh 'mvn clean package -Dmaven.test.failure.ignore=true -Dbuild.number=${BUILD_NUMBER}'
//sh 'mvn clean org.jacoco:jacoco-maven-plugin:0.8.8:prepare-agent package -Dmaven.test.failure.ignore=true -Dbuild.number=${BUILD_NUMBER}'
}
}
stage('Junit Test') {
steps {
echo "构建后使用junit插件 添加报告"
junit 'target/surefire-reports/*.xml'
}
}
stage('SonarQube') {
steps {
script{
scannerHome = tool 'SonarQubeScanner'
}
withSonarQubeEnv('SonarQube7.8') {
// This expands the evironment variables SONAR_CONFIG_NAME, SONAR_HOST_URL, SONAR_AUTH_TOKEN that can be used by any script.
sh "${scannerHome}/bin/sonar-scanner " +
"-Dsonar.projectKey=demo " +
"-Dsonar.host.url=http://10.97.8.185:6080 " +
"-Dsonar.java.binaries=target/classes " +
"-Dsonar.sources=. " +
"-Dsonar.sourceEncoding=UTF-8 " +
"-Dsonar.nodejs.executable=/opt/node/node-v16.17.0-linux-x64/bin/node " +
"-Dsonar.login=4ac58e617129ce88038d9741f5ba6a786cda3ed6"
}
}
}
stage('Code Coverage') {
steps {
echo "构建后使用jacoco插件 添加报告"
sh 'ls target'
//sh "mvn org.jacoco:jacoco-maven-plugin:0.8.8:report"
jacoco()
}
}
stage('artifacts') {
steps {
echo "归档文件,指定目录下build文件"
//archiveArtifacts artifacts: 'target/*.jar', followSymlinks: false
}
}
}
post {
always {
echo " always 构建后触发"
}
success{
echo " success 构建成功 "
}
failure{
echo " failure 构建失败 "
}
aborted{
echo " aborted 终止构建 "
}
}
}
到了这里,关于Jenkins通过git合并分支实现自动打包的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!