bi-gateway/Jenkinsfile
2025-07-15 13:12:39 +08:00

118 lines
5.1 KiB
Groovy
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

pipeline {
agent {
node {
label labelName
}
}
stages {
stage('初始化') {
steps {
script {
echo """ ==== 构建信息 ====
应 用 名:$serverName
分 支:$gitBranch
gitkey$gitkey
harborkey: $harborkey
镜像版本:$DOCKER_TAG """
echo """ $params"""
}
}
}
stage('编译') {
steps {
script {
container(labelName) {
dir(serverName) {
checkout([
$class: 'GitSCM',
branches: [[name: "*/$gitBranch"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]],
submoduleCfg: [],
userRemoteConfigs: [[url: gitPath, credentialsId: gitkey]],
])
/** 打印基础信息 **/
sh " echo 打印基础信息 "
sh " pwd "
sh " ls "
sh " mvn clean install -e -Dmaven.test.skip=true "
}
}
}
}
}
stage('镜像打包') {
steps {
script {
container(labelName) {
dir(serverName) {
withCredentials([usernamePassword(passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME', credentialsId: "harborkey",)]) {
sh 'echo "$DOCKER_PASSWORD" | docker login $DOCKER_REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh """
cd admin-application
docker build -t $DOCKER_IMAGE --build-arg JAR_FILE=$filepath --build-arg APP_NAME=$serverName .
docker push $DOCKER_IMAGE
"""
}
}
}
}
}
}
stage('发布服务') {
steps {
container(labelName) {
script{
//查询服务是否已经创建,如果已经创建再次发布只更新镜像
def result = ""
def global_config = true
def license = true
def harborkey = true
//服务是否已经部署
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
result = sh(returnStdout: true, script: "kubectl get deployment $serverName -n $namespace ").trim()
}
if(result==""){
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
sh "helm delete $serverName -n $namespace "
}
sh "helm repo update "
sh "helm install --set image.repository=$DOCKER_REGISTRY,image.tag=$DOCKER_TAG,nameOverride=$serverName $serverName do1-chart/base-server --version=$CHART_VERSION -n $namespace "
}else{
sh "kubectl set image deploy $serverName *=$DOCKER_IMAGE -n $namespace"
}
}
}
}
}
}
environment {
dockerFile = "Dockerfile"
filepath = " ./target/**.jar "
labelName = "maven"
DOCKER_REGISTRY="$DOCKER_REGISTRY/$namespace/$serverName".replaceAll("//","/")
DOCKER_IMAGE="$DOCKER_REGISTRY:$DOCKER_TAG".replaceAll("//","/")
CHART_VERSION = "1.1.0"
}
// 说明: 参数初始化脚本,默认情况下是注释的,如果需要新增参数可以通过可视化界面添加也可以通过此脚本添加,一般用来初始化和调整顺序,注意,使用此代码片段添加参数,参数会在第一次构建生效,且会覆相同的参数值,以代码片段中的参数配置重新添加,意思就是如果初始化后,下面代码 parameters 没有 注释,那么每次运行后之前添加或修改的参数都会被下面的参数和默认值替换
parameters {
string(name: 'gitBranch', defaultValue: 'dev', description: '选择需要发布的分支')
string(name: 'gitPath', defaultValue: 'https://git.qiweioa.cn/bi-system/bi-admin.git', description: 'git 仓库地址')
string(name: 'serverName', defaultValue: 'bi-admin-dev', description: 'KubeSphere服务名')
string(name: 'namespace', defaultValue: 'bi-dev', description: '需要发布的k8s命名空间每个项目都应该有一个命名空间且在集群中唯一')
string(name: 'gitkey', defaultValue: 'denghuizhi', description: 'git仓库访问凭证配置在流水线访问凭证中gitkey为全局的配置在Jenkins中不需要手动创建模版中的值由主流程传递过来')
string(name: 'harborkey', defaultValue: 'harborkey', description: '镜像仓库访问凭证harborkey 为默认值,镜像仓库地址发生变更时需要修改此参数,自定义凭证需要在流水线凭证中创建')
string(name: 'DOCKER_REGISTRY', defaultValue: 'harbor.uat.do1.com.cn:6001/do1cloud/', description: '镜像仓库,默认值 harbor.uat.do1.com.cn/do1cloud/ 为公司全局镜像仓库如有需要可以自行更改如果使用了其他镜像仓库地址需要修改dockerkey的值')
string(name: 'DOCKER_TAG', defaultValue: 'dev', description: '镜像版本')
}
}