jenkins配置,脚本代码
pipeline {
agent any
parameters {
string (
name: 'PROJECT_NAME',
defaultValue: 'motolive.58moto.com',
description: '项目名称',
trim: true
)
string (
name: 'ALIAS',
defaultValue: 'live',
description: '子项目',
trim: true
)
string (
name: 'REMOTE_PATH',
defaultValue: '/data/wwwroot/motolive.58moto.com',
description: '目标路径',
trim: true
)
string (
name: 'GIT_URL',
defaultValue: 'http://gitlab.jddmoto.com/h5/oss_moto_live.git',
description: 'GIT地址',
trim: true
)
string (
name: 'GIT_BRANCH',
defaultValue: 'develop',
description: 'GIT分支',
trim: true
)
string (
name: 'HOST_IP',
defaultValue: '172.16.247.31',
description: '服务器IP',
trim: true
)
string (
name: 'HOST_PORT',
defaultValue: '62222',
description: '服务器PORT',
trim: true
)
string (
name: 'HOST_USER',
defaultValue: 'root',
description: '服务器账号',
trim: true
)
password (
name: 'HOST_PASS',
defaultValue: '123qwe!@#',
description: '服务器密码'
)
}
environment{
ZIP_FILE_NAME = ''
}
stages {
stage('代码获取') {
environment{
GIT_CRED_ID='749e2eb1-3eb3-499e-a9df-ef9f0d1232ae'
}
steps {
script {
git branch: params.GIT_BRANCH, credentialsId: GIT_CRED_ID, url: params.GIT_URL
def commit_no = sh(returnStdout: true, script: 'git rev-parse HEAD').trim().take(6)
def date = new Date().format('yyyyMMdd')
def version = params.ALIAS+'_'+params.GIT_BRANCH+'_'+commit_no+'_'+date+'_'+env.BUILD_NUMBER
ZIP_FILE_NAME = params.PROJECT_NAME+'.'+version+'.zip'
}
}
}
stage('NPM Build') {
steps {
script {
sh "npm run build:dev"
zip dir: "dist/${params.PROJECT_NAME}", archive: true, glob:'', zipFile: ZIP_FILE_NAME
}
}
}
stage('部署至服务器') {
steps {
script {
def ansible_str = params.HOST_IP+' ansible_ssh_user='+params.HOST_USER+' ansible_ssh_port='+params.HOST_PORT+' ansible_ssh_pass="'+params.HOST_PASS+'"'
writeFile file: 'ansible-hosts', text: '[all]'+'\n'+ansible_str
sh "ansible ${params.HOST_IP} -i ansible-hosts -m shell -a 'mkdir -p ${REMOTE_PATH}'"
sh "ansible ${params.HOST_IP} -i ansible-hosts -m unarchive -a 'src=${ZIP_FILE_NAME} dest=${REMOTE_PATH} mode=0755'"
}
}
}
}
post {
cleanup {
deleteDir()
dir(env.WORKSPACE+"@tmp") {
deleteDir()
}
}
}
}
上诉代码可以通过,参数化配置生成
参数配置,操作界面,有配置界面自动生成,并且接受相关参数