代码拉取完成,页面将自动刷新
//脚本化流水线
node {
// 定义全局环境变量
def remote_name = '8.134.51.43'
def remote_host = '8.134.51.43'
def remote_user = 'root'
def remote_password = ''
def remote_path = '/data/run/java/test'
def remote = [:]
def file_name = 'email-test'
remote.name = "${remote_name}"
remote.host = "${remote_host}"
remote.user = "${remote_user}"
remote.password = "${remote_password}"
remote.allowAnyHosts = true
// 项目地址
def git_address = 'https://gitee.com/the_jun/email_test_demo.git'
// 分支
def git_branch = 'master'
// 帐号id
def credentialsId = 'hejj'
// maven的profile环境
def profile = 'dev'
stage('checkout and build') {
stage('checkout from git') {
echo "checkout from ${git_address} : ${git_branch}"
checkout([$class: 'GitSCM', branches: [[name: "*/${git_branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: credentialsId, url: "${git_address}"]]])
}
stage('build') {
sh 'pwd'
sh 'mvn -version'
sh 'java -version'
echo 'build...'
// 指定profile 配置id 为dev 的build 参数打包
sh "mvn -B -P${profile} -DskipTests clean install"
}
}
stage('remote ssh put jar file') {
stage('remote ssh ===> put dongxing-task') {
def put_file = './target/email-test-demo-0.0.1-SNAPSHOT.jar'
sshPut remote: remote, from: put_file, into: "${remote_path}"
}
}
stage('deployment') {
// 发布文件名
def deployment_name = "${file_name}.jar"
def log_name = "${file_name}.log"
// 执行的shell命令
// 修改jar包文件名
def change_name_script = "mv ${remote_path}/email-test-demo-0.0.1-SNAPSHOT.jar ${remote_path}/${deployment_name}"
def check_running_script = "ps -ef | grep ${deployment_name} | grep -v grep |wc -l"
def get_pid_script = "ps -ef | grep ${deployment_name} | grep -v grep | awk '{printf \" \"\$2}'"
def deployment_script = "nohup java -jar -Xms256m -Xmx512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y,address=5585 -Dspring.profiles.active=${profile} ${remote_path}/${deployment_name} > ${remote_path}/${log_name} 2>&1 &"
// 获取deployment资源,确认服务是否正在运行
// 执行并返回输出
sshCommand remote: remote, command: "${change_name_script}"
def num = sshCommand remote: remote, command: "${check_running_script}"
echo "${deployment_name} running num:${num}"
if ("${num}" > 0) {
echo "${deployment_name} already running, delete it now"
// 执行并返回输出
def pid = sshCommand remote: remote, command: "${get_pid_script}"
echo pid
sshCommand remote: remote, command: "kill -9 ${pid}"
sshCommand remote: remote, command: "sleep 5s"
}
echo "deploying ${deployment_name}"
//jenkins 默认会关闭衍生的子进程,声明如下环境变量JENKINS_NODE_COOKIE,使子进程不被关闭
withEnv(['JENKINS_NODE_COOKIE=dontkillme']) {
sshCommand remote: remote, command: "${deployment_script}"
}
num = sshCommand remote: remote, command: "${get_pid_script}"
echo "${deployment_name} running num:${num}"
def tail_log_script = "tail -f ${remote_path}/${log_name} | sed '/程序启动成功\\|程序启动失败/Q'"
sshCommand remote: remote, command: "${tail_log_script}"
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。