-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
50 lines (47 loc) · 1.4 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
pipeline {
agent {label 'master'}
options {
skipStagesAfterUnstable()
timeout(time: 1, unit: 'HOURS')
}
environment {def server = ''}
stages {
stage('远程拉取') {
steps {
// 初始化参数
script {server = getServer()}
// 在目标服务器上获取最新代码
sshCommand remote: server, command: 'cd /root/TTMS.Api && git pull'
}
}
stage('远程构建') {
steps {
// 在目标服务器上构建docker镜像
sshCommand remote: server, command: 'cd /root/TTMS.Api/src && podman build -t ttms -f TTMS.Api/Dockerfile .'
}
}
stage('远程启动') {
steps {
// 在目标服务器上停止容器、并根据新构建的docker镜像启动容器
sshCommand remote: server, command: 'podman stop ttms; podman rm ttms; podman run --rm -d -p 8082:80 --name ttms ttms'
}
}
}
}
// 定义一个方法,返回ssh连接所需的信息
def getServer() {
def remote = [:]
remote.name = "ssh"
remote.host = "chdxia.com"
remote.port = 22
remote.allowAnyHosts = true
// 这里不展示明文密码,所以在jenkins凭据里提取
withCredentials([usernamePassword(
credentialsId: "a94f0f8e-c562-434a-aed7-8e08afa3670b",
usernameVariable: "username",
passwordVariable: "password")]) {
remote.user = "${username}"
remote.password = "${password}"
}
return remote
}