-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
executable file
·53 lines (50 loc) · 1.3 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
pipeline {
agent {label 'master'}
tools {nodejs '16.18.0'}
options {
skipStagesAfterUnstable()
timeout(time: 1, unit: 'HOURS')
}
environment {def server = ''}
stages {
stage('清理文件') {
steps {
// 初始化参数
script {
server = getServer()
}
// 在远程主机上删除项目文件
sshCommand remote: server, command: 'rm -rf /usr/share/nginx/lrtest-web/dist'
}
}
stage('本地构建') {
steps {
// 删除历史构建,重新在本地构建
sh 'rm -rf ./dist && npm install && npm run build:prod'
}
}
stage('远程部署') {
steps {
// 将构建好的文件部署到远程服务器
sshPut remote: server, from: "dist", into: "/usr/share/nginx/lrtest-web"
}
}
}
}
// 定义一个方法,返回ssh连接所需的信息
def getServer() {
def remote = [:]
remote.name = "ssh"
remote.host = "chdxia.com"
remote.port = 22
remote.allowAnyHosts = true
withCredentials([usernamePassword(
credentialsId: "f76fdeee-5bbb-46c2-8d2c-2916f45576c9",
usernameVariable: "username",
passwordVariable: "password"
)]) {
remote.user = "${username}"
remote.password = "${password}"
}
return remote
}