成功部署的第一個(gè)項(xiàng)目就是通過pipeline,下面分享一下經(jīng)驗(yàn)
主要是變量定義和使用部分闯参, SSH Pipeline Steps。
這里需要配置兩個(gè)全局憑證,git和更新服務(wù)器的。也可以使用明文密碼晤锥。
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!'
}
參考資料:
jenkins pipelines 使用ssh 例子
jenkins + pipeline構(gòu)建自動(dòng)化部署