youlai-mall/docs/devops/DockerJenkinsFile

192 lines
9.1 KiB
Plaintext
Raw 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.

properties([
parameters([
[$class: "ChoiceParameterDefinition",
description: "你需要选择哪条分支进行构建?",
name: "branch_name",
choices: ["youlai_k8s_deploy", "master"]
],
[$class: "ChoiceParameter",
choiceType: "PT_SINGLE_SELECT",
description: "你需要选择哪个微服务模块进行构建?",
filterLength: 1,
filterable: false,
name: "module_name",
randomName: "choice-parameter-5631314439613978",
script: [
$class: "GroovyScript",
fallbackScript: [
classpath: [],
sandbox: false,
script:
'''return["Could not get module_name"]'''
],
script: [
classpath: [],
sandbox: false,
script:
'''return["youlai-gateway", "youlai-auth","youlai-admin/admin-boot","mall-oms/oms-boot","mall-pms/pms-boot","mall-sms/sms-boot","mall-ums/ums-boot"]
'''
]
]
],
[$class: "CascadeChoiceParameter",
choiceType: "PT_SINGLE_SELECT",
description: "你需要选择哪台机器进行部署微服务?",
filterLength: 1,
filterable: false,
name: "deploy_on",
randomName: "choice-parameter-5631314456178619",
referencedParameters: "module_name",
script: [
$class: "GroovyScript",
fallbackScript: [
classpath: [],
sandbox: false,
script:
'''return["Could not get Environment from module_name Param"]'''
],
script: [
classpath: [],
sandbox: false,
script:
'''if (module_name.equals("youlai-gateway")){
return["a.youlai.tech"]
}
else if(module_name.equals("mall-ums/ums-boot")){
return["c.youlai.tech"]
}
else if(module_name.equals("mall-oms/oms-boot")){
return["d.youlai.tech"]
}
else {
return["f.youlai.tech"]
}
'''
]
]
]
])
])
// 以上代码,需要插件支持,主要作用是:选择具体的微服务,部署至对应的机器
pipeline { // 直接上机器,在机器上启容器
agent {
node {
label "maven"
}
}
environment {
// 自建harbor仓库的namespace
docker_hub_namespace = "youlai"
// docker_hub_namespace = "youlaiwuhui"
// 自建镜像仓库地址
docker_hub = "k8s-harbor:30002"
docker_hub_ext = "harbor.howlaisi.com:30002"
// docker_hub = "https://registry.cn-hangzhou.aliyuncs.com"
// 在jenknis或kubesphere上面的凭证
docker_hub_id = "youlai-zhangjialin-myself-harbor-account"
// docker_hub_id = "zhangjialin-aliyun-pingzheng"
// k8s 上面的 namespace
k8s_namespace = "youlai-mall"
GIT_COMMIT_ID = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
// BUILD_NUMBER 这个变量从jenkins传递过来
current_build_number = "${BUILD_NUMBER}"
// 在k8s上面配置的id
KUBECONFIG_CREDENTIAL_ID = "youlai-kubeconfig"
}
stages {
stage ("打印相关变量") {
steps{
echo "docker_hub_namespace信息为: ${docker_hub_namespace}"
// 获取commit信息用于后面打tag
echo "commit信息为${env.GIT_COMMIT_ID}"
echo "current_build_number信息为${env.current_build_number}"
script {
// 因为传递过来的module有可能是youlai-admin/admin-boot形式的打镜像时会失败
env.module_name_prefix = "${module_name}".split("/")[0]
env.module_name_suffix = "${module_name}".split("/")[-1]
// 本端tag名
env.local_tag = "${module_name_suffix}:${current_build_number}_${GIT_COMMIT_ID}"
// 远端tag名必须以这种方式命令才能push到远端
env.remote_tag = "${docker_hub}/${docker_hub_namespace}/${local_tag}"
// 外网访问下载的远端tag
env.remote_tag_ext = "${docker_hub_ext}/${docker_hub_namespace}/${local_tag}"
echo "module_name信息为: ${module_name}"
echo "module_name_prefix信息为: ${module_name_prefix}"
echo "module_name_suffix信息为: ${module_name_suffix}"
echo "local_tag信息为${env.local_tag}"
echo "remote_tag信息为${env.remote_tag}"
echo "remote_tag_ext信息为${env.remote_tag_ext}"
}
}
}
stage("checkout代码") {
steps {
//git branch: "${branch_name}", credentialsId: 'zhangjialin-youlai-mall-pingzheng', url: 'https://gitee.com/youlaitech/youlai-mall.git'
//checkout([
//$class: 'GitSCM',
//branches: [[name: "${branch_name}"]],
//extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]],
//userRemoteConfigs: [[credentialsId: 'zhangjialin-youlai-mall-pingzheng', url: 'https://gitee.com/youlaitech/youlai-mall.git']]])
sh "du -h --max-depth=1"
}
}
stage("读取maven配置"){
steps {
script {
// 需要用到插件Pipeline Utility Steps参考https://www.jianshu.com/p/29403ecf7fc2
def pom = readMavenPom file: "${module_name}/pom.xml"
def properties = pom.properties
env.service_port = properties["service.port"]
env.service_nodeport = properties["service.nodeport"]
sh "echo service_port: ${service_port}"
sh "echo service_nodeport: ${service_nodeport}"
}
}
}
stage("打包镜像") {
steps {
script {
container ('maven') {
// 最外边
sh "mvn clean install -Dmaven.test.skip=true"
sh "mvn -f ${module_name} clean package dockerfile:build -Ddockerfile.tag=${current_build_number}_${GIT_COMMIT_ID} -Dmaven.test.skip=true -Dspring.profiles.active=dev"
withCredentials([usernamePassword(credentialsId: "${docker_hub_id}", passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
sh 'echo "$DOCKER_PASSWORD" | docker login http://k8s-harbor:30002 -u "$DOCKER_USERNAME" --password-stdin'
sh "docker tag ${env.local_tag} ${env.remote_tag}"
sh "docker push ${env.remote_tag}"
}
}
}
}
}
stage("自动部署至docker容器") {
agent none
steps {
script {
panlong_deploy_script_url = "\'https://gitee.com/youlaitech/youlai-mall/raw/${branch_name}/deploy.sh?access_token=a646170fbfafe84d3412ff594d530b6d&ref=${branch_name}\'"
script_dir = "/opt/youlai/script"
withCredentials([usernamePassword(credentialsId: "${docker_hub_id}", passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
sshPublisher(publishers: [
sshPublisherDesc(
configName: "${deploy_on}",
transfers: [
sshTransfer(cleanRemote: false,
excludes: '',
execCommand: "mkdir -p ${script_dir} && cd ${script_dir} && rm -rf deploy.sh.${module_name_suffix} && curl -X GET --header 'Content-Type: application/json;charset=UTF-8' ${panlong_deploy_script_url} -o deploy.sh.${module_name_suffix} && sh deploy.sh.${module_name_suffix} ${remote_tag_ext} ${service_port} ${docker_hub_namespace} ${module_name_suffix} ${DOCKER_USERNAME} ${DOCKER_PASSWORD}",execTimeout: 1200000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: true)])
}
}
}
}
}
}