pipeline { agent any tools { nodejs 'node14' } parameters { choice name: 'branch', description: '选择分支', choices: ['master'] } stages { stage('Checkout') { when { not { environment name: 'branch', value: '' } } steps { git branch: "${env.branch}", credentialsId: 'id_rsa_example', url: 'git@example.com:java/yylr-ui.git' } } stage('Compile') { when { not { environment name: 'branch', value: '' } } steps { sh ''' npm set registry https://registry.npmmirror.com/ npm set disturl https://npmmirror.com/mirrors/node npm set sass_binary_site https://registry.npmmirror.com/-/binary/node-sass npm set chromedriver_cdnurl https://registry.npmmirror.com/-/binary/chromedriver ''' // sh 'npm install -g cnpm' sh 'npm install' // sh 'npm rebuild node-sass' // sh 'npm run build:stage' sh 'npm run build' sh 'ls -al' sh 'test -d dist && tar -cvzf dist.tar.gz dist' } } stage('Deploy') { when { not { environment name: 'branch', value: '' } } steps { script { def remoteDir = '/opt/yylr/admin-ui' def dt = new Date().format('yyyy-MMdd-HHmmss') withCredentials([sshUserPrivateKey(credentialsId: '95402717-acc1-4ad3-9bba-33db35086a48', keyFileVariable: 'identity')]) { def remote = [name: '11.22.33.44', host: '11.22.33.44', user: 'deploy', port: 22, allowAnyHosts: true, identityFile: identity] sshPut remote: remote, from: 'dist.tar.gz', into: "${remoteDir}/dist.tar.gz" sshCommand remote: remote, command: """ cd ${remoteDir} #if test -d dist; then # mv -f dist dist-bak-${dt} #fi rm -fr dist tar -xzf dist.tar.gz """ } } } } } }