分享一段新手也能看懂的jenkins部署脚本,便于其他同学快速入门.
步骤
下面实现了代码从仓库到目标服务器的部署过程,分别是
源码
pipeline {
agent any
parameters{
string(name:'site_hash',defaultValue:'deploy_001',description:'站点名称')
string(name:'git_url',defaultValue:'https://xxx.com/xxxx.git',description:'仓库地址')
string(name:'version',defaultValue:'v1.0.0',description:'版本')
string(name:'build_name',defaultValue:'deploy_001',description:'打包名称')
string(name:'server_name',defaultValue:'dev',description:'服务器名称')
string(name:'server_host',defaultValue:'xx.xx.xx.xx',description:'服务器ip')
string(name:'server_user',defaultValue:'root',description:'服务器账号')
string(name:'server_password',defaultValue:'xxxxxxxxxx',description:'服务器密码')
string(name:'back_path',defaultValue:'/www/jenkins/deploy',description:'备份目录')
string(name:'release_path',defaultValue:'/www/jenkins/deploy',description:'发布目录')
}
stages {
stage('拉取'){
steps{
deleteDir()
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: '$version']], extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]], userRemoteConfigs: [[url: '$git_url']]]
}
}
stage('配置服务') {
steps {
sh 'curl https://xxx.s.xxxx.cn/xxxx?sign=xxxxxx -o env'
}
}
stage('打包') {
steps {
sh 'tar -zcvf "$build_name".tar.gz *'
}
}
stage("检查环境"){
steps{
echo 'check...'
script{
def remote = [:]
remote.name = "${params.server_name}"
remote.host = "${params.server_host}"
remote.user = "${params.server_user}"
remote.password = "${params.server_password}"
remote.allowAnyHosts = true
sshCommand remote:remote,command:"mkdir -p ${params.back_path}/${params.build_name}"
}
}
}
stage('部署') {
steps {
echo 'Deploying....'
script{
def remote = [:]
remote.name = "${params.server_name}"
remote.host = "${params.server_host}"
remote.user = "${params.server_user}"
remote.password = "${params.server_password}"
remote.allowAnyHosts = true
sshPut remote: remote, from: "${env.WORKSPACE}/${params.build_name}", into: "${params.back_path}/${params.build_name}"
sshCommand remote:remote,command:"cd ${params.back_path}/${params.build_name} && tar -xvf ${params.build_name} && rm -f ${params.build_name}"
}
}
}
stage('发布') {
steps {
echo '发布....'
script{
def remote = [:]
remote.name = "${params.server_name}"
remote.host = "${params.server_host}"
remote.user = "${params.server_user}"
remote.password = "${params.server_password}"
remote.allowAnyHosts = true
sshCommand remote:remote,command:"ln -snf ${params.back_path}/${params.build_name} ${params.release_path}"
}
}
}
stage('清理工作空间'){
steps{
deleteDir()
}
}
}
}
评论已关闭