pipeline { agent any parameters { choice name: 'action', description: '动作', choices: ['', 'deploy'] stashedFile name: 'h5.zip', description: '上传zip包(仅deploy动作必传,包内h5作为一级目录或不设置一级目录)' } stages { stage('Deploy') { when { not { environment name: 'action', value: '' } } steps { script { def remoteDir = '/var/DigSys/cloud/h5-ui' def dt = new Date().format('yyyy-MMdd-HHmmss') withCredentials([sshUserPrivateKey(credentialsId: 'id_rsa_example', keyFileVariable: 'identity')]) { def remote = [name: '11.22.33.44', host: '11.22.33.44', user: 'root', port: 9003, allowAnyHosts: true, identityFile: identity] if (env.action == 'deploy') { echo "获取并检查zip文件大小..." unstash 'h5.zip' sh 'test -s h5.zip' echo "重新打包..." sh """ unzip h5.zip -d h5 if [ -d h5/h5 ]; then tar -C h5 -czf h5.tar.gz h5 else tar -czf h5.tar.gz h5 fi """ echo "传输文件..." sshPut remote: remote, from: "h5.tar.gz", into: remoteDir + '/h5.tar.gz' sh 'rm -fr h5.zip h5.tar.gz h5' echo "部署文件..." sshCommand remote: remote, command: """ cd ${remoteDir} if [ -d h5 ]; then mv h5 h5-bak-${dt} fi tar -xzf h5.tar.gz """ } } } } } } }