pipeline 打包maven项目, 多目录, 不同目录结构, 打包完成后使用docker进行build 构建镜像
以下是pipeline文件, 根据实际情况修改,这里只是做个简单的例子
def relase = "prod" //构建的版本
def branch = "main" //分支
def baseImage = "k9scc/base-env:maven3-java8-git" //构建maven的基础镜像
def folders = ['gateway', 'background', 'management','auth', 'business-user', 'data-interaction','inter-message','modularize-e-business','order','payment','shop-product','user'] //项目目录列表
def gitlabUrl = "http://gitlab地址"
def qyWechatUrl ="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx-xxxx-xxxx-xxxx-4xxxxx" //发信webhook
def BuilAndPush = { folder ->
def dockerUrl = "registry.cn-chengdu.aliyuncs.com/仓库名/jar:$folder-$relase-${env.BUILD_NUMBER}"
def fileDir = "${WORKSPACE}@2/$folder"
def bootDir = "${WORKSPACE}@2/$folder/$folder-boot" //可根据实际情况修改, 如果目录统一,可以不进行判断
if (fileExists("$bootDir")) {
dir ("$bootDir") {
script {
sh 'pwd'
sh 'ls -lsa'
sh "docker build -t ${dockerUrl} . && docker push ${dockerUrl}"
}
}
} else if(fileExists("$fileDir")) {
dir ("$fileDir") {
script {
sh 'pwd'
sh 'ls -lsa'
sh "docker build -t ${dockerUrl} . && docker push ${dockerUrl}"
}
}
} else {
echo "$folder 文件夹不存在"
}
}
pipeline {
agent any
stages {
stage('build') {
agent{
docker {
image "$baseImage"
}
}
steps {
git branch: "$branch", credentialsId: 'gitlab-root', url: "$gitlabUrl"
sh 'ls -lsa'
sh 'pwd'
sh 'mvn -v'
sh "mvn clean package -Dmaven.test.skip=true -P$relase"
}
}
stage('并发构建') {
steps {
script {
def buildTasks = [:]
folders.each { folder ->
buildTasks["构建 $folder"] = {
BuilAndPush(folder)
}
}
parallel buildTasks
}
}
}
}
post {
always {
qyWechatNotification mentionedId: '', mentionedMobile: '', moreInfo: '', webhookUrl: "$qyWechatUrl"
}
}
}