成功部署的第一个项目就是通过pipeline,下面分享一下经验
主要是变量定义和使用部分, SSH Pipeline Steps。
这里需要配置两个全局凭证,git和更新服务器的。也可以使用明文密码。
node () {
def workspace = pwd()
def env='sit'
def basedir ='projectname-'+env
def remote = [:]
remote.name = env
remote.host = 'ip地址'
remote.user = 'root'
remote.allowAnyHosts = true
stage 'checkout'
dir(basedir){
git branch:'master', credentialsId:'git-cre', url:'git地址'
}
stage('build') {
sh"""
cd $workspace/projectname-$env
#cnpm install
#cnpm run build:$env
npm -v
"""
}
withCredentials([sshUserPrivateKey(credentialsId: 'id-cre', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'root')]) {
remote.user = root
remote.identityFile = identity
echo 'hello Mr.'+env
stage('upload') {
sshPut remote: remote, from: 'projectname-'+env+'/dist', into: '/root/'+env
}
stage('backup') {
sshCommand remote: remote, command: "cd /root/${env} && cp -r tb-web html-back/projectname`date '+%Y%m%d%H%M%S'` "
}
stage('cover') {
sshCommand remote: remote, command: "cd /root/${env} && rm -rf projectname && mv dist projectname "
}
}
echo 'done!'
}
网友评论