Files
school-developer/deploy-tools/Jenkinsfile.back
wangdongjun b4b6389b88 1
2026-01-06 16:00:48 +08:00

139 lines
5.7 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.

node("slave") {
def project = 'city-zhxy-front' //镜像名称
def gitSource = 'ssh://git@code.cyweb.top:30033/scj/zhxy-city/backend-front.git' //代码地址
def toEmail = '493840844@qq.com,374362909@qq.com' //需要接收邮件提醒的
// def namespace = "fund" //k8snamespaces
/******* 自定义变量在jenkins必须对应设置 start *********/
def namespace = "${namespace}" //k8snamespaces
def branch = "${branch}" //当前分支变量
def profile = "${profile}" //dev
def remoteHost = "${remoteHost}" //部署服务器的ip
def remoteHostAuth = "${remoteHostAuth}" //部署服务器的凭证
/******* 自定义变量在jenkins必须对应设置 end *********/
def tag = "jenkins-2021-${env.BUILD_NUMBER}" //镜像tag
def registryHost = 'docker.registry.cyweb.net:5000' //har仓库
def harBorNamespace = 'project' //镜像存储目录
def artifactId = ''
def version = ''
def needSonarQube = false //是否开启代码质量检测
def isSuccess = true
try{
stage("获取最新代码"){
sh 'printenv'
echo "当前分支${branch}"
git branch: "${branch}", credentialsId: 'git-ssh-auth', url: "${gitSource}"
}
stage("打包编译"){
nodejs(cacheLocationStrategy: workspace(), configId: '066d1e6d-2a35-4832-96c1-78970fb7d78b', nodeJSInstallationName: 'node-10.15.1') {
sh 'npm install'
sh 'npm rebuild node-sass'
sh 'npm run build'
}
}
if(needSonarQube){
stage("代码审查"){
def scannerHome = tool 'sonar-scanner'
withSonarQubeEnv('SonarQuber-Server') {
sh "${scannerHome}/bin/sonar-scanner " +
"-Dsonar.projectKey=${project}-${branch} " +
"-Dsonar.projectName=基金-开发分支 " +
"-Dsonar.sourceEncoding=UTF-8 " +
"-Dsonar.projectVersion=1.0 " +
"-Dsonar.java.binaries=./target/classes " +
"-Dsonar.sources=. "+
"-Dsonar.exclusions=**/test/**,**/target/**,**/main/resources/**,**/deploy-tools/** "+
"-Dsonar.java.source=1.8 "+
"-Dsonar.java.target=1.8"
// sh "${scannerHome}/bin/sonar-scanner -Dproject.settings=./deploy-tools/sonar-project.properties"
}
sleep(10)
timeout(time: 10, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
echo "代码质量检测不通过"
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}else{
echo "通过了代码质量检测"
}
}
}
}
stage("docker 操作"){
tool name: 'docker: 1.12.6', type: 'dockerTool'
withCredentials([usernamePassword(credentialsId: '193f70e2-0fc6-43dc-a1c6-c026d0fa3523', passwordVariable: 'password', usernameVariable: 'username')]) {
echo "登录docker harbor"
sh "docker login ${registryHost} -u ${username} -p ${password} "
// sh ' echo ${password} | docker login ${registryHost} -u Wang628625!@# --password-stdin '
echo "构建镜像"
sh "docker build --build-arg profile=${profile} -t ${registryHost}/${harBorNamespace}/${project}/${project}-${profile}:${tag} -f deploy-tools/Dockerfile . "
echo "提交镜像"
sh "docker push ${registryHost}/${harBorNamespace}/${project}/${project}-${profile}:${tag} "
}
}
stage("更新服务"){
echo "开始更新服务"
def remote = [:]
remote.name = "k8s-master-dev"
remote.host = "${remoteHost}"
remote.allowAnyHosts = true
withCredentials([usernamePassword(credentialsId: "${remoteHostAuth}", passwordVariable: 'passowrd', usernameVariable: 'username')]) {
remote.user = "${username}"
remote.password = "${passowrd}"
sshCommand remote: remote, command: 'source ~/.bash_profile'
sshCommand remote: remote, command: "kubectl set image deployment/front front=${registryHost}/${harBorNamespace}/${project}/${project}-${profile}:${tag} --namespace=${namespace}"
}
}
}catch(Exception e){
isSuccess = false
currentBuild.result = 'FAILURE'
throw e; //必须跑出异常 否则构建失败结果无法被jenkins捕获
}finally{
stage("发送邮件通知"){
if(isSuccess)
{
emailext body: '${FILE,path="deploy-tools/email-notice-success.html"}', subject: '构建通知:{$PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS} 请勿回复', to: "$toEmail"
}else{
emailext body: '${FILE,path="deploy-tools/email-notice-false.html"}', subject: '构建通知:{$PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS} 请勿回复', to: "$toEmail"
}
// echo "clear workspace......"
// deleteDir()
}
}
}