pipeline {
agent {label 'local-slave'}
environment {
def imageName = "${project_name}/${single_project_name}"
def build_tag = "${registry_url}/${group_id}/${single_project_name}:${BUILD_NUMBER}"
}
parameters {
string(name:'project_name', defaultValue: 'wukong-esthesia', description: '项目project名称')
string(name:'group_id', defaultValue: 'product', description: '项目groupId')
string(name:'registry_url', defaultValue: 'hub.xxxapp.com', description: '镜像仓库地址')
}
tools {
maven "maven-3.6.3"
}
//git代码拉取
stages {
//stage('check out') {
// steps {
// checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@gitlab.xxxapp.com:product/xxx.git']]])
//}
//}
stage("Checkout") {
steps {
checkout([$class: 'GitSCM',
branches: [
[name: '**']
],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'LocalBranch', localBranch: "**"]],
submoduleCfg: [],
userRemoteConfigs: [[ url: 'git@gitlab.xxxapp.com:product/xxx.git']]
])
}
}
stage("Branch To Build") {
steps {
script {
def gitBranches = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref --all | sed s:origin/:: | sort -u')
env.BRANCH_TO_BUILD = input message: 'Please select a branch', ok: 'Continue',
parameters: [choice(name: 'BRANCH_TO_BUILD', choices: gitBranches, description: 'Select the branch to build?')]
}
git branch: "${env.BRANCH_TO_BUILD}", url: 'git@gitlab.xxxapp.com:product/xxx.git'
}
}
//本地镜像删除(如果存在)、生成新镜像
stage('building dependency ') {
steps {
sh '''
containerStr=`docker ps -a | grep -w ${imageName} | awk '{print $1}'`
imageStr=`docker images | grep -w $imageName | awk '{print $3}'`
echo "container id:$containerStr"
echo "image id:$imageStr"
if [ "$imageStr" != "" ] ; then
if [ "$containerStr" != "" ] ; then
#停掉容器
docker stop `docker ps -a | grep -w ${imageName} | awk '{print $1}'`
#删除容器
docker rm `docker ps -a | grep -w ${imageName} | awk '{print $1}'`
#删除镜像
docker rmi --force ${imageName}
else
#删除镜像
docker rmi --force ${imageName}
fi
fi
source /etc/profile
mvn clean install -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true -U
mvn -f ${single_project_name} clean install dockerfile:build -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true
'''
}
}
//删除仓库老镜像、重命名镜像、上传重命名新镜像
stage('upload image') {
steps {
script {
sh '''
#(1)判断本地是否已存在该项目的镜像,存在则先删除
regName=${registry_url}/$imageName
imageStr=`docker images | grep -w $regName | awk '{print $3}'`
if [ "$imageStr" != "" ] ; then
#将仓库中的镜像名称删除
docker rmi ${registry_url}/${group_id}/${single_project_name}:${BUILD_NUMBER}
fi
#(2)必须要先将镜像的名称给变成 域名或ip/镜像名
docker tag ${imageName} ${registry_url}/${group_id}/${single_project_name}:${BUILD_NUMBER}
#(3)推送到远端仓库
docker push ${registry_url}/${group_id}/${single_project_name}:${BUILD_NUMBER}
#(4)删除本地镜像
docker rmi $imageName
#(5)删除本地镜像
docker rmi ${registry_url}/${group_id}/${single_project_name}:${BUILD_NUMBER}
'''
}
}
}
stage('update yaml'){
steps{
sh "sed -i 's/<GROUP_ID>/${group_id}/' k8s.yaml"
sh "sed -i 's/<PROJECT_NAME>/${single_project_name}/' k8s.yaml"
sh "sed -i 's/<BUILD_TAG>/${BUILD_NUMBER}/' k8s.yaml"
sh "sed -i 's/<BRANCH_NAME>/${single_project_name}/' k8s.yaml"
}
}
// 实际部署的机器上运行,从镜像仓库下载新镜像、运行、启动新镜像
stage('publish new images') {
steps {
echo "6. Deploy Stage"
sh "kubectl apply -f k8s.yaml"
}
}
}
}
jenkins-k8s-Pipeline 流水线模板
最新推荐文章于 2025-04-04 14:24:48 发布