注:jenkins需要安装SSH Pipeline Steps插件
#!groovy
pipeline {
agent {
node {
label 'maven'
}
}
//拉取代码
stages {
stage('clone code') {
agent none
steps {
git(url: "${params.git_url}", credentialsId: "${params.gitCredentialsId}", branch: "${params.branch_name}", changelog: true, poll: false)
}
}
//编译代码
stage(' build') {
agent none
steps {
container('maven') {
sh 'mvn clean package -DskipTests -P test'
}
}
}
//上传代码
stage('SSH') {
steps {
script {
def remote = [:]
remote.name = 'test'
remote.host = '47.122.56.xxx'
remote.allowAnyHosts = true
withCredentials([usernamePassword(credentialsId: 'test', passwordVariable: 'password', usernameVariable: 'username')]) {
remote.user = "${username}"
remote.password = "${password}"
}
sshCommand remote: remote, command: "rm -f /root/ROOT.war"
sshPut remote: remote, from: './target/ROOT.war', into: '/root/ROOT.war'
sshCommand remote: remote, command: "systemctl stop tomcat"
sshCommand remote: remote, command: "rm -rf /www/server/tomcat9/webapps/ROOT"
sshCommand remote: remote, command: " unzip /root/ROOT.war -d /www/server/tomcat9/webapps/ROOT"
sshCommand remote: remote, command: "systemctl start tomcat"
}
}
}
}
environment {
}
parameters {
string(name: 'project_name', defaultValue: 'live-server', description: '项目名称')
string(name: 'git_url', defaultValue: 'http://192.168.x.xx:xxx/root/xxx.git', description: 'git仓库地址')
string(name: 'gitCredentialsId', defaultValue: 'gitlab-id', description: 'git访问凭证')
string(name: 'deploy_version', defaultValue: 'latest', description: '发布版本')
string(name: 'branch_name', defaultValue: 'develop', description: 'git分支')
string(name: 'profiles', defaultValue: 'prod', description: '环境')
string(name: 'k8sServerUrl', defaultValue: 'https://192.168.x.xx:6443', description: 'k8s集群服务地址')
}
}