基于Jenkins自动打包并部署Tomcat环境
// Jenkinsfile 示例
pipeline {
agent any
stages {
stage('检出代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'your-git-credentials', url: 'your-git-repo-url']]])
}
}
stage('构建') {
steps {
sh 'mvn clean package'
}
}
stage('部署') {
steps {
script {
def SERVER_ID = 'your-server-id'
def WAR_FILE = 'target/your-app.war'
def TOMCAT_USER = 'your-tomcat-username'
def TOMCAT_PASSWORD = 'your-tomcat-password'
// 上传WAR包
sshCommand remote: $SERVER_ID, command: "mkdir -p /opt/tomcat && mv /home/jenkins/workspace/${JOB_NAME}/${WAR_FILE} /opt/tomcat/"
// 停止Tomcat
sshCommand remote: $SERVER_ID, command: "sudo /opt/tomcat/bin/shutdown.sh"
// 替换应用
sshCommand remote: $SERVER_ID, command: "sudo rm -rf /opt/tomcat/webapps/ROOT"
sshCommand remote: $SERVER_ID, command: "sudo ln -s /opt/tomcat/webapps/your-app.war /opt/tomcat/webapps/ROOT.war"
// 启动Tomcat
withCredentials([usernamePassword(credentialsId: 'your-tomcat-credentials', passwordVariable: 'TOMCAT_PASSWORD', usernameVariable: 'TOMCAT_USER')]) {
sshCommand remote: $SERVER_ID, command: "sudo /opt/tomcat/bin/startup.sh"
}
}
}
}
}
}
这个Jenkinsfile定义了一个简单的pipeline,包括检出代码、构建和部署阶段。在构建阶段使用了Maven来打包应用程序,在部署阶段使用了SSH命令来上传WAR包、重启Tomcat。注意,你需要替换脚本中的'your-*'占位符为实际的值。
评论已关闭