// 定义参数化构建的参数
properties([
parameters([
booleanParam(name: 'ApiManage', defaultValue: false, description: '是否部署api-manage'),
booleanParam(name: 'ApiManageWeb', defaultValue: false, description: '是否部署api-manage-web'),
booleanParam(name: 'AuthManage', defaultValue: false, description: '是否部署AuthManage'),
booleanParam(name: 'AuthManageWeb', defaultValue: false, description: '是否部署AuthManageWeb'),
choice(name: 'deployEnv', choices: ['test', 'product'], description: '部署test环境还是product环境'),
])
])
//代码检出
def GetCode(srcUrl,branchName,relativeDir,credentialsId) {
checkout([
$class: 'GitSCM',
branches: [[name: "${branchName}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'RelativeTargetDirectory', relativeTargetDir: "${relativeDir}"],
[$class: 'CheckoutOption', timeout: 30],
[$class: 'CloneOption', noTags: false, reference: '', shallow: false, timeout: 30],
[$class: 'LocalBranch', localBranch: "${branchName}"]
],
submoduleCfg: [],
userRemoteConfigs: [
[credentialsId: "${credentialsId}", url: "${srcUrl}"]
]
])
}
//示例:PackageBuild("wangfeng-service/wangfeng-assess","registry.cn-beijing.aliyuncs.com","registry-vpc.cn-beijing.aliyuncs.com","wangfeng-assess")
// Java 项目构建
def PackageBuild(relativeDir,pomRepoUrl,repoUrl,imageName){
dir("${relativeDir}"){
def gitcommitid = sh(returnStdout: true, script:'git rev-parse --short HEAD').trim()
echo "=============================================Git-Commit-id: ${gitcommitid}"
def pom = readMavenPom file: 'pom.xml'
sh "mvn clean && mvn package -DskipTests && "
sh "docker tag ${pomRepoUrl}/wangfeng/${imageName}:${pom.version} ${repoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
sh "docker rmi ${pomRepoUrl}/wangfeng/${imageName}:${pom.version}"
withCredentials([usernamePassword(credentialsId: 'ali-docker-registry', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUser')]) {
sh "docker login --username=${dockerUser} -p ${dockerPassword} ${repoUrl}"
sh "docker push ${repoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
}
echo "=============================================DockerImage: ${pomRepoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
if(params.deployEnv == "test"){
sh "kubectl --kubeconfig /var/lib/jenkins/k8s/config --context k8s-ali-admin set image deployment/${imageName} ${imageName}=${repoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER} -n oppc"
}
if(params.deployEnv == "product"){
print "生产环境需手动发布!"
}
}
}
// vue前端构建
def ManageWebBuild(relativeDir,pomRepoUrl,repoUrl,imageName){
dir("${relativeDir}"){
sh "npm install --registry=https://registry.npm.taobao.org"
if(params.deployEnv == "test"){
sh 'npm run build'
}
if(params.deployEnv == "product"){
sh 'npm run build:prod'
}
sh "\\cp -rf dist docker"
sh "cd docker && docker build -t ${pomRepoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER} . "
sh "docker tag ${pomRepoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER} ${repoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
sh "docker rmi ${pomRepoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
withCredentials([usernamePassword(credentialsId: 'ali-docker-registry', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUser')]) {
sh "docker login --username=${dockerUser} -p ${dockerPassword} ${repoUrl}"
sh "docker push ${repoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
}
echo "=============================================DockerImage: ${pomRepoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
if(params.deployEnv == "test"){
sh "kubectl --kubeconfig /var/lib/jenkins/k8s/config --context k8s-ali-admin set image deployment/${imageName} ${imageName}=${repoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER} -n oppc"
}
if(params.deployEnv == "product"){
print "生产环境需手动发布!"
}
}
}
// node后端构建
def ManageBuild(relativeDir,pomRepoUrl,repoUrl,imageName,versionDir){
dir("${relativeDir}"){
sh "cd ${versionDir} && pwd && node createVersion.js"
sh "docker build -t ${pomRepoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER} . "
sh "docker tag ${pomRepoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER} ${repoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
sh "docker rmi ${pomRepoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
withCredentials([usernamePassword(credentialsId: 'ali-docker-registry', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUser')]) {
sh "docker login --username=${dockerUser} -p ${dockerPassword} ${repoUrl}"
sh "docker push ${repoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
}
echo "=============================================DockerImage: ${pomRepoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER}"
if(params.deployEnv == "test"){
sh "kubectl --kubeconfig /var/lib/jenkins/k8s/config --context k8s-ali-admin set image deployment/${imageName} ${imageName}=${repoUrl}/wangfeng/${imageName}:v${BUILD_NUMBER} -n oppc"
}
if(params.deployEnv == "product"){
print "生产环境需手动发布!"
}
}
}
node {
MVNHOME ='/u01/install/apache-maven-3.6.2'
JAVA_HOME = "/u01/install/jdk1.8.0_201"
NODE_HOME="/u01/install/node"
//内网git仓
GIT_REPO_URL = 'git@172.17.252.143'
//公网阿里docker仓
POM_REPO_URL = 'registry.cn-beijing.aliyuncs.com'
//内网阿里docker仓
REPO_URL = 'registry-vpc.cn-beijing.aliyuncs.com'
//jenkins定义拉取git代码凭据
JENKINS_GIT_PUT_KEY = 'jenkins-pub-key'
if(params.deployEnv == "test"){
BRANCH_NAME = 'test'
}
if(params.deployEnv == "product"){
BRANCH_NAME = 'master'
}
// 环境变量
env.PATH = "${JAVA_HOME}/bin:${MVNHOME}/bin:${env.PATH}:${NODE_HOME}/bin:$PATH"
env.JAVA_HOME = "${JAVA_HOME}"
// 拉取代码、编译、打包构建
if( params.ApiManage ) {
stage('部署api-manage') {
GetCode(GIT_REPO_URL+":oppc/oppcApiManage.git",BRANCH_NAME,"oppcApiManage",JENKINS_GIT_PUT_KEY)
ManageBuild("oppcApiManage",POM_REPO_URL,REPO_URL,"api-manage","apiManage")
}
}
if( params.ApiManageWeb ) {
stage('部署api-manage-web') {
GetCode(GIT_REPO_URL+":oppc/oppcApiManage.git",BRANCH_NAME,"oppcApiManage",JENKINS_GIT_PUT_KEY)
ManageWebBuild("oppcApiManage/apiManageWeb",POM_REPO_URL,REPO_URL,"api-manage-web")
}
}
if( params.AuthManage ) {
stage('部署AuthManage') {
GetCode(GIT_REPO_URL+":oppc/ommpAuthManage.git",BRANCH_NAME,"ommpAuthManage",JENKINS_GIT_PUT_KEY)
ManageBuild("ommpAuthManage",POM_REPO_URL,REPO_URL,"auth-manage","ommpAuthManageApi")
}
}
if( params.AuthManageWeb ) {
stage('部署AuthManageWeb') {
GetCode(GIT_REPO_URL+":oppc/ommpAuthManage.git",BRANCH_NAME,"ommpAuthManage",JENKINS_GIT_PUT_KEY)
ManageWebBuild("ommpAuthManage/ommpAuthManageWeb",POM_REPO_URL,REPO_URL,"auth-manage-web")
}
}
}