1 Star 1 Fork 0

The俊/email_test_demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
jenkins-test-demo.gradle 3.64 KB
一键复制 编辑 原始数据 按行查看 历史
The俊 提交于 2025-01-03 10:34 . 1111
//脚本化流水线
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}"
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/the_jun/email_test_demo.git
[email protected]:the_jun/email_test_demo.git
the_jun
email_test_demo
email_test_demo
master

搜索帮助