单pipeline部署一套代码,多项目
pipeline {
agent any
parameters {
gitParameter(name: 'BRANCH_TAG', type: 'PT_BRANCH_TAG', branchFilter: 'origin/(.*)', defaultValue: 'main', selectedValue: 'DEFAULT', sortMode: 'DESCENDING_SMART', description: '请选择需要部署的代码:')
choice(name: 'mode', choices: ['deploy','rollback'], description: '请选择发布或者回滚?')
//string(name: 'iname', defaultValue: 'kubiex-asset', description: '服务名称')
extendedChoice(name: 'ProjectName',
type: 'PT_CHECKBOX',
description: '请勾选所要发布的项目模块',
quoteValue: false,
saveJSONParameterToFile: false,
value: 'legend-hpw-common,hyperw-common,option-common,hyperw-gateway,hyperw-account,hyperw-agent,hyperw-option,hyperw-binary-option,hyperw-user,hyperw-admin,hyperw-assets,hyperw-system,legend-index,legend-index-bloomberg-calculation,option-core,option-index,option-ws',
visibleItemCount: 16,
multiSelectDelimiter: ',',
defaultValue: ''
)
string(name: 'ENVMENT', defaultValue: 'test', description: '环境参数')
}
tools{
jdk 'java8'
maven 'Maven3.8.7'
}
environment {
project_location = "${iname}"
}
stages {
stage('clean'){
steps {
cleanWs(
cleanWhenAborted: true,
cleanWhenFailure: true,
cleanWhenNotBuilt: true,
cleanWhenSuccess: true,
cleanWhenUnstable: true,
cleanupMatrixParent: true,
disableDeferredWipeout: true,
deleteDirs: true
)
}
}
stage('从 gitlab 中拉取代码') {
steps {
deleteDir()
checkout([$class: 'GitSCM',
branches: [[name: "${BRANCH_TAG}"]],
gitTool: 'Default',
userRemoteConfigs: [[url: 'https://gitlab.yunson.com/test/option/hyperw.git', credentialsId: 'gitlab-deploy',]]
])
}
}
stage('mvn install') {
steps {
script{
if ( env.ProjectName.isEmpty() ) {
echo "ProjectName not specified."
autoCancelled = true
error('Aborting the build.')
}
else {
for (iname in ProjectName.tokenize(',')) {
if (iname == 'hyperw-gateway') {
sh """
cd hyperw-master/gateway
mvn clean deploy -DskipTests
cd ../../
"""
} else if (iname == 'hyperw-account' || iname == 'hyperw-option' || iname == 'hyperw-binary-option') {
sh """
cd hyperw-master/${iname}
mvn clean deploy -DskipTests
cd ../../
"""
} else if (iname == 'option-common' || iname == 'option-core' || iname == 'option-index' || iname == 'option-ws') {
sh """
cd option-master/${iname}
rm -rf .mvn/
mvn clean deploy -DskipTests
cd ../../
"""
}
else {
sh """
cd ${iname}-master
sed -i 's/8.28.19.71/172.16.0.60/g' pom.xml
mvn clean deploy -DskipTests
cd ../
"""
}
}
}
}
}
}
stage('Build docker image'){
steps{
script{
if ( env.ProjectName.isEmpty() ) {
echo "ProjectName not specified."
autoCancelled = true
error('Aborting the build.')
}
else {
for (iname in ProjectName.tokenize(',')) {
if (!(iname.contains('common'))) {
if (iname == 'hyperw-gateway') {
sh """
cd hyperw-master/gateway
docker build --build-arg JOB_NAME=gateway -t harbor.yunson.com/test/${iname}:$ENVMENT .
cd -
"""
} else if (iname == 'hyperw-account' || iname == 'hyperw-option' || iname == 'hyperw-binary-option') {
sh """
cd hyperw-master/
docker build --build-arg JOB_NAME=${iname} -t harbor.yunson.com/test/${iname}:$ENVMENT .
cd ../../
"""
} else if (iname == 'option-core' || iname == 'option-index' || iname == 'option-ws') {
sh """
cd option-master/
docker build --build-arg JOB_NAME=${iname} -t harbor.yunson.com/test/${iname}:$ENVMENT .
cd ../../
"""
}
else {
sh """
cd ${iname}-master
docker build --build-arg JOB_NAME=${iname} -t harbor.yunson.com/test/${iname}:$ENVMENT .
cd ../
"""
}
}
}
}
}
}
}
stage('Push image to hub'){
steps{
script{
withCredentials([usernamePassword(credentialsId: 'harbor-secret-dev', passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'docker login -u ${username} -p ${password} harbor.yunson.com'
}
if ( env.ProjectName.isEmpty() ) {
echo "ProjectName not specified."
autoCancelled = true
error('Aborting the build.')
}
else {
for (iname in ProjectName.tokenize(',')) {
if (!(iname.contains('common'))) {
sh """
docker push harbor.yunson.com/test/${iname}:$ENVMENT
docker rmi -f harbor.yunson.com/test/${iname}:$ENVMENT
"""
}
}
}
}
}
}
stage('deploy Server'){
steps{
script{
if ( env.ProjectName.isEmpty() ) {
echo "ProjectName not specified."
autoCancelled = true
error('Aborting the build.')
}
else {
for (iname in ProjectName.tokenize(',')) {
if (!(iname.contains('common'))) {
sh """
curl -X PUT \
-H "content-type: application/json" \
-H "Cookie: KuboardUsername=admin; KuboardAccessKey=ccpyiaxei7i8.disiejnk4dg5pfjlobgmflkuefkufdwf" \
-d '{"kind":"deployments","namespace":"test","name":"${iname}","images":{"harbor.yunson.com/bktest/${iname}":"harbor.yunson.com/test/${iname}:${ENVMENT}"}}' \
"http://69.36.89.2:18085/kuboard-api/cluster/Test/kind/CICDApi/admin/resource/updateImageTag"
curl -X PUT \
-H "Content-Type: application/yaml" \
-H "Cookie: KuboardUsername=admin; KuboardAccessKey=ccpyiaxei7i8.disiejnk4dg5pfjlobgmflkuefkufdwf" \
-d '{"kind":"deployments","namespace":"test","name":"${iname}"}' \
"http://69.36.89.2:18085/kuboard-api/cluster/Test/kind/CICDApi/admin/resource/restartWorkload"
"""
}
}
}
}
}
}
}
}
效果如下: