pipeline {
agent any
environment {
HOST_TEST = '172.16.195.190'
HOST_ONLINE = 'jenkins@39.101.219.110'
SOURCE_DIR = "$WORKSPACE/src/backend/bin/Release/net5.0/linux-x64/publish/*"
PROJECT_NAME = 'app_backend'
TARGET_DIR = "/data/ufun/${PROJECT_NAME}/"
PROCESS_NAME = "/data/ufun/${PROJECT_NAME}/backend"
BUILD_DIR = "$WORKSPACE/src/backend"
SERVICE_DIT = "/data/ufun/start_scripts/${PROJECT_NAME}.sh"
LOG_DIR = "/data/ufun/start_scripts/logs/${PROJECT_NAME}.log"
}
options{
timestamps()
ansiColor('xterm')
buildDiscarder(logRotator(daysToKeepStr: '6', numToKeepStr: '5'))
disableConcurrentBuilds()
}
parameters {
choice(
description: '你需要选择哪个环境进行部署 ?',
name: 'env',
choices: ['测试环境']
)
choice(
description: '你需要选择哪个分支进行部署 ?',
name: 'branchs',
choices: ['test','dev']
)
string(name: 'update', defaultValue: '', description: '本次更新内容?')
}
stages {
stage('获取仓库代码') {
steps {
script {
//deleteDir()
checkout([$class: 'GitSCM', branches: [[name: "*/$branchs"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[url: 'git@codefenglei.leihuofeng.net:edu/eiduapp.backend.git']]])
env.GIT_COMMIT_MSG = sh (script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim()
wrap([$class: 'BuildUser']) {
// some block
//def user = env.BUILD_USER
//manager.addShortText("启动人:${manager.envVars['BUILD_USER']}")
//manager.addShortText("启动人:${user}")
//buildName "#${BUILD_NUMBER}-^${BRANCH}^-${BUILD_USER}"
buildName "#${BUILD_NUMBER}-^${env.GIT_COMMIT_MSG}"
//输出最新一次提交信息
//HTTPD_LOCATION= sh( returnStdout: true, script: 'git show -s |grep -vE "commit|Date" |grep -v "^$"')
//修改Description"
//buildDescription "${HTTPD_LOCATION}"
}
}
}
}
stage('build') {
steps {
sh " echo 进入到${BUILD_DIR}目录下"
sh """
set +x
cd ${BUILD_DIR}
/data/apps/sdk5.0/dotnet publish -c Release -r linux-x64 --self-contained true --configfile ${BUILD_DIR}/nuget.config
"""
}
}
stage('deploy service') {
when {
expression {
params.env == '测试环境'
}
}
steps {
sshagent(credentials: ['beta']) {
//sh "ssh -o StrictHostKeyChecking=no ${HOST_TEST} uname -a"
//sh "rsync -avzP --exclude=appsettings.json ${SOURCE_DIR} ${HOST_TEST}:${TARGET_DIR}"
sh "rsync -avzP ${SOURCE_DIR} ${HOST_TEST}:${TARGET_DIR}"
sh 'echo "部署成功~"'
}
}
}
stage('start service') {
steps {
withEnv(['JENKINS_NODE_COOKIE=dontKillMe']) {
sh '''
export BUILD_ID=dontKillMe
echo "开始调用远程对auth进行重启"
ssh -o StrictHostKeyChecking=no ${HOST_TEST} "sh ${SERVICE_DIT} stop"
ssh -o StrictHostKeyChecking=no ${HOST_TEST} "sh ${SERVICE_DIT} start"
echo "查看启动日志"
ssh -o StrictHostKeyChecking=no ${HOST_TEST} "sleep 10;tail -20 ${LOG_DIR}"
'''
}
}
}
stage('check health') {
steps {
script{
//
script {
//def healthUrl = null
healthUrl = "ssh ${HOST_TEST} 'ps -ef|grep ${PROCESS_NAME}|grep -v grep'"
echo "睡眠1分钟,待应用完全准备好"
Thread.sleep((long) 1000 * 60 * 1)//睡眠1分钟
String shellStr = sh(script: "${healthUrl}", returnStdout: true)
echo "应用健康检查结果:${shellStr}"
if (shellStr.indexOf("${PROCESS_NAME}") > 0) {
echo "应用健康运行"
} else {
Thread.sleep((long) 1000 * 60 * 1)//睡眠0.5分钟
shellStr = sh(script: "${healthUrl}", returnStdout: true)
if (shellStr.indexOf("${PROCESS_NAME}") > 0) {
echo "应用健康运行"
} else {
throw new RuntimeException("应用不稳定,请检查服务是否正常")
}
}
}
//
}
}
}
}
post {
success {
dingtalk (
robot: 'han',
type: 'ACTION_CARD',
atAll: true,
title: '你有新的消息,请注意查收',
hideAvatar: false,
text:[
"### [${env.JOB_NAME}](${env.JOB_URL}) ",
'---',
'- 所属:后端服务',
"- 构建任务:${env.BUILD_DISPLAY_NAME}",
"- Git commit:${env.GIT_COMMIT_MSG}",
"- 本次更新内容:${params.update}",
"- 部署环境:${params.env}",
"- 持续时间:${currentBuild.durationString}".split("and counting")[0],
"- 构建人: <font color=#EE0000 >${currentBuild.buildCauses.shortDescription}</font>",
'- 构建结果:成功'
]
)
}
}
}
pipeline {
agent any
environment {
HOST_TEST = '172.16.195.190'
HOST_ONLINE = 'jenkins@39.101.219.110'
SOURCE_DIR = "$WORKSPACE/src/backend/bin/Release/net5.0/linux-x64/publish/*"
PROJECT_NAME = 'app_backend'
TARGET_DIR = "/data/ufun/${PROJECT_NAME}/"
PROCESS_NAME = "/data/ufun/${PROJECT_NAME}/backend"
BUILD_DIR = "$WORKSPACE/src/backend"
SERVICE_DIT = "/data/ufun/start_scripts/${PROJECT_NAME}.sh"
LOG_DIR = "/data/ufun/start_scripts/logs/${PROJECT_NAME}.log"
}
options{
timestamps()
ansiColor('xterm')
buildDiscarder(logRotator(daysToKeepStr: '6', numToKeepStr: '5'))
disableConcurrentBuilds()
}
parameters {
choice(
description: '你需要选择哪个环境进行部署 ?',
name: 'env',
choices: ['测试环境']
)
choice(
description: '你需要选择哪个分支进行部署 ?',
name: 'branchs',
choices: ['test','dev']
)
string(name: 'update', defaultValue: '', description: '本次更新内容?')
}
stages {
stage('获取仓库代码') {
steps {
script {
//deleteDir()
checkout([$class: 'GitSCM', branches: [[name: "*/$branchs"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[url: 'git@codefenglei.leihuofeng.net:edu/eiduapp.backend.git']]])
env.GIT_COMMIT_MSG = sh (script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim()
wrap([$class: 'BuildUser']) {
// some block
//def user = env.BUILD_USER
//manager.addShortText("启动人:${manager.envVars['BUILD_USER']}")
//manager.addShortText("启动人:${user}")
//buildName "#${BUILD_NUMBER}-^${BRANCH}^-${BUILD_USER}"
buildName "#${BUILD_NUMBER}-^${env.GIT_COMMIT_MSG}"
//输出最新一次提交信息
//HTTPD_LOCATION= sh( returnStdout: true, script: 'git show -s |grep -vE "commit|Date" |grep -v "^$"')
//修改Description"
//buildDescription "${HTTPD_LOCATION}"
}
}
}
}
stage('build') {
steps {
sh " echo 进入到${BUILD_DIR}目录下"
sh """
set +x
cd ${BUILD_DIR}
/data/apps/sdk5.0/dotnet publish -c Release -r linux-x64 --self-contained true --configfile ${BUILD_DIR}/nuget.config
"""
}
}
stage('deploy service') {
when {
expression {
params.env == '测试环境'
}
}
steps {
sshagent(credentials: ['beta']) {
//sh "ssh -o StrictHostKeyChecking=no ${HOST_TEST} uname -a"
//sh "rsync -avzP --exclude=appsettings.json ${SOURCE_DIR} ${HOST_TEST}:${TARGET_DIR}"
sh "rsync -avzP ${SOURCE_DIR} ${HOST_TEST}:${TARGET_DIR}"
sh 'echo "部署成功~"'
}
}
}
stage('start service') {
steps {
withEnv(['JENKINS_NODE_COOKIE=dontKillMe']) {
sh '''
export BUILD_ID=dontKillMe
echo "开始调用远程对auth进行重启"
ssh -o StrictHostKeyChecking=no ${HOST_TEST} "sh ${SERVICE_DIT} stop"
ssh -o StrictHostKeyChecking=no ${HOST_TEST} "sh ${SERVICE_DIT} start"
echo "查看启动日志"
ssh -o StrictHostKeyChecking=no ${HOST_TEST} "sleep 10;tail -20 ${LOG_DIR}"
'''
}
}
}
stage('check health') {
steps {
script{
//
script {
//def healthUrl = null
healthUrl = "ssh ${HOST_TEST} 'ps -ef|grep ${PROCESS_NAME}|grep -v grep'"
echo "睡眠1分钟,待应用完全准备好"
Thread.sleep((long) 1000 * 60 * 1)//睡眠1分钟
String shellStr = sh(script: "${healthUrl}", returnStdout: true)
echo "应用健康检查结果:${shellStr}"
if (shellStr.indexOf("${PROCESS_NAME}") > 0) {
echo "应用健康运行"
} else {
Thread.sleep((long) 1000 * 60 * 1)//睡眠0.5分钟
shellStr = sh(script: "${healthUrl}", returnStdout: true)
if (shellStr.indexOf("${PROCESS_NAME}") > 0) {
echo "应用健康运行"
} else {
throw new RuntimeException("应用不稳定,请检查服务是否正常")
}
}
}
//
}
}
}
}
post {
success {
dingtalk (
robot: 'han',
type: 'ACTION_CARD',
atAll: true,
title: '你有新的消息,请注意查收',
hideAvatar: false,
text:[
"### [${env.JOB_NAME}](${env.JOB_URL}) ",
'---',
'- 所属:后端服务',
"- 构建任务:${env.BUILD_DISPLAY_NAME}",
"- Git commit:${env.GIT_COMMIT_MSG}",
"- 本次更新内容:${params.update}",
"- 部署环境:${params.env}",
"- 持续时间:${currentBuild.durationString}".split("and counting")[0],
"- 构建人: <font color=#EE0000 >${currentBuild.buildCauses.shortDescription}</font>",
'- 构建结果:成功'
]
)
}
}
}