pipeline {
agent any
//options
options {
buildDiscarder logRotator(artifactDaysToKeepStr: '',
artifactNumToKeepStr: '',
daysToKeepStr: '5',
numToKeepStr: '5')
}
// parameters
parameters {
// string (name:'gitRepo',defaultValue:'ssh://git@10xxxxxx/root/lcfex-amp.git',description:'代码仓库')
//gitParameter (name: 'gitBranch', defaultValue:'develop-1.0', description:'分支')
gitParameter name: 'gitBranch',
type: 'PT_BRANCH_TAG',
branchFilter: 'origin/(.*)',
defaultValue: 'master',
selectedValue: 'DEFAULT',
sortMode: 'ASCENDING_SMART',
quickFilterEnabled: true,
listSize: '20',
description: '选择分支.'
}
// post
// post{
// success{
// mail body: '2222' , subject: "111", to: 'xxxxx6@dingtalk.com'
// }
// failure{
// mail body: '2222', subject: "111", to: 'zxxxxx46@dingtalk.com'
// }
// unstable{
// mail body: '2222', subject: "111", to: 'xxxxx746@dingtalk.com'
// }
// }
// stages
stages {
stage('gitlab code') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: '$gitBranch']],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[url: 'ssh://git@xxxxxxxx/root/lcfex-amp.git']]])
}
}
stage('打包源码 ') {
steps{
sh '''
cd $WORKSPACE
if [ -f ./amp.tar.gz ];then
rm -rf amp.tar.gz
echo "打包程序"
tar -zcf amp.tar.gz amp || exit 1
else
echo "打包程序"
tar -zcvf amp.tar.gz amp || exit 1
fi
echo "上传程序"
/usr/bin/sshpass -p xxxxx scp -P 23798 -o StrictHostKeychecking=no amp.tar.gz developer@xxxxxx:/data/project/amp/beta/amp_app/ || exit 1
'''
}
}
stage('depoy') {
steps{
sh '''
# 备份操作
/usr/bin/sshpass -p xxxxx ssh -T -p 23798 -o StrictHostKeychecking=no developer@xxxxxx<<EOF
cd /data/project/amp/beta/amp_app/
if [ ! -d amp_bak ];then
mkdir -pv amp_bak
fi
cp -rf amp amp_bak/amp_$(date +%Y%m%d_%H%M)
if [ -f ./amp.tar.gz ];then
echo "解压程序"
tar -zxf amp.tar.gz || exit 1
rm -rf amp.tar.gz || exit 2
else
echo "更新包不存在"
exit 2
fi
if [ -d amp_bak ];then
echo "清除备份"
cd amp_bak/ || exit 3
ls -t|tail -n +6|xargs rm -rf
fi
EOF
'''
}
}
// 启动服务并且探测端
stage('start service') {
steps{
sh '''
/usr/bin/sshpass -p xxxxxxxssh -T -p 23798 -o StrictHostKeychecking=no developer@xxxxxx <<EOF
docker container exec amp_beta sh /data/project/web/amp_app/manageproject.sh restart || exit 1
EOF
'''
}
}
}
}