-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
90 lines (89 loc) · 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
pipeline {
agent {
node {
label "master"
}
}
environment {
GOOS = 'linux'
GOARCH = "amd64"
MYPROJECTNAME = 'myddns'
BUILDBIN = "${MYPROJECTNAME}_${GOARCH}"
AliyunAccessId = ""
AliyunAccessKey = ""
DDNSDomain = ""
}
tools {
go 'go'
}
stages {
stage('Checkout') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: 'refs/tags/*']],
userRemoteConfigs: [[
url: '[email protected]:huyinghuan/ddns.git',
credentialsId: '1d6fabe3-4e94-419b-9a48-a90fd3a0f327',
]]
])
}
}
stage("编译"){
environment {
ProjectVersion = """${sh(
returnStdout: true,
script: "git describe --tags"
).trim()}"""
BuildTime = """${sh(
returnStdout: true,
script: "date '+%Y-%m-%d %H:%M:%S'"
).trim()}
"""
argsVersion = "main.Version=${ProjectVersion}"
argsBuildTime = "main.BuildTime=${BuildTime}"
}
steps {
sh '''
go build -o ${BUILDBIN} -mod=vendor -ldflags="-X '${argsVersion}' -X '${argsBuildTime}'"
'''
// 存储编译文件
stash name: "bin_${BUILDBIN}", includes: "${BUILDBIN}"
sh "rm ${BUILDBIN}"
}
}
stage("部署"){
agent {
node {
label "LAN-UB"
customWorkspace "/home/hyh/service/"
}
}
steps{
sh "mkdir -p /home/hyh/service"
// 取出编译二进制文件
unstash name: "bin_${BUILDBIN}"
sh """
if [ "\$(pm2 id ${MYPROJECTNAME})" != "[]" ]; then
pm2 stop ${MYPROJECTNAME};
fi
chmod +x ${BUILDBIN}
cp ${BUILDBIN} ${MYPROJECTNAME}
if [ "\$(pm2 id ${MYPROJECTNAME})" = "[]" ]; then
pm2 start ${MYPROJECTNAME} --name ${MYPROJECTNAME} -l start.${MYPROJECTNAME}.log --interpreter none -- --accessId ${AliyunAccessId} --accessKey ${AliyunAccessKey} --domain ${DDNSDomain};
else
pm2 restart ${MYPROJECTNAME};
fi
#等待2秒,判断是否启动成功
sleep 2s
if [ "\$(pm2 pid ${MYPROJECTNAME})" = "0" ]; then
pm2 delete ${MYPROJECTNAME}
echo "启动失败"
exit 1;
fi
pm2 save
"""
}
}
}
}