1.选中coding的中要自动化部署的项目
2.点击创建构建计划
3. 本项目使用ssh。
4.按照图片上的提示填上去就行
5.
6.
7.
8. 选择流程配置,将以下代码填写上去就行
def remoteConfig = [:]
remoteConfig.name = "my-remote-server"
remoteConfig.host = "${REMOTE_HOST}"
remoteConfig.allowAnyHosts = true
// 使用 SCP 作为文件传输
remoteConfig.fileTransfer = "scp"
pipeline {
agent any
stages {
stage('安装Node.js') {
steps {
sh 'rm -rf /usr/lib/node_modules/npm/'
dir ('/root/.cache/downloads') {
sh 'wget -nc "https://coding-public-generic.pkg.coding.net/public/downloads/node-linux-x64.tar.xz?version=v16.13.0" -O node-v16.13.0-linux-x64.tar.xz | true'
sh 'tar -xf node-v16.13.0-linux-x64.tar.xz -C /usr --strip-components 1'
// sh 'wget -nc "https://coding-public-generic.pkg.coding.net/public/downloads/node-linux-x64.tar.xz?version=v14.18.2" -O node-v14.18.2-linux-x64.tar.xz | true'
// sh 'tar -xf node-v14.18.2-linux-x64.tar.xz -C /usr --strip-components 1'
// 更多版本:v12.22.7、v17.2.0
}
sh 'node -v'
}
}
stage("检出") {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: env.GIT_BUILD_REF]],
userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]
])
}
}
stage("安装依赖") {
steps {
sh "pnpm i"
}
}
stage("编译") {
steps {
sh "pnpm build"
}
}
stage('部署') {
steps {
echo '发布中...'
script {
def remote = [:]
remote.name = 'server2'
remote.allowAnyHosts = true
remote.host = "${REMOTE_HOST}"
remote.port = 22
remote.user = "${REMOTE_USER_NAME}"
withCredentials([ sshUserPrivateKey(credentialsId:"${REMOTE_CRED}"
,keyFileVariable:'SSH_PRIVATE_KEY_PATH')]) {
remote.identityFile = SSH_PRIVATE_KEY_PATH
sshPut remote: remote, from: './dist/.', into: '/html/aia'
sshCommand remote: remote, sudo: true, command: "cp -rf /html/aia/dist/* /html/aia"
sshCommand remote: remote, sudo: true, command: "rm -rf /html/aia/dist"
}
}
echo '发布完成.'
}
}
}
}
9.填写变量
10.点击立即构建即可